cam_mem_mgr.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. */
  90. struct cam_mem_table {
  91. struct mutex m_lock;
  92. void *bitmap;
  93. size_t bits;
  94. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  95. size_t dbg_buf_idx;
  96. bool force_cache_allocs;
  97. bool need_shared_buffer_padding;
  98. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  99. struct dma_heap *system_heap;
  100. struct dma_heap *system_uncached_heap;
  101. struct dma_heap *camera_heap;
  102. struct dma_heap *camera_uncached_heap;
  103. struct dma_heap *secure_display_heap;
  104. #endif
  105. };
  106. /**
  107. * struct cam_mem_table_mini_dump
  108. *
  109. * @bufq: array of buffers
  110. * @dbg_buf_idx: debug buffer index to get usecases info
  111. * @alloc_profile_enable: Whether to enable alloc profiling
  112. * @dbg_buf_idx: debug buffer index to get usecases info
  113. * @force_cache_allocs: Force all internal buffer allocations with cache
  114. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  115. * allocations.
  116. */
  117. struct cam_mem_table_mini_dump {
  118. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  119. size_t dbg_buf_idx;
  120. bool alloc_profile_enable;
  121. bool force_cache_allocs;
  122. bool need_shared_buffer_padding;
  123. };
  124. /**
  125. * @brief: Allocates and maps buffer
  126. *
  127. * @cmd: Allocation information
  128. *
  129. * @return Status of operation. Negative in case of error. Zero otherwise.
  130. */
  131. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd *cmd);
  132. /**
  133. * @brief: Releases a buffer reference
  134. *
  135. * @cmd: Buffer release information
  136. *
  137. * @return Status of operation. Negative in case of error. Zero otherwise.
  138. */
  139. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  140. /**
  141. * @brief Maps a buffer
  142. *
  143. * @cmd: Buffer mapping information
  144. *
  145. * @return Status of operation. Negative in case of error. Zero otherwise.
  146. */
  147. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd *cmd);
  148. /**
  149. * @brief: Perform cache ops on the buffer
  150. *
  151. * @cmd: Cache ops information
  152. *
  153. * @return Status of operation. Negative in case of error. Zero otherwise.
  154. */
  155. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  156. /**
  157. * @brief: Initializes the memory manager
  158. *
  159. * @return Status of operation. Negative in case of error. Zero otherwise.
  160. */
  161. int cam_mem_mgr_init(void);
  162. /**
  163. * @brief: Tears down the memory manager
  164. *
  165. * @return None
  166. */
  167. void cam_mem_mgr_deinit(void);
  168. #ifdef CONFIG_CAM_PRESIL
  169. /**
  170. * @brief: Put dma-buf for input dmabuf
  171. *
  172. * @return Status of operation. Negative in case of error. Zero otherwise.
  173. */
  174. int cam_mem_mgr_put_dmabuf_from_fd(uint64_t input_dmabuf);
  175. /**
  176. * @brief: Create a fd for dma-buf
  177. *
  178. * @return Status of operation. Negative in case of error. Zero or
  179. * Positive otherwise.
  180. */
  181. int cam_mem_mgr_get_fd_from_dmabuf(uint64_t input_dmabuf);
  182. #endif /* ifdef CONFIG_CAM_PRESIL */
  183. /**
  184. * @brief: Copy buffer content to presil mem for all buffers of
  185. * iommu handle
  186. *
  187. * @return Status of operation. Negative in case of error. Zero otherwise.
  188. */
  189. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  190. /**
  191. * @brief: Copy buffer content of single buffer to presil
  192. *
  193. * @return Status of operation. Negative in case of error. Zero otherwise.
  194. */
  195. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  196. /**
  197. * @brief: Copy back buffer content of single buffer from
  198. * presil
  199. *
  200. * @return Status of operation. Negative in case of error. Zero otherwise.
  201. */
  202. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  203. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  204. #endif /* _CAM_MEM_MGR_H_ */