cam_mem_mgr.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * @brief: Allocates and maps buffer
  101. *
  102. * @cmd: Allocation information
  103. *
  104. * @return Status of operation. Negative in case of error. Zero otherwise.
  105. */
  106. int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd *cmd);
  107. /**
  108. * @brief: Releases a buffer reference
  109. *
  110. * @cmd: Buffer release information
  111. *
  112. * @return Status of operation. Negative in case of error. Zero otherwise.
  113. */
  114. int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd);
  115. /**
  116. * @brief Maps a buffer
  117. *
  118. * @cmd: Buffer mapping information
  119. *
  120. * @return Status of operation. Negative in case of error. Zero otherwise.
  121. */
  122. int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd *cmd);
  123. /**
  124. * @brief: Perform cache ops on the buffer
  125. *
  126. * @cmd: Cache ops information
  127. *
  128. * @return Status of operation. Negative in case of error. Zero otherwise.
  129. */
  130. int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd);
  131. /**
  132. * @brief: Initializes the memory manager
  133. *
  134. * @return Status of operation. Negative in case of error. Zero otherwise.
  135. */
  136. int cam_mem_mgr_init(void);
  137. /**
  138. * @brief: Tears down the memory manager
  139. *
  140. * @return None
  141. */
  142. void cam_mem_mgr_deinit(void);
  143. /**
  144. * @brief: Copy buffer content to presil mem for all buffers of
  145. * iommu handle
  146. *
  147. * @return Status of operation. Negative in case of error. Zero otherwise.
  148. */
  149. int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl);
  150. /**
  151. * @brief: Copy buffer content of single buffer to presil
  152. *
  153. * @return Status of operation. Negative in case of error. Zero otherwise.
  154. */
  155. int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle);
  156. /**
  157. * @brief: Copy back buffer content of single buffer from
  158. * presil
  159. *
  160. * @return Status of operation. Negative in case of error. Zero otherwise.
  161. */
  162. int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
  163. uint32_t buf_size, uint32_t offset, int32_t iommu_hdl);
  164. #endif /* _CAM_MEM_MGR_H_ */