cam_mem_mgr.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 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. * @system_heap: Handle to system heap
  85. * @system_uncached_heap: Handle to system uncached heap
  86. * @camera_heap: Handle to camera heap
  87. * @camera_uncached_heap: Handle to camera uncached heap
  88. * @secure_display_heap: Handle to secure display heap
  89. * @ubwc_p_heap: Handle to ubwc-p heap
  90. */
  91. struct cam_mem_table {
  92. struct mutex m_lock;
  93. void *bitmap;
  94. size_t bits;
  95. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  96. size_t dbg_buf_idx;
  97. bool force_cache_allocs;
  98. bool need_shared_buffer_padding;
  99. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  100. struct dma_heap *system_heap;
  101. struct dma_heap *system_uncached_heap;
  102. struct dma_heap *camera_heap;
  103. struct dma_heap *camera_uncached_heap;
  104. struct dma_heap *secure_display_heap;
  105. struct dma_heap *ubwc_p_heap;
  106. #endif
  107. };
  108. /**
  109. * struct cam_mem_table_mini_dump
  110. *
  111. * @bufq: array of buffers
  112. * @dbg_buf_idx: debug buffer index to get usecases info
  113. * @alloc_profile_enable: Whether to enable alloc profiling
  114. * @dbg_buf_idx: debug buffer index to get usecases info
  115. * @force_cache_allocs: Force all internal buffer allocations with cache
  116. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  117. * allocations.
  118. */
  119. struct cam_mem_table_mini_dump {
  120. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  121. size_t dbg_buf_idx;
  122. bool alloc_profile_enable;
  123. bool force_cache_allocs;
  124. bool need_shared_buffer_padding;
  125. };
  126. /**
  127. * @brief: Allocates and maps buffer
  128. *
  129. * @cmd: Allocation information
  130. *
  131. * @return Status of operation. Negative in case of error. Zero otherwise.
  132. */
  133. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd_v2 *cmd);
  134. /**
  135. * @brief: Releases a buffer reference
  136. *
  137. * @cmd: Buffer release information
  138. *
  139. * @return Status of operation. Negative in case of error. Zero otherwise.
  140. */
  141. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  142. /**
  143. * @brief Maps a buffer
  144. *
  145. * @cmd: Buffer mapping information
  146. *
  147. * @return Status of operation. Negative in case of error. Zero otherwise.
  148. */
  149. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd_v2 *cmd);
  150. /**
  151. * @brief: Perform cache ops on the buffer
  152. *
  153. * @cmd: Cache ops information
  154. *
  155. * @return Status of operation. Negative in case of error. Zero otherwise.
  156. */
  157. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  158. /**
  159. * @brief: Perform cpu access ops on the buffer
  160. *
  161. * @cmd: CPU access ops information
  162. *
  163. * @return Status of operation. Negative in case of error. Zero otherwise.
  164. */
  165. int cam_mem_mgr_cpu_access_op(struct cam_mem_cpu_access_op *cmd);
  166. /**
  167. * @brief: Check whether ubwc-p heap is supported
  168. *
  169. * @return true if supported, false otherwise
  170. */
  171. bool cam_mem_mgr_ubwc_p_heap_supported(void);
  172. /**
  173. * @brief: Initializes the memory manager
  174. *
  175. * @return Status of operation. Negative in case of error. Zero otherwise.
  176. */
  177. int cam_mem_mgr_init(void);
  178. /**
  179. * @brief: Tears down the memory manager
  180. *
  181. * @return None
  182. */
  183. void cam_mem_mgr_deinit(void);
  184. #ifdef CONFIG_CAM_PRESIL
  185. /**
  186. * @brief: Put dma-buf for input dmabuf
  187. *
  188. * @return Status of operation. Negative in case of error. Zero otherwise.
  189. */
  190. int cam_mem_mgr_put_dmabuf_from_fd(uint64_t input_dmabuf);
  191. /**
  192. * @brief: Create a fd for dma-buf
  193. *
  194. * @return Status of operation. Negative in case of error. Zero or
  195. * Positive otherwise.
  196. */
  197. int cam_mem_mgr_get_fd_from_dmabuf(uint64_t input_dmabuf);
  198. #endif /* ifdef CONFIG_CAM_PRESIL */
  199. /**
  200. * @brief: Copy buffer content to presil mem for all buffers of
  201. * iommu handle
  202. *
  203. * @return Status of operation. Negative in case of error. Zero otherwise.
  204. */
  205. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  206. /**
  207. * @brief: Copy buffer content of single buffer to presil
  208. *
  209. * @return Status of operation. Negative in case of error. Zero otherwise.
  210. */
  211. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  212. /**
  213. * @brief: Copy back buffer content of single buffer from
  214. * presil
  215. *
  216. * @return Status of operation. Negative in case of error. Zero otherwise.
  217. */
  218. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  219. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  220. #endif /* _CAM_MEM_MGR_H_ */