cam_mem_mgr.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. */
  83. struct cam_mem_buf_queue {
  84. struct dma_buf *dma_buf;
  85. struct mutex q_lock;
  86. int32_t fd;
  87. unsigned long i_ino;
  88. int32_t buf_handle;
  89. int32_t align;
  90. size_t len;
  91. uint32_t flags;
  92. uintptr_t kmdvaddr;
  93. int32_t num_hdls;
  94. struct cam_mem_buf_hw_hdl_info *hdls_info;
  95. bool active;
  96. bool release_deferred;
  97. bool is_imported;
  98. bool is_internal;
  99. struct timespec64 timestamp;
  100. struct kref krefcount;
  101. enum cam_smmu_mapping_client smmu_mapping_client;
  102. char buf_name[CAM_DMA_BUF_NAME_LEN];
  103. #ifdef CONFIG_CAM_PRESIL
  104. struct cam_presil_dmabuf_params presil_params;
  105. #endif
  106. struct kref urefcount;
  107. };
  108. /**
  109. * struct cam_mem_table
  110. *
  111. * @m_lock: mutex lock for table
  112. * @bitmap: bitmap of the mem mgr utility
  113. * @bits: max bits of the utility
  114. * @bufq: array of buffers
  115. * @dbg_buf_idx: debug buffer index to get usecases info
  116. * @max_hdls_supported: Maximum number of SMMU device handles supported
  117. * A buffer can only be mapped for these number of
  118. * device context banks
  119. * @max_hdls_info_size: Size of the hdls array allocated per buffer,
  120. * computed value to be used in driver
  121. * @force_cache_allocs: Force all internal buffer allocations with cache
  122. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  123. * allocations.
  124. * @csf_version: Camera security framework version
  125. * @system_heap: Handle to system heap
  126. * @system_movable_heap: Handle to system movable heap
  127. * @system_uncached_heap: Handle to system uncached heap
  128. * @camera_heap: Handle to camera heap
  129. * @camera_uncached_heap: Handle to camera uncached heap
  130. * @secure_display_heap: Handle to secure display heap
  131. * @ubwc_p_heap: Handle to ubwc-p heap
  132. * @ubwc_p_movable_heap: Handle to ubwc-p movable heap
  133. */
  134. struct cam_mem_table {
  135. struct mutex m_lock;
  136. void *bitmap;
  137. size_t bits;
  138. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  139. size_t dbg_buf_idx;
  140. int32_t max_hdls_supported;
  141. size_t max_hdls_info_size;
  142. bool force_cache_allocs;
  143. bool need_shared_buffer_padding;
  144. struct cam_csf_version csf_version;
  145. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  146. struct dma_heap *system_heap;
  147. struct dma_heap *system_movable_heap;
  148. struct dma_heap *system_uncached_heap;
  149. struct dma_heap *camera_heap;
  150. struct dma_heap *camera_uncached_heap;
  151. struct dma_heap *secure_display_heap;
  152. struct dma_heap *ubwc_p_heap;
  153. struct dma_heap *ubwc_p_movable_heap;
  154. #endif
  155. };
  156. /**
  157. * struct cam_mem_table_mini_dump
  158. *
  159. * @bufq: array of buffers
  160. * @dbg_buf_idx: debug buffer index to get usecases info
  161. * @alloc_profile_enable: Whether to enable alloc profiling
  162. * @dbg_buf_idx: debug buffer index to get usecases info
  163. * @force_cache_allocs: Force all internal buffer allocations with cache
  164. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  165. * allocations.
  166. */
  167. struct cam_mem_table_mini_dump {
  168. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  169. size_t dbg_buf_idx;
  170. bool alloc_profile_enable;
  171. bool force_cache_allocs;
  172. bool need_shared_buffer_padding;
  173. };
  174. /**
  175. * @brief: Allocates and maps buffer
  176. *
  177. * @cmd: Allocation information
  178. *
  179. * @return Status of operation. Negative in case of error. Zero otherwise.
  180. */
  181. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd_v2 *cmd);
  182. /**
  183. * @brief: Releases a buffer reference
  184. *
  185. * @cmd: Buffer release information
  186. *
  187. * @return Status of operation. Negative in case of error. Zero otherwise.
  188. */
  189. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  190. /**
  191. * @brief Maps a buffer
  192. *
  193. * @cmd: Buffer mapping information
  194. *
  195. * @return Status of operation. Negative in case of error. Zero otherwise.
  196. */
  197. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd_v2 *cmd);
  198. /**
  199. * @brief: Perform cache ops on the buffer
  200. *
  201. * @cmd: Cache ops information
  202. *
  203. * @return Status of operation. Negative in case of error. Zero otherwise.
  204. */
  205. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  206. /**
  207. * @brief: Perform cpu access ops on the buffer
  208. *
  209. * @cmd: CPU access ops information
  210. *
  211. * @return Status of operation. Negative in case of error. Zero otherwise.
  212. */
  213. int cam_mem_mgr_cpu_access_op(struct cam_mem_cpu_access_op *cmd);
  214. /**
  215. * @brief: Provide all supported heap capabilities
  216. *
  217. * @heap_mask: Update mask for all supported heaps
  218. *
  219. * @return Status of operation. Negative in case of error. Zero otherwise.
  220. */
  221. int cam_mem_mgr_check_for_supported_heaps(uint64_t *heap_mask);
  222. /**
  223. * @brief: Initializes the memory manager
  224. *
  225. * @return Status of operation. Negative in case of error. Zero otherwise.
  226. */
  227. int cam_mem_mgr_init(void);
  228. /**
  229. * @brief: Tears down the memory manager
  230. *
  231. * @return None
  232. */
  233. void cam_mem_mgr_deinit(void);
  234. #ifdef CONFIG_CAM_PRESIL
  235. /**
  236. * @brief: Put dma-buf for input dmabuf
  237. *
  238. * @return Status of operation. Negative in case of error. Zero otherwise.
  239. */
  240. int cam_mem_mgr_put_dmabuf_from_fd(uint64_t input_dmabuf);
  241. /**
  242. * @brief: Create a fd for dma-buf
  243. *
  244. * @return Status of operation. Negative in case of error. Zero or
  245. * Positive otherwise.
  246. */
  247. int cam_mem_mgr_get_fd_from_dmabuf(uint64_t input_dmabuf);
  248. #endif /* ifdef CONFIG_CAM_PRESIL */
  249. /**
  250. * @brief: Copy buffer content to presil mem for all buffers of
  251. * iommu handle
  252. *
  253. * @return Status of operation. Negative in case of error. Zero otherwise.
  254. */
  255. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  256. /**
  257. * @brief: Copy buffer content of single buffer to presil
  258. *
  259. * @return Status of operation. Negative in case of error. Zero otherwise.
  260. */
  261. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  262. /**
  263. * @brief: Copy back buffer content of single buffer from
  264. * presil
  265. *
  266. * @return Status of operation. Negative in case of error. Zero otherwise.
  267. */
  268. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  269. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  270. /**
  271. * @brief: Dump mem mgr info into user buffer
  272. *
  273. * @return Status of operation. Negative in case of error. Zero otherwise.
  274. */
  275. int cam_mem_mgr_dump_user(struct cam_dump_req_cmd *dump_req);
  276. #endif /* _CAM_MEM_MGR_H_ */