cam_mem_mgr.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef _CAM_MEM_MGR_H_
  7. #define _CAM_MEM_MGR_H_
  8. #include <linux/mutex.h>
  9. #include <linux/dma-buf.h>
  10. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  11. #include <linux/dma-heap.h>
  12. #endif
  13. #include <media/cam_req_mgr.h>
  14. #include "cam_mem_mgr_api.h"
  15. /* Enum for possible mem mgr states */
  16. enum cam_mem_mgr_state {
  17. CAM_MEM_MGR_UNINITIALIZED,
  18. CAM_MEM_MGR_INITIALIZED,
  19. };
  20. /*Enum for memory allocation initiator */
  21. enum cam_mem_mgr_allocator {
  22. CAM_MEMMGR_ALLOC_USER,
  23. CAM_MEMMGR_ALLOC_KERNEL,
  24. };
  25. /*Enum for possible SMMU operations */
  26. enum cam_smmu_mapping_client {
  27. CAM_SMMU_MAPPING_USER,
  28. CAM_SMMU_MAPPING_KERNEL,
  29. };
  30. #ifdef CONFIG_CAM_PRESIL
  31. struct cam_presil_dmabuf_params {
  32. int32_t fd_for_umd_daemon;
  33. uint32_t refcount;
  34. };
  35. #endif
  36. /**
  37. * struct cam_mem_buf_hw_vaddr_info
  38. *
  39. * @iommu_hdl: IOMMU handle for the given bank
  40. * @vaddr: IOVA of the buffer
  41. * @len: cached length for a given handle
  42. * @addr_updated: Indicates if entry is updated only for addr caching
  43. * @valid_mapping: Indicates if entry is indeed a valid mapping for this buf
  44. *
  45. */
  46. struct cam_mem_buf_hw_hdl_info {
  47. int32_t iommu_hdl;
  48. dma_addr_t vaddr;
  49. size_t len;
  50. bool addr_updated;
  51. bool valid_mapping;
  52. };
  53. /**
  54. * struct cam_mem_buf_queue
  55. *
  56. * @dma_buf: pointer to the allocated dma_buf in the table
  57. * @q_lock: mutex lock for buffer
  58. * @fd: file descriptor of buffer
  59. * @i_ino: inode number of this dmabuf. Uniquely identifies a buffer
  60. * @buf_handle: unique handle for buffer
  61. * @align: alignment for allocation
  62. * @len: size of buffer
  63. * @flags: attributes of buffer
  64. * @num_hdls: number of valid handles
  65. * @vaddr_info: Array of IOVA addresses mapped for different devices
  66. * using the same indexing as SMMU
  67. * @kmdvaddr: Kernel virtual address
  68. * @active: state of the buffer
  69. * @is_imported: Flag indicating if buffer is imported from an FD in user space
  70. * @is_internal: Flag indicating kernel allocated buffer
  71. * @timestamp: Timestamp at which this entry in tbl was made
  72. * @presil_params: Parameters specific to presil environment
  73. */
  74. struct cam_mem_buf_queue {
  75. struct dma_buf *dma_buf;
  76. struct mutex q_lock;
  77. int32_t fd;
  78. unsigned long i_ino;
  79. int32_t buf_handle;
  80. int32_t align;
  81. size_t len;
  82. uint32_t flags;
  83. uintptr_t kmdvaddr;
  84. int32_t num_hdls;
  85. struct cam_mem_buf_hw_hdl_info *hdls_info;
  86. bool active;
  87. bool is_imported;
  88. bool is_internal;
  89. struct timespec64 timestamp;
  90. #ifdef CONFIG_CAM_PRESIL
  91. struct cam_presil_dmabuf_params presil_params;
  92. #endif
  93. };
  94. /**
  95. * struct cam_mem_table
  96. *
  97. * @m_lock: mutex lock for table
  98. * @bitmap: bitmap of the mem mgr utility
  99. * @bits: max bits of the utility
  100. * @bufq: array of buffers
  101. * @dbg_buf_idx: debug buffer index to get usecases info
  102. * @max_hdls_supported: Maximum number of SMMU device handles supported
  103. * A buffer can only be mapped for these number of
  104. * device context banks
  105. * @max_hdls_info_size: Size of the hdls array allocated per buffer,
  106. * computed value to be used in driver
  107. * @force_cache_allocs: Force all internal buffer allocations with cache
  108. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  109. * allocations.
  110. * @csf_version: Camera security framework version
  111. * @system_heap: Handle to system heap
  112. * @system_movable_heap: Handle to system movable heap
  113. * @system_uncached_heap: Handle to system uncached heap
  114. * @camera_heap: Handle to camera heap
  115. * @camera_uncached_heap: Handle to camera uncached heap
  116. * @secure_display_heap: Handle to secure display heap
  117. * @ubwc_p_heap: Handle to ubwc-p heap
  118. * @ubwc_p_movable_heap: Handle to ubwc-p movable heap
  119. */
  120. struct cam_mem_table {
  121. struct mutex m_lock;
  122. void *bitmap;
  123. size_t bits;
  124. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  125. size_t dbg_buf_idx;
  126. int32_t max_hdls_supported;
  127. size_t max_hdls_info_size;
  128. bool force_cache_allocs;
  129. bool need_shared_buffer_padding;
  130. struct cam_csf_version csf_version;
  131. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  132. struct dma_heap *system_heap;
  133. struct dma_heap *system_movable_heap;
  134. struct dma_heap *system_uncached_heap;
  135. struct dma_heap *camera_heap;
  136. struct dma_heap *camera_uncached_heap;
  137. struct dma_heap *secure_display_heap;
  138. struct dma_heap *ubwc_p_heap;
  139. struct dma_heap *ubwc_p_movable_heap;
  140. #endif
  141. };
  142. /**
  143. * struct cam_mem_table_mini_dump
  144. *
  145. * @bufq: array of buffers
  146. * @dbg_buf_idx: debug buffer index to get usecases info
  147. * @alloc_profile_enable: Whether to enable alloc profiling
  148. * @dbg_buf_idx: debug buffer index to get usecases info
  149. * @force_cache_allocs: Force all internal buffer allocations with cache
  150. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  151. * allocations.
  152. */
  153. struct cam_mem_table_mini_dump {
  154. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  155. size_t dbg_buf_idx;
  156. bool alloc_profile_enable;
  157. bool force_cache_allocs;
  158. bool need_shared_buffer_padding;
  159. };
  160. /**
  161. * @brief: Allocates and maps buffer
  162. *
  163. * @cmd: Allocation information
  164. *
  165. * @return Status of operation. Negative in case of error. Zero otherwise.
  166. */
  167. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd_v2 *cmd);
  168. /**
  169. * @brief: Releases a buffer reference
  170. *
  171. * @cmd: Buffer release information
  172. *
  173. * @return Status of operation. Negative in case of error. Zero otherwise.
  174. */
  175. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  176. /**
  177. * @brief Maps a buffer
  178. *
  179. * @cmd: Buffer mapping information
  180. *
  181. * @return Status of operation. Negative in case of error. Zero otherwise.
  182. */
  183. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd_v2 *cmd);
  184. /**
  185. * @brief: Perform cache ops on the buffer
  186. *
  187. * @cmd: Cache ops information
  188. *
  189. * @return Status of operation. Negative in case of error. Zero otherwise.
  190. */
  191. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  192. /**
  193. * @brief: Perform cpu access ops on the buffer
  194. *
  195. * @cmd: CPU access ops information
  196. *
  197. * @return Status of operation. Negative in case of error. Zero otherwise.
  198. */
  199. int cam_mem_mgr_cpu_access_op(struct cam_mem_cpu_access_op *cmd);
  200. /**
  201. * @brief: Check whether ubwc-p heap is supported
  202. *
  203. * @return true if supported, false otherwise
  204. */
  205. bool cam_mem_mgr_ubwc_p_heap_supported(void);
  206. /**
  207. * @brief: Initializes the memory manager
  208. *
  209. * @return Status of operation. Negative in case of error. Zero otherwise.
  210. */
  211. int cam_mem_mgr_init(void);
  212. /**
  213. * @brief: Tears down the memory manager
  214. *
  215. * @return None
  216. */
  217. void cam_mem_mgr_deinit(void);
  218. #ifdef CONFIG_CAM_PRESIL
  219. /**
  220. * @brief: Put dma-buf for input dmabuf
  221. *
  222. * @return Status of operation. Negative in case of error. Zero otherwise.
  223. */
  224. int cam_mem_mgr_put_dmabuf_from_fd(uint64_t input_dmabuf);
  225. /**
  226. * @brief: Create a fd for dma-buf
  227. *
  228. * @return Status of operation. Negative in case of error. Zero or
  229. * Positive otherwise.
  230. */
  231. int cam_mem_mgr_get_fd_from_dmabuf(uint64_t input_dmabuf);
  232. #endif /* ifdef CONFIG_CAM_PRESIL */
  233. /**
  234. * @brief: Copy buffer content to presil mem for all buffers of
  235. * iommu handle
  236. *
  237. * @return Status of operation. Negative in case of error. Zero otherwise.
  238. */
  239. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  240. /**
  241. * @brief: Copy buffer content of single buffer to presil
  242. *
  243. * @return Status of operation. Negative in case of error. Zero otherwise.
  244. */
  245. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  246. /**
  247. * @brief: Copy back buffer content of single buffer from
  248. * presil
  249. *
  250. * @return Status of operation. Negative in case of error. Zero otherwise.
  251. */
  252. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  253. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  254. /**
  255. * @brief: Dump mem mgr info into user buffer
  256. *
  257. * @return Status of operation. Negative in case of error. Zero otherwise.
  258. */
  259. int cam_mem_mgr_dump_user(struct cam_dump_req_cmd *dump_req);
  260. #endif /* _CAM_MEM_MGR_H_ */