cam_smmu_api.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef _CAM_SMMU_API_H_
  6. #define _CAM_SMMU_API_H_
  7. #include <linux/dma-direction.h>
  8. #include <linux/module.h>
  9. #include <linux/dma-buf.h>
  10. #include <linux/dma-direction.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/iommu.h>
  13. #include <linux/random.h>
  14. #include <linux/spinlock_types.h>
  15. #include <linux/mutex.h>
  16. #include <linux/msm_ion.h>
  17. /*Enum for possible CAM SMMU operations */
  18. enum cam_smmu_ops_param {
  19. CAM_SMMU_ATTACH,
  20. CAM_SMMU_DETACH,
  21. CAM_SMMU_VOTE,
  22. CAM_SMMU_DEVOTE,
  23. CAM_SMMU_OPS_INVALID
  24. };
  25. enum cam_smmu_map_dir {
  26. CAM_SMMU_MAP_READ,
  27. CAM_SMMU_MAP_WRITE,
  28. CAM_SMMU_MAP_RW,
  29. CAM_SMMU_MAP_INVALID
  30. };
  31. enum cam_smmu_region_id {
  32. CAM_SMMU_REGION_FIRMWARE,
  33. CAM_SMMU_REGION_SHARED,
  34. CAM_SMMU_REGION_SCRATCH,
  35. CAM_SMMU_REGION_IO,
  36. CAM_SMMU_REGION_SECHEAP,
  37. CAM_SMMU_REGION_QDSS
  38. };
  39. /**
  40. * @brief : Callback function type that gets called back on cam
  41. * smmu page fault.
  42. *
  43. * @param domain : Iommu domain received in iommu page fault handler
  44. * @param dev : Device received in iommu page fault handler
  45. * @param iova : IOVA where page fault occurred
  46. * @param flags : Flags received in iommu page fault handler
  47. * @param token : Userdata given during callback registration
  48. * @param buf_info : Closest mapped buffer info
  49. */
  50. typedef void (*cam_smmu_client_page_fault_handler)(struct iommu_domain *domain,
  51. struct device *dev, unsigned long iova, int flags, void *token,
  52. uint32_t buf_info);
  53. /**
  54. * @brief : Structure to store region information
  55. *
  56. * @param iova_start : Start address of region
  57. * @param iova_len : length of region
  58. * @param discard_iova_start : iova addr start from where should not be used
  59. * @param discard_iova_len : length of discard iova region
  60. */
  61. struct cam_smmu_region_info {
  62. dma_addr_t iova_start;
  63. size_t iova_len;
  64. dma_addr_t discard_iova_start;
  65. size_t discard_iova_len;
  66. };
  67. /**
  68. * @brief : Gets an smmu handle
  69. *
  70. * @param identifier: Unique identifier to be used by clients which they
  71. * should get from device tree. CAM SMMU driver will
  72. * not enforce how this string is obtained and will
  73. * only validate this against the list of permitted
  74. * identifiers
  75. * @param handle_ptr: Based on the indentifier, CAM SMMU drivier will
  76. * fill the handle pointed by handle_ptr
  77. * @return Status of operation. Negative in case of error. Zero otherwise.
  78. */
  79. int cam_smmu_get_handle(char *identifier, int *handle_ptr);
  80. /**
  81. * @brief : Performs IOMMU operations
  82. *
  83. * @param handle: Handle to identify the CAM SMMU client (VFE, CPP, FD etc.)
  84. * @param op : Operation to be performed. Can be either CAM_SMMU_ATTACH
  85. * or CAM_SMMU_DETACH
  86. *
  87. * @return Status of operation. Negative in case of error. Zero otherwise.
  88. */
  89. int cam_smmu_ops(int handle, enum cam_smmu_ops_param op);
  90. /**
  91. * @brief : Maps user space IOVA for calling driver
  92. *
  93. * @param handle: Handle to identify the CAM SMMU client (VFE, CPP, FD etc.)
  94. * @param ion_fd: ION handle identifying the memory buffer.
  95. * @param dis_delayed_unmap: Whether to disable Delayed Unmap feature
  96. * for this mapping
  97. * @dir : Mapping direction: which will traslate toDMA_BIDIRECTIONAL,
  98. * DMA_TO_DEVICE or DMA_FROM_DEVICE
  99. * @dma_addr : Pointer to physical address where mapped address will be
  100. * returned if region_id is CAM_SMMU_REGION_IO. If region_id is
  101. * CAM_SMMU_REGION_SHARED, dma_addr is used as an input parameter
  102. * which specifies the cpu virtual address to map.
  103. * @len_ptr : Length of buffer mapped returned by CAM SMMU driver.
  104. * @return Status of operation. Negative in case of error. Zero otherwise.
  105. */
  106. int cam_smmu_map_user_iova(int handle, int ion_fd, bool dis_delayed_unmap,
  107. enum cam_smmu_map_dir dir, dma_addr_t *dma_addr, size_t *len_ptr,
  108. enum cam_smmu_region_id region_id);
  109. /**
  110. * @brief : Maps kernel space IOVA for calling driver
  111. *
  112. * @param handle : Handle to identify the CAM SMMU client (VFE, CPP, FD etc.)
  113. * @param buf : dma_buf allocated for kernel usage in mem_mgr
  114. * @dir : Mapping direction: which will traslate toDMA_BIDIRECTIONAL,
  115. * DMA_TO_DEVICE or DMA_FROM_DEVICE
  116. * @dma_addr : Pointer to physical address where mapped address will be
  117. * returned if region_id is CAM_SMMU_REGION_IO. If region_id is
  118. * CAM_SMMU_REGION_SHARED, dma_addr is used as an input
  119. * parameter which specifies the cpu virtual address to map.
  120. * @len_ptr : Length of buffer mapped returned by CAM SMMU driver.
  121. * @return Status of operation. Negative in case of error. Zero otherwise.
  122. */
  123. int cam_smmu_map_kernel_iova(int handle,
  124. struct dma_buf *buf, enum cam_smmu_map_dir dir,
  125. dma_addr_t *dma_addr, size_t *len_ptr,
  126. enum cam_smmu_region_id region_id);
  127. /**
  128. * @brief : Unmaps user space IOVA for calling driver
  129. *
  130. * @param handle: Handle to identify the CAMSMMU client (VFE, CPP, FD etc.)
  131. * @param ion_fd: ION handle identifying the memory buffer.
  132. *
  133. * @return Status of operation. Negative in case of error. Zero otherwise.
  134. */
  135. int cam_smmu_unmap_user_iova(int handle,
  136. int ion_fd, enum cam_smmu_region_id region_id);
  137. /**
  138. * @brief : Unmaps kernel IOVA for calling driver
  139. *
  140. * @param handle: Handle to identify the CAMSMMU client (VFE, CPP, FD etc.)
  141. * @param buf : dma_buf allocated for the kernel
  142. *
  143. * @return Status of operation. Negative in case of error. Zero otherwise.
  144. */
  145. int cam_smmu_unmap_kernel_iova(int handle,
  146. struct dma_buf *buf, enum cam_smmu_region_id region_id);
  147. /**
  148. * @brief : Allocates a scratch buffer
  149. *
  150. * This function allocates a scratch virtual buffer of length virt_len in the
  151. * device virtual address space mapped to phys_len physically contiguous bytes
  152. * in that device's SMMU.
  153. *
  154. * virt_len and phys_len are expected to be aligned to PAGE_SIZE and with each
  155. * other, otherwise -EINVAL is returned.
  156. *
  157. * -EINVAL will be returned if virt_len is less than phys_len.
  158. *
  159. * Passing a too large phys_len might also cause failure if that much size is
  160. * not available for allocation in a physically contiguous way.
  161. *
  162. * @param handle : Handle to identify the CAMSMMU client (VFE, CPP, FD etc.)
  163. * @param dir : Direction of mapping which will translate to IOMMU_READ
  164. * IOMMU_WRITE or a bit mask of both.
  165. * @param paddr_ptr: Device virtual address that the client device will be
  166. * able to read from/write to
  167. * @param virt_len : Virtual length of the scratch buffer
  168. * @param phys_len : Physical length of the scratch buffer
  169. *
  170. * @return Status of operation. Negative in case of error. Zero otherwise.
  171. */
  172. int cam_smmu_get_scratch_iova(int handle,
  173. enum cam_smmu_map_dir dir,
  174. dma_addr_t *paddr_ptr,
  175. size_t virt_len,
  176. size_t phys_len);
  177. /**
  178. * @brief : Frees a scratch buffer
  179. *
  180. * This function frees a scratch buffer and releases the corresponding SMMU
  181. * mappings.
  182. *
  183. * @param handle : Handle to identify the CAMSMMU client (IFE, ICP, etc.)
  184. * @param paddr : Device virtual address of client's scratch buffer that
  185. * will be freed.
  186. *
  187. * @return Status of operation. Negative in case of error. Zero otherwise.
  188. */
  189. int cam_smmu_put_scratch_iova(int handle,
  190. dma_addr_t paddr);
  191. /**
  192. *@brief : Destroys an smmu handle
  193. *
  194. * @param handle: Handle to identify the CAM SMMU client (VFE, CPP, FD etc.)
  195. *
  196. * @return Status of operation. Negative in case of error. Zero otherwise.
  197. */
  198. int cam_smmu_destroy_handle(int handle);
  199. /**
  200. * @brief : Finds index by handle in the smmu client table
  201. *
  202. * @param handle: Handle to identify the CAM SMMU client (VFE, CPP, FD etc.)
  203. * @return Index of SMMU client. Nagative in case of error.
  204. */
  205. int cam_smmu_find_index_by_handle(int hdl);
  206. /**
  207. * @brief : Registers smmu fault handler for client
  208. *
  209. * @param handle: Handle to identify the CAM SMMU client (VFE, CPP, FD etc.)
  210. * @param handler_cb: It is triggered in IOMMU page fault
  211. * @param token: It is input param when trigger page fault handler
  212. */
  213. void cam_smmu_set_client_page_fault_handler(int handle,
  214. cam_smmu_client_page_fault_handler handler_cb, void *token);
  215. /**
  216. * @brief : Unregisters smmu fault handler for client
  217. *
  218. * @param handle: Handle to identify the CAM SMMU client (VFE, CPP, FD etc.)
  219. * @param token: It is input param when trigger page fault handler
  220. */
  221. void cam_smmu_unset_client_page_fault_handler(int handle, void *token);
  222. /**
  223. * @brief Maps memory from an ION fd into IOVA space
  224. *
  225. * @param handle: SMMU handle identifying the context bank to map to
  226. * @param ion_fd: ION fd of memory to map to
  227. * @param paddr_ptr: Pointer IOVA address that will be returned
  228. * @param len_ptr: Length of memory mapped
  229. *
  230. * @return Status of operation. Negative in case of error. Zero otherwise.
  231. */
  232. int cam_smmu_get_iova(int handle, int ion_fd,
  233. dma_addr_t *paddr_ptr, size_t *len_ptr);
  234. /**
  235. * @brief Maps memory from an ION fd into IOVA space
  236. *
  237. * @param handle: SMMU handle identifying the secure context bank to map to
  238. * @param ion_fd: ION fd of memory to map to
  239. * @param paddr_ptr: Pointer IOVA address that will be returned
  240. * @param len_ptr: Length of memory mapped
  241. *
  242. * @return Status of operation. Negative in case of error. Zero otherwise.
  243. */
  244. int cam_smmu_get_stage2_iova(int handle, int ion_fd,
  245. dma_addr_t *paddr_ptr, size_t *len_ptr);
  246. /**
  247. * @brief Unmaps memory from context bank
  248. *
  249. * @param handle: SMMU handle identifying the context bank
  250. * @param ion_fd: ION fd of memory to unmap
  251. *
  252. * @return Status of operation. Negative in case of error. Zero otherwise.
  253. */
  254. int cam_smmu_put_iova(int handle, int ion_fd);
  255. /**
  256. * @brief Maps secure memory for SMMU handle
  257. *
  258. * @param handle: SMMU handle identifying secure context bank
  259. * @param ion_fd: ION fd to map securely
  260. * @param dir: DMA Direction for the mapping
  261. * @param dma_addr: Returned IOVA address after mapping
  262. * @param len_ptr: Length of memory mapped
  263. *
  264. * @return Status of operation. Negative in case of error. Zero otherwise.
  265. */
  266. int cam_smmu_map_stage2_iova(int handle,
  267. int ion_fd, enum cam_smmu_map_dir dir, dma_addr_t *dma_addr,
  268. size_t *len_ptr);
  269. /**
  270. * @brief Unmaps secure memopry for SMMU handle
  271. *
  272. * @param handle: SMMU handle identifying secure context bank
  273. * @param ion_fd: ION fd to unmap
  274. *
  275. * @return Status of operation. Negative in case of error. Zero otherwise.
  276. */
  277. int cam_smmu_unmap_stage2_iova(int handle, int ion_fd);
  278. /**
  279. * @brief Allocates firmware for context bank
  280. *
  281. * @param smmu_hdl: SMMU handle identifying context bank
  282. * @param iova: IOVA address of allocated firmware
  283. * @param kvaddr: CPU mapped address of allocated firmware
  284. * @param len: Length of allocated firmware memory
  285. *
  286. * @return Status of operation. Negative in case of error. Zero otherwise.
  287. */
  288. int cam_smmu_alloc_firmware(int32_t smmu_hdl,
  289. dma_addr_t *iova,
  290. uintptr_t *kvaddr,
  291. size_t *len);
  292. /**
  293. * @brief Deallocates firmware memory for context bank
  294. *
  295. * @param smmu_hdl: SMMU handle identifying the context bank
  296. *
  297. * @return Status of operation. Negative in case of error. Zero otherwise.
  298. */
  299. int cam_smmu_dealloc_firmware(int32_t smmu_hdl);
  300. /**
  301. * @brief Gets region information specified by smmu handle and region id
  302. *
  303. * @param smmu_hdl: SMMU handle identifying the context bank
  304. * @param region_id: Region id for which information is desired
  305. * @param region_info: Struct populated with region information
  306. *
  307. * @return Status of operation. Negative in case of error. Zero otherwise.
  308. */
  309. int cam_smmu_get_region_info(int32_t smmu_hdl,
  310. enum cam_smmu_region_id region_id,
  311. struct cam_smmu_region_info *region_info);
  312. /**
  313. * @brief Reserves secondary heap
  314. *
  315. * @param smmu_hdl: SMMU handle identifying the context bank
  316. * @param iova: IOVA of secondary heap after reservation has completed
  317. * @param buf: Allocated dma_buf for secondary heap
  318. * @param request_len: Length of secondary heap after reservation has completed
  319. *
  320. * @return Status of operation. Negative in case of error. Zero otherwise.
  321. */
  322. int cam_smmu_reserve_sec_heap(int32_t smmu_hdl,
  323. struct dma_buf *buf,
  324. dma_addr_t *iova,
  325. size_t *request_len);
  326. /**
  327. * @brief Releases secondary heap
  328. *
  329. * @param smmu_hdl: SMMU handle identifying the context bank
  330. *
  331. * @return Status of operation. Negative in case of error. Zero otherwise.
  332. */
  333. int cam_smmu_release_sec_heap(int32_t smmu_hdl);
  334. /**
  335. * @brief Allocates qdss for context bank
  336. *
  337. * @param smmu_hdl: SMMU handle identifying context bank
  338. * @param iova: IOVA address of allocated qdss
  339. * @param len: Length of allocated qdss memory
  340. *
  341. * @return Status of operation. Negative in case of error. Zero otherwise.
  342. */
  343. int cam_smmu_alloc_qdss(int32_t smmu_hdl,
  344. dma_addr_t *iova,
  345. size_t *len);
  346. /**
  347. * @brief Deallocates qdss memory for context bank
  348. *
  349. * @param smmu_hdl: SMMU handle identifying the context bank
  350. *
  351. * @return Status of operation. Negative in case of error. Zero otherwise.
  352. */
  353. int cam_smmu_dealloc_qdss(int32_t smmu_hdl);
  354. /**
  355. * @brief Get start addr & len of I/O region for a given cb
  356. *
  357. * @param smmu_hdl: SMMU handle identifying the context bank
  358. * @param iova: IOVA address of allocated I/O region
  359. * @param len: Length of allocated I/O memory
  360. * @param discard_iova_start: Start address of io space to discard
  361. * @param discard_iova_len: Length of io space to discard
  362. *
  363. * @return Status of operation. Negative in case of error. Zero otherwise.
  364. */
  365. int cam_smmu_get_io_region_info(int32_t smmu_hdl,
  366. dma_addr_t *iova, size_t *len,
  367. dma_addr_t *discard_iova_start, size_t *discard_iova_len);
  368. /**
  369. * @brief : API to register SMMU hw to platform framework.
  370. * @return struct platform_device pointer on on success, or ERR_PTR() on error.
  371. */
  372. int cam_smmu_init_module(void);
  373. /**
  374. * @brief : API to remove SMMU Hw from platform framework.
  375. */
  376. void cam_smmu_exit_module(void);
  377. #endif /* _CAM_SMMU_API_H_ */