cam_mem_mgr.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef _CAM_MEM_MGR_H_
  6. #define _CAM_MEM_MGR_H_
  7. #include <linux/mutex.h>
  8. #include <linux/dma-buf.h>
  9. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  10. #include <linux/dma-heap.h>
  11. #endif
  12. #include <media/cam_req_mgr.h>
  13. #include "cam_mem_mgr_api.h"
  14. /* Enum for possible mem mgr states */
  15. enum cam_mem_mgr_state {
  16. CAM_MEM_MGR_UNINITIALIZED,
  17. CAM_MEM_MGR_INITIALIZED,
  18. };
  19. /*Enum for possible SMMU operations */
  20. enum cam_smmu_mapping_client {
  21. CAM_SMMU_MAPPING_USER,
  22. CAM_SMMU_MAPPING_KERNEL,
  23. };
  24. #ifdef CONFIG_CAM_PRESIL
  25. struct cam_presil_dmabuf_params {
  26. int32_t fd_for_umd_daemon;
  27. uint32_t refcount;
  28. };
  29. #endif
  30. /**
  31. * struct cam_mem_buf_queue
  32. *
  33. * @dma_buf: pointer to the allocated dma_buf in the table
  34. * @q_lock: mutex lock for buffer
  35. * @hdls: list of mapped handles
  36. * @num_hdl: number of handles
  37. * @fd: file descriptor of buffer
  38. * @i_ino: inode number of this dmabuf. Uniquely identifies a buffer
  39. * @buf_handle: unique handle for buffer
  40. * @align: alignment for allocation
  41. * @len: size of buffer
  42. * @flags: attributes of buffer
  43. * @vaddr: IOVA of buffer
  44. * @kmdvaddr: Kernel virtual address
  45. * @active: state of the buffer
  46. * @is_imported: Flag indicating if buffer is imported from an FD in user space
  47. * @is_internal: Flag indicating kernel allocated buffer
  48. * @timestamp: Timestamp at which this entry in tbl was made
  49. * @presil_params: Parameters specific to presil environment
  50. */
  51. struct cam_mem_buf_queue {
  52. struct dma_buf *dma_buf;
  53. struct mutex q_lock;
  54. int32_t hdls[CAM_MEM_MMU_MAX_HANDLE];
  55. int32_t num_hdl;
  56. int32_t fd;
  57. unsigned long i_ino;
  58. int32_t buf_handle;
  59. int32_t align;
  60. size_t len;
  61. uint32_t flags;
  62. dma_addr_t vaddr;
  63. uintptr_t kmdvaddr;
  64. bool active;
  65. bool is_imported;
  66. bool is_internal;
  67. struct timespec64 timestamp;
  68. #ifdef CONFIG_CAM_PRESIL
  69. struct cam_presil_dmabuf_params presil_params;
  70. #endif
  71. };
  72. /**
  73. * struct cam_mem_table
  74. *
  75. * @m_lock: mutex lock for table
  76. * @bitmap: bitmap of the mem mgr utility
  77. * @bits: max bits of the utility
  78. * @bufq: array of buffers
  79. * @dbg_buf_idx: debug buffer index to get usecases info
  80. * @force_cache_allocs: Force all internal buffer allocations with cache
  81. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  82. * allocations.
  83. * @system_heap: Handle to system heap
  84. * @system_uncached_heap: Handle to system uncached heap
  85. * @camera_heap: Handle to camera heap
  86. * @camera_uncached_heap: Handle to camera uncached heap
  87. * @secure_display_heap: Handle to secure display heap
  88. */
  89. struct cam_mem_table {
  90. struct mutex m_lock;
  91. void *bitmap;
  92. size_t bits;
  93. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  94. size_t dbg_buf_idx;
  95. bool force_cache_allocs;
  96. bool need_shared_buffer_padding;
  97. #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
  98. struct dma_heap *system_heap;
  99. struct dma_heap *system_uncached_heap;
  100. struct dma_heap *camera_heap;
  101. struct dma_heap *camera_uncached_heap;
  102. struct dma_heap *secure_display_heap;
  103. #endif
  104. };
  105. /**
  106. * struct cam_mem_table_mini_dump
  107. *
  108. * @bufq: array of buffers
  109. * @dbg_buf_idx: debug buffer index to get usecases info
  110. * @alloc_profile_enable: Whether to enable alloc profiling
  111. * @dbg_buf_idx: debug buffer index to get usecases info
  112. * @force_cache_allocs: Force all internal buffer allocations with cache
  113. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  114. * allocations.
  115. */
  116. struct cam_mem_table_mini_dump {
  117. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  118. size_t dbg_buf_idx;
  119. bool alloc_profile_enable;
  120. bool force_cache_allocs;
  121. bool need_shared_buffer_padding;
  122. };
  123. /**
  124. * @brief: Allocates and maps buffer
  125. *
  126. * @cmd: Allocation information
  127. *
  128. * @return Status of operation. Negative in case of error. Zero otherwise.
  129. */
  130. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd *cmd);
  131. /**
  132. * @brief: Releases a buffer reference
  133. *
  134. * @cmd: Buffer release information
  135. *
  136. * @return Status of operation. Negative in case of error. Zero otherwise.
  137. */
  138. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  139. /**
  140. * @brief Maps a buffer
  141. *
  142. * @cmd: Buffer mapping information
  143. *
  144. * @return Status of operation. Negative in case of error. Zero otherwise.
  145. */
  146. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd *cmd);
  147. /**
  148. * @brief: Perform cache ops on the buffer
  149. *
  150. * @cmd: Cache ops information
  151. *
  152. * @return Status of operation. Negative in case of error. Zero otherwise.
  153. */
  154. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  155. /**
  156. * @brief: Initializes the memory manager
  157. *
  158. * @return Status of operation. Negative in case of error. Zero otherwise.
  159. */
  160. int cam_mem_mgr_init(void);
  161. /**
  162. * @brief: Tears down the memory manager
  163. *
  164. * @return None
  165. */
  166. void cam_mem_mgr_deinit(void);
  167. /**
  168. * @brief: Copy buffer content to presil mem for all buffers of
  169. * iommu handle
  170. *
  171. * @return Status of operation. Negative in case of error. Zero otherwise.
  172. */
  173. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  174. /**
  175. * @brief: Copy buffer content of single buffer to presil
  176. *
  177. * @return Status of operation. Negative in case of error. Zero otherwise.
  178. */
  179. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  180. /**
  181. * @brief: Copy back buffer content of single buffer from
  182. * presil
  183. *
  184. * @return Status of operation. Negative in case of error. Zero otherwise.
  185. */
  186. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  187. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  188. #endif /* _CAM_MEM_MGR_H_ */