cam_mem_mgr.h 5.4 KB

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