cam_mem_mgr.h 4.7 KB

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