cam_mem_mgr.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 possible SMMU operations */
  21. enum cam_smmu_mapping_client {
  22. CAM_SMMU_MAPPING_USER,
  23. CAM_SMMU_MAPPING_KERNEL,
  24. };
  25. #ifdef CONFIG_CAM_PRESIL
  26. struct cam_presil_dmabuf_params {
  27. int32_t fd_for_umd_daemon;
  28. uint32_t refcount;
  29. };
  30. #endif
  31. /**
  32. * struct cam_mem_buf_queue
  33. *
  34. * @dma_buf: pointer to the allocated dma_buf in the table
  35. * @q_lock: mutex lock for buffer
  36. * @hdls: list of mapped handles
  37. * @num_hdl: number of handles
  38. * @fd: file descriptor of buffer
  39. * @i_ino: inode number of this dmabuf. Uniquely identifies a buffer
  40. * @buf_handle: unique handle for buffer
  41. * @align: alignment for allocation
  42. * @len: size of buffer
  43. * @flags: attributes of buffer
  44. * @vaddr: IOVA of buffer
  45. * @kmdvaddr: Kernel virtual address
  46. * @active: state of the buffer
  47. * @is_imported: Flag indicating if buffer is imported from an FD in user space
  48. * @is_internal: Flag indicating kernel allocated buffer
  49. * @timestamp: Timestamp at which this entry in tbl was made
  50. * @presil_params: Parameters specific to presil environment
  51. */
  52. struct cam_mem_buf_queue {
  53. struct dma_buf *dma_buf;
  54. struct mutex q_lock;
  55. int32_t hdls[CAM_MEM_MMU_MAX_HANDLE];
  56. int32_t num_hdl;
  57. int32_t fd;
  58. unsigned long i_ino;
  59. int32_t buf_handle;
  60. int32_t align;
  61. size_t len;
  62. uint32_t flags;
  63. dma_addr_t vaddr;
  64. uintptr_t kmdvaddr;
  65. bool active;
  66. bool is_imported;
  67. bool is_internal;
  68. struct timespec64 timestamp;
  69. #ifdef CONFIG_CAM_PRESIL
  70. struct cam_presil_dmabuf_params presil_params;
  71. #endif
  72. };
  73. /**
  74. * struct cam_mem_table
  75. *
  76. * @m_lock: mutex lock for table
  77. * @bitmap: bitmap of the mem mgr utility
  78. * @bits: max bits of the utility
  79. * @bufq: array of buffers
  80. * @dbg_buf_idx: debug buffer index to get usecases info
  81. * @force_cache_allocs: Force all internal buffer allocations with cache
  82. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  83. * allocations.
  84. * @csf_version: Camera security framework version
  85. * @system_heap: Handle to system heap
  86. * @system_uncached_heap: Handle to system uncached heap
  87. * @camera_heap: Handle to camera heap
  88. * @camera_uncached_heap: Handle to camera uncached heap
  89. * @secure_display_heap: Handle to secure display heap
  90. * @ubwc_p_heap: Handle to ubwc-p heap
  91. */
  92. struct cam_mem_table {
  93. struct mutex m_lock;
  94. void *bitmap;
  95. size_t bits;
  96. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  97. size_t dbg_buf_idx;
  98. bool force_cache_allocs;
  99. bool need_shared_buffer_padding;
  100. struct cam_csf_version csf_version;
  101. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  102. struct dma_heap *system_heap;
  103. struct dma_heap *system_uncached_heap;
  104. struct dma_heap *camera_heap;
  105. struct dma_heap *camera_uncached_heap;
  106. struct dma_heap *secure_display_heap;
  107. struct dma_heap *ubwc_p_heap;
  108. #endif
  109. };
  110. /**
  111. * struct cam_mem_table_mini_dump
  112. *
  113. * @bufq: array of buffers
  114. * @dbg_buf_idx: debug buffer index to get usecases info
  115. * @alloc_profile_enable: Whether to enable alloc profiling
  116. * @dbg_buf_idx: debug buffer index to get usecases info
  117. * @force_cache_allocs: Force all internal buffer allocations with cache
  118. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  119. * allocations.
  120. */
  121. struct cam_mem_table_mini_dump {
  122. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  123. size_t dbg_buf_idx;
  124. bool alloc_profile_enable;
  125. bool force_cache_allocs;
  126. bool need_shared_buffer_padding;
  127. };
  128. /**
  129. * @brief: Allocates and maps buffer
  130. *
  131. * @cmd: Allocation information
  132. *
  133. * @return Status of operation. Negative in case of error. Zero otherwise.
  134. */
  135. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd_v2 *cmd);
  136. /**
  137. * @brief: Releases a buffer reference
  138. *
  139. * @cmd: Buffer release information
  140. *
  141. * @return Status of operation. Negative in case of error. Zero otherwise.
  142. */
  143. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  144. /**
  145. * @brief Maps a buffer
  146. *
  147. * @cmd: Buffer mapping information
  148. *
  149. * @return Status of operation. Negative in case of error. Zero otherwise.
  150. */
  151. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd_v2 *cmd);
  152. /**
  153. * @brief: Perform cache ops on the buffer
  154. *
  155. * @cmd: Cache ops information
  156. *
  157. * @return Status of operation. Negative in case of error. Zero otherwise.
  158. */
  159. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  160. /**
  161. * @brief: Perform cpu access ops on the buffer
  162. *
  163. * @cmd: CPU access ops information
  164. *
  165. * @return Status of operation. Negative in case of error. Zero otherwise.
  166. */
  167. int cam_mem_mgr_cpu_access_op(struct cam_mem_cpu_access_op *cmd);
  168. /**
  169. * @brief: Check whether ubwc-p heap is supported
  170. *
  171. * @return true if supported, false otherwise
  172. */
  173. bool cam_mem_mgr_ubwc_p_heap_supported(void);
  174. /**
  175. * @brief: Initializes the memory manager
  176. *
  177. * @return Status of operation. Negative in case of error. Zero otherwise.
  178. */
  179. int cam_mem_mgr_init(void);
  180. /**
  181. * @brief: Tears down the memory manager
  182. *
  183. * @return None
  184. */
  185. void cam_mem_mgr_deinit(void);
  186. #ifdef CONFIG_CAM_PRESIL
  187. /**
  188. * @brief: Put dma-buf for input dmabuf
  189. *
  190. * @return Status of operation. Negative in case of error. Zero otherwise.
  191. */
  192. int cam_mem_mgr_put_dmabuf_from_fd(uint64_t input_dmabuf);
  193. /**
  194. * @brief: Create a fd for dma-buf
  195. *
  196. * @return Status of operation. Negative in case of error. Zero or
  197. * Positive otherwise.
  198. */
  199. int cam_mem_mgr_get_fd_from_dmabuf(uint64_t input_dmabuf);
  200. #endif /* ifdef CONFIG_CAM_PRESIL */
  201. /**
  202. * @brief: Copy buffer content to presil mem for all buffers of
  203. * iommu handle
  204. *
  205. * @return Status of operation. Negative in case of error. Zero otherwise.
  206. */
  207. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  208. /**
  209. * @brief: Copy buffer content of single buffer to presil
  210. *
  211. * @return Status of operation. Negative in case of error. Zero otherwise.
  212. */
  213. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  214. /**
  215. * @brief: Copy back buffer content of single buffer from
  216. * presil
  217. *
  218. * @return Status of operation. Negative in case of error. Zero otherwise.
  219. */
  220. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  221. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  222. /**
  223. * @brief: Dump mem mgr info into user buffer
  224. *
  225. * @return Status of operation. Negative in case of error. Zero otherwise.
  226. */
  227. int cam_mem_mgr_dump_user(struct cam_dump_req_cmd *dump_req);
  228. #endif /* _CAM_MEM_MGR_H_ */