cam_mem_mgr.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024 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. * @ref_count: ref count for buffer
  43. * @addr_updated: Indicates if entry is updated only for addr caching
  44. * @valid_mapping: Indicates if entry is indeed a valid mapping for this buf
  45. *
  46. */
  47. struct cam_mem_buf_hw_hdl_info {
  48. int32_t iommu_hdl;
  49. dma_addr_t vaddr;
  50. size_t len;
  51. struct kref *ref_count;
  52. bool addr_updated;
  53. bool valid_mapping;
  54. };
  55. /**
  56. * struct cam_mem_buf_queue
  57. *
  58. * @dma_buf: pointer to the allocated dma_buf in the table
  59. * @q_lock: mutex lock for buffer
  60. * @fd: file descriptor of buffer
  61. * @i_ino: inode number of this dmabuf. Uniquely identifies a buffer
  62. * @buf_handle: unique handle for buffer
  63. * @align: alignment for allocation
  64. * @len: size of buffer
  65. * @flags: attributes of buffer
  66. * @num_hdls: number of valid handles
  67. * @vaddr_info: Array of IOVA addresses mapped for different devices
  68. * using the same indexing as SMMU
  69. * @kmdvaddr: Kernel virtual address
  70. * @active: state of the buffer
  71. * @release_deferred: Buffer is deferred for release.
  72. * @is_imported: Flag indicating if buffer is imported from an FD in user space
  73. * @is_internal: Flag indicating kernel allocated buffer
  74. * @timestamp: Timestamp at which this entry in tbl was made
  75. * @krefcount: Reference counter to track whether the buffer is
  76. * mapped and in use by kmd
  77. * @smmu_mapping_client: Client buffer (User or kernel)
  78. * @buf_name: Name associated with buffer.
  79. * @presil_params: Parameters specific to presil environment
  80. * @urefcount: Reference counter to track whether the buffer is
  81. * mapped and in use by umd
  82. * @ref_lock: Mutex lock for refcount
  83. */
  84. struct cam_mem_buf_queue {
  85. struct dma_buf *dma_buf;
  86. struct mutex q_lock;
  87. int32_t fd;
  88. unsigned long i_ino;
  89. int32_t buf_handle;
  90. int32_t align;
  91. size_t len;
  92. uint32_t flags;
  93. uintptr_t kmdvaddr;
  94. int32_t num_hdls;
  95. struct cam_mem_buf_hw_hdl_info *hdls_info;
  96. bool active;
  97. bool release_deferred;
  98. bool is_imported;
  99. bool is_internal;
  100. struct timespec64 timestamp;
  101. struct kref krefcount;
  102. enum cam_smmu_mapping_client smmu_mapping_client;
  103. char buf_name[CAM_DMA_BUF_NAME_LEN];
  104. #ifdef CONFIG_CAM_PRESIL
  105. struct cam_presil_dmabuf_params presil_params;
  106. #endif
  107. struct kref urefcount;
  108. struct mutex ref_lock;
  109. };
  110. /**
  111. * struct cam_mem_table
  112. *
  113. * @m_lock: mutex lock for table
  114. * @bitmap: bitmap of the mem mgr utility
  115. * @bits: max bits of the utility
  116. * @bufq: array of buffers
  117. * @dbg_buf_idx: debug buffer index to get usecases info
  118. * @max_hdls_supported: Maximum number of SMMU device handles supported
  119. * A buffer can only be mapped for these number of
  120. * device context banks
  121. * @max_hdls_info_size: Size of the hdls array allocated per buffer,
  122. * computed value to be used in driver
  123. * @force_cache_allocs: Force all internal buffer allocations with cache
  124. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  125. * allocations.
  126. * @csf_version: Camera security framework version
  127. * @system_heap: Handle to system heap
  128. * @system_movable_heap: Handle to system movable heap
  129. * @system_uncached_heap: Handle to system uncached heap
  130. * @camera_heap: Handle to camera heap
  131. * @camera_uncached_heap: Handle to camera uncached heap
  132. * @secure_display_heap: Handle to secure display heap
  133. * @ubwc_p_heap: Handle to ubwc-p heap
  134. * @ubwc_p_movable_heap: Handle to ubwc-p movable heap
  135. */
  136. struct cam_mem_table {
  137. struct mutex m_lock;
  138. void *bitmap;
  139. size_t bits;
  140. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  141. size_t dbg_buf_idx;
  142. int32_t max_hdls_supported;
  143. size_t max_hdls_info_size;
  144. bool force_cache_allocs;
  145. bool need_shared_buffer_padding;
  146. struct cam_csf_version csf_version;
  147. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  148. struct dma_heap *system_heap;
  149. struct dma_heap *system_movable_heap;
  150. struct dma_heap *system_uncached_heap;
  151. struct dma_heap *camera_heap;
  152. struct dma_heap *camera_uncached_heap;
  153. struct dma_heap *secure_display_heap;
  154. struct dma_heap *ubwc_p_heap;
  155. struct dma_heap *ubwc_p_movable_heap;
  156. #endif
  157. };
  158. /**
  159. * struct cam_mem_table_mini_dump
  160. *
  161. * @bufq: array of buffers
  162. * @dbg_buf_idx: debug buffer index to get usecases info
  163. * @alloc_profile_enable: Whether to enable alloc profiling
  164. * @dbg_buf_idx: debug buffer index to get usecases info
  165. * @force_cache_allocs: Force all internal buffer allocations with cache
  166. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  167. * allocations.
  168. */
  169. struct cam_mem_table_mini_dump {
  170. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  171. size_t dbg_buf_idx;
  172. bool alloc_profile_enable;
  173. bool force_cache_allocs;
  174. bool need_shared_buffer_padding;
  175. };
  176. /**
  177. * @brief: Allocates and maps buffer
  178. *
  179. * @cmd: Allocation information
  180. *
  181. * @return Status of operation. Negative in case of error. Zero otherwise.
  182. */
  183. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd_v2 *cmd);
  184. /**
  185. * @brief: Releases a buffer reference
  186. *
  187. * @cmd: Buffer release information
  188. *
  189. * @return Status of operation. Negative in case of error. Zero otherwise.
  190. */
  191. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  192. /**
  193. * @brief Maps a buffer
  194. *
  195. * @cmd: Buffer mapping information
  196. *
  197. * @return Status of operation. Negative in case of error. Zero otherwise.
  198. */
  199. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd_v2 *cmd);
  200. /**
  201. * @brief: Perform cache ops on the buffer
  202. *
  203. * @cmd: Cache ops information
  204. *
  205. * @return Status of operation. Negative in case of error. Zero otherwise.
  206. */
  207. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  208. /**
  209. * @brief: Perform cpu access ops on the buffer
  210. *
  211. * @cmd: CPU access ops information
  212. *
  213. * @return Status of operation. Negative in case of error. Zero otherwise.
  214. */
  215. int cam_mem_mgr_cpu_access_op(struct cam_mem_cpu_access_op *cmd);
  216. /**
  217. * @brief: Provide all supported heap capabilities
  218. *
  219. * @heap_mask: Update mask for all supported heaps
  220. *
  221. * @return Status of operation. Negative in case of error. Zero otherwise.
  222. */
  223. int cam_mem_mgr_check_for_supported_heaps(uint64_t *heap_mask);
  224. /**
  225. * @brief: Initializes the memory manager
  226. *
  227. * @return Status of operation. Negative in case of error. Zero otherwise.
  228. */
  229. int cam_mem_mgr_init(void);
  230. /**
  231. * @brief: Tears down the memory manager
  232. *
  233. * @return None
  234. */
  235. void cam_mem_mgr_deinit(void);
  236. #ifdef CONFIG_CAM_PRESIL
  237. /**
  238. * @brief: Put dma-buf for input dmabuf
  239. *
  240. * @return Status of operation. Negative in case of error. Zero otherwise.
  241. */
  242. int cam_mem_mgr_put_dmabuf_from_fd(uint64_t input_dmabuf);
  243. /**
  244. * @brief: Create a fd for dma-buf
  245. *
  246. * @return Status of operation. Negative in case of error. Zero or
  247. * Positive otherwise.
  248. */
  249. int cam_mem_mgr_get_fd_from_dmabuf(uint64_t input_dmabuf);
  250. #endif /* ifdef CONFIG_CAM_PRESIL */
  251. /**
  252. * @brief: Copy buffer content to presil mem for all buffers of
  253. * iommu handle
  254. *
  255. * @return Status of operation. Negative in case of error. Zero otherwise.
  256. */
  257. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  258. /**
  259. * @brief: Copy buffer content of single buffer to presil
  260. *
  261. * @return Status of operation. Negative in case of error. Zero otherwise.
  262. */
  263. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  264. /**
  265. * @brief: Copy back buffer content of single buffer from
  266. * presil
  267. *
  268. * @return Status of operation. Negative in case of error. Zero otherwise.
  269. */
  270. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  271. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  272. /**
  273. * @brief: Dump mem mgr info into user buffer
  274. *
  275. * @return Status of operation. Negative in case of error. Zero otherwise.
  276. */
  277. int cam_mem_mgr_dump_user(struct cam_dump_req_cmd *dump_req);
  278. #endif /* _CAM_MEM_MGR_H_ */