cam_mem_mgr.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * @dentry: Debugfs entry
  80. * @alloc_profile_enable: Whether to enable alloc profiling
  81. * @dbg_buf_idx: debug buffer index to get usecases info
  82. * @force_cache_allocs: Force all internal buffer allocations with cache
  83. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  84. * allocations.
  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. */
  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. struct dentry *dentry;
  97. bool alloc_profile_enable;
  98. size_t dbg_buf_idx;
  99. bool force_cache_allocs;
  100. bool need_shared_buffer_padding;
  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. #endif
  108. };
  109. /**
  110. * struct cam_mem_table_mini_dump
  111. *
  112. * @bufq: array of buffers
  113. * @dbg_buf_idx: debug buffer index to get usecases info
  114. * @alloc_profile_enable: Whether to enable alloc profiling
  115. * @dbg_buf_idx: debug buffer index to get usecases info
  116. * @force_cache_allocs: Force all internal buffer allocations with cache
  117. * @need_shared_buffer_padding: Whether padding is needed for shared buffer
  118. * allocations.
  119. */
  120. struct cam_mem_table_mini_dump {
  121. struct cam_mem_buf_queue bufq[CAM_MEM_BUFQ_MAX];
  122. size_t dbg_buf_idx;
  123. bool alloc_profile_enable;
  124. bool force_cache_allocs;
  125. bool need_shared_buffer_padding;
  126. };
  127. /**
  128. * @brief: Allocates and maps buffer
  129. *
  130. * @cmd: Allocation information
  131. *
  132. * @return Status of operation. Negative in case of error. Zero otherwise.
  133. */
  134. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd *cmd);
  135. /**
  136. * @brief: Releases a buffer reference
  137. *
  138. * @cmd: Buffer release information
  139. *
  140. * @return Status of operation. Negative in case of error. Zero otherwise.
  141. */
  142. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  143. /**
  144. * @brief Maps a buffer
  145. *
  146. * @cmd: Buffer mapping information
  147. *
  148. * @return Status of operation. Negative in case of error. Zero otherwise.
  149. */
  150. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd *cmd);
  151. /**
  152. * @brief: Perform cache ops on the buffer
  153. *
  154. * @cmd: Cache ops information
  155. *
  156. * @return Status of operation. Negative in case of error. Zero otherwise.
  157. */
  158. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  159. /**
  160. * @brief: Initializes the memory manager
  161. *
  162. * @return Status of operation. Negative in case of error. Zero otherwise.
  163. */
  164. int cam_mem_mgr_init(void);
  165. /**
  166. * @brief: Tears down the memory manager
  167. *
  168. * @return None
  169. */
  170. void cam_mem_mgr_deinit(void);
  171. /**
  172. * @brief: Copy buffer content to presil mem for all buffers of
  173. * iommu handle
  174. *
  175. * @return Status of operation. Negative in case of error. Zero otherwise.
  176. */
  177. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  178. /**
  179. * @brief: Copy buffer content of single buffer to presil
  180. *
  181. * @return Status of operation. Negative in case of error. Zero otherwise.
  182. */
  183. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  184. /**
  185. * @brief: Copy back buffer content of single buffer from
  186. * presil
  187. *
  188. * @return Status of operation. Negative in case of error. Zero otherwise.
  189. */
  190. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  191. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  192. #endif /* _CAM_MEM_MGR_H_ */