cam_mem_mgr.h 7.2 KB

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