qdf_mem.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #else
  89. void *
  90. qdf_mem_malloc(qdf_size_t size);
  91. #endif
  92. void *qdf_mem_alloc_outline(qdf_device_t osdev, qdf_size_t size);
  93. /**
  94. * qdf_mem_free() - free QDF memory
  95. * @ptr: Pointer to the starting address of the memory to be free'd.
  96. * This function will free the memory pointed to by 'ptr'.
  97. * Return:
  98. * None
  99. */
  100. void qdf_mem_free(void *ptr);
  101. void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value);
  102. void qdf_mem_zero(void *ptr, uint32_t num_bytes);
  103. void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  104. void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  105. void qdf_mem_free_outline(void *buf);
  106. void *qdf_mem_alloc_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
  107. qdf_dma_addr_t *paddr);
  108. void qdf_mem_free_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
  109. void *vaddr, qdf_dma_addr_t paddr,
  110. qdf_dma_context_t memctx);
  111. void qdf_mem_zero_outline(void *buf, qdf_size_t size);
  112. void qdf_ether_addr_copy(void *dst_addr, const void *src_addr);
  113. /**
  114. * qdf_mem_cmp() - memory compare
  115. * @memory1: pointer to one location in memory to compare.
  116. * @memory2: pointer to second location in memory to compare.
  117. * @num_bytes: the number of bytes to compare.
  118. *
  119. * Function to compare two pieces of memory, similar to memcmp function
  120. * in standard C.
  121. * Return:
  122. * int32_t - returns an int value that tells if the memory
  123. * locations are equal or not equal.
  124. * 0 -- equal
  125. * < 0 -- *memory1 is less than *memory2
  126. * > 0 -- *memory1 is bigger than *memory2
  127. */
  128. static inline int32_t qdf_mem_cmp(const void *memory1, const void *memory2,
  129. uint32_t num_bytes)
  130. {
  131. return __qdf_mem_cmp(memory1, memory2, num_bytes);
  132. }
  133. /**
  134. * qdf_str_cmp - Compare two strings
  135. * @str1: First string
  136. * @str2: Second string
  137. * Return: =0 equal
  138. * >0 not equal, if str1 sorts lexicographically after str2
  139. * <0 not equal, if str1 sorts lexicographically before str2
  140. */
  141. static inline int32_t qdf_str_cmp(const char *str1, const char *str2)
  142. {
  143. return __qdf_str_cmp(str1, str2);
  144. }
  145. /**
  146. * qdf_str_lcopy - Copy from one string to another
  147. * @dest: destination string
  148. * @src: source string
  149. * @bytes: limit of num bytes to copy
  150. * Return: =0 returns the initial value of dest
  151. */
  152. static inline uint32_t qdf_str_lcopy(char *dest, const char *src, uint32_t bytes)
  153. {
  154. return __qdf_str_lcopy(dest, src, bytes);
  155. }
  156. /**
  157. * qdf_mem_map_nbytes_single - Map memory for DMA
  158. * @osdev: pomter OS device context
  159. * @buf: pointer to memory to be dma mapped
  160. * @dir: DMA map direction
  161. * @nbytes: number of bytes to be mapped.
  162. * @phy_addr: ponter to recive physical address.
  163. *
  164. * Return: success/failure
  165. */
  166. static inline uint32_t qdf_mem_map_nbytes_single(qdf_device_t osdev, void *buf,
  167. qdf_dma_dir_t dir, int nbytes,
  168. qdf_dma_addr_t *phy_addr)
  169. {
  170. #if defined(HIF_PCI)
  171. return __qdf_mem_map_nbytes_single(osdev, buf, dir, nbytes, phy_addr);
  172. #else
  173. return 0;
  174. #endif
  175. }
  176. /**
  177. * qdf_mem_unmap_nbytes_single() - un_map memory for DMA
  178. * @osdev: pomter OS device context
  179. * @phy_addr: physical address of memory to be dma unmapped
  180. * @dir: DMA unmap direction
  181. * @nbytes: number of bytes to be unmapped.
  182. *
  183. * Return: none
  184. */
  185. static inline void qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
  186. qdf_dma_addr_t phy_addr,
  187. qdf_dma_dir_t dir,
  188. int nbytes)
  189. {
  190. #if defined(HIF_PCI)
  191. __qdf_mem_unmap_nbytes_single(osdev, phy_addr, dir, nbytes);
  192. #endif
  193. }
  194. /**
  195. * qdf_mempool_init - Create and initialize memory pool
  196. * @osdev: platform device object
  197. * @pool_addr: address of the pool created
  198. * @elem_cnt: no. of elements in pool
  199. * @elem_size: size of each pool element in bytes
  200. * @flags: flags
  201. * Return: Handle to memory pool or NULL if allocation failed
  202. */
  203. static inline int qdf_mempool_init(qdf_device_t osdev,
  204. qdf_mempool_t *pool_addr, int elem_cnt,
  205. size_t elem_size, uint32_t flags)
  206. {
  207. return __qdf_mempool_init(osdev, pool_addr, elem_cnt, elem_size,
  208. flags);
  209. }
  210. /**
  211. * qdf_mempool_destroy - Destroy memory pool
  212. * @osdev: platform device object
  213. * @Handle: to memory pool
  214. * Return: none
  215. */
  216. static inline void qdf_mempool_destroy(qdf_device_t osdev, qdf_mempool_t pool)
  217. {
  218. __qdf_mempool_destroy(osdev, pool);
  219. }
  220. /**
  221. * qdf_mempool_alloc - Allocate an element memory pool
  222. * @osdev: platform device object
  223. * @Handle: to memory pool
  224. * Return: Pointer to the allocated element or NULL if the pool is empty
  225. */
  226. static inline void *qdf_mempool_alloc(qdf_device_t osdev, qdf_mempool_t pool)
  227. {
  228. return (void *)__qdf_mempool_alloc(osdev, pool);
  229. }
  230. /**
  231. * qdf_mempool_free - Free a memory pool element
  232. * @osdev: Platform device object
  233. * @pool: Handle to memory pool
  234. * @buf: Element to be freed
  235. * Return: none
  236. */
  237. static inline void qdf_mempool_free(qdf_device_t osdev, qdf_mempool_t pool,
  238. void *buf)
  239. {
  240. __qdf_mempool_free(osdev, pool, buf);
  241. }
  242. void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
  243. qdf_dma_addr_t bus_addr,
  244. qdf_size_t size,
  245. __dma_data_direction direction);
  246. void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
  247. qdf_dma_addr_t bus_addr,
  248. qdf_size_t size,
  249. __dma_data_direction direction);
  250. /**
  251. * qdf_str_len() - returns the length of a string
  252. * @str: input string
  253. * Return:
  254. * length of string
  255. */
  256. static inline int32_t qdf_str_len(const char *str)
  257. {
  258. return __qdf_str_len(str);
  259. }
  260. void qdf_mem_multi_pages_alloc(qdf_device_t osdev,
  261. struct qdf_mem_multi_page_t *pages,
  262. size_t element_size, uint16_t element_num,
  263. qdf_dma_context_t memctxt, bool cacheable);
  264. void qdf_mem_multi_pages_free(qdf_device_t osdev,
  265. struct qdf_mem_multi_page_t *pages,
  266. qdf_dma_context_t memctxt, bool cacheable);
  267. int qdf_mem_multi_page_link(qdf_device_t osdev,
  268. struct qdf_mem_multi_page_t *pages,
  269. uint32_t elem_size, uint32_t elem_count, uint8_t cacheable);
  270. /**
  271. * qdf_mem_skb_inc() - increment total skb allocation size
  272. * @size: size to be added
  273. *
  274. * Return: none
  275. */
  276. void qdf_mem_skb_inc(qdf_size_t size);
  277. /**
  278. * qdf_mem_skb_dec() - decrement total skb allocation size
  279. * @size: size to be decremented
  280. *
  281. * Return: none
  282. */
  283. void qdf_mem_skb_dec(qdf_size_t size);
  284. #endif /* __QDF_MEMORY_H */