cam_mem_mgr.h 8.6 KB

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