cam_mem_mgr.h 8.3 KB

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