amdgpu_prime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright 2012 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * based on nouveau_prime.c
  23. *
  24. * Authors: Alex Deucher
  25. */
  26. /**
  27. * DOC: PRIME Buffer Sharing
  28. *
  29. * The following callback implementations are used for :ref:`sharing GEM buffer
  30. * objects between different devices via PRIME <prime_buffer_sharing>`.
  31. */
  32. #include <drm/drmP.h>
  33. #include "amdgpu.h"
  34. #include "amdgpu_display.h"
  35. #include <drm/amdgpu_drm.h>
  36. #include <linux/dma-buf.h>
  37. static const struct dma_buf_ops amdgpu_dmabuf_ops;
  38. /**
  39. * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
  40. * implementation
  41. * @obj: GEM buffer object
  42. *
  43. * Returns:
  44. * A scatter/gather table for the pinned pages of the buffer object's memory.
  45. */
  46. struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
  47. {
  48. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  49. int npages = bo->tbo.num_pages;
  50. return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
  51. }
  52. /**
  53. * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
  54. * @obj: GEM buffer object
  55. *
  56. * Sets up an in-kernel virtual mapping of the buffer object's memory.
  57. *
  58. * Returns:
  59. * The virtual address of the mapping or an error pointer.
  60. */
  61. void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
  62. {
  63. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  64. int ret;
  65. ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
  66. &bo->dma_buf_vmap);
  67. if (ret)
  68. return ERR_PTR(ret);
  69. return bo->dma_buf_vmap.virtual;
  70. }
  71. /**
  72. * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
  73. * @obj: GEM buffer object
  74. * @vaddr: virtual address (unused)
  75. *
  76. * Tears down the in-kernel virtual mapping of the buffer object's memory.
  77. */
  78. void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  79. {
  80. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  81. ttm_bo_kunmap(&bo->dma_buf_vmap);
  82. }
  83. /**
  84. * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
  85. * @obj: GEM buffer object
  86. * @vma: virtual memory area
  87. *
  88. * Sets up a userspace mapping of the buffer object's memory in the given
  89. * virtual memory area.
  90. *
  91. * Returns:
  92. * 0 on success or negative error code.
  93. */
  94. int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  95. {
  96. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  97. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  98. unsigned asize = amdgpu_bo_size(bo);
  99. int ret;
  100. if (!vma->vm_file)
  101. return -ENODEV;
  102. if (adev == NULL)
  103. return -ENODEV;
  104. /* Check for valid size. */
  105. if (asize < vma->vm_end - vma->vm_start)
  106. return -EINVAL;
  107. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
  108. (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
  109. return -EPERM;
  110. }
  111. vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
  112. /* prime mmap does not need to check access, so allow here */
  113. ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
  114. if (ret)
  115. return ret;
  116. ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
  117. drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
  118. return ret;
  119. }
  120. /**
  121. * amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
  122. * implementation
  123. * @dev: DRM device
  124. * @attach: DMA-buf attachment
  125. * @sg: Scatter/gather table
  126. *
  127. * Import shared DMA buffer memory exported by another device.
  128. *
  129. * Returns:
  130. * A new GEM buffer object of the given DRM device, representing the memory
  131. * described by the given DMA-buf attachment and scatter/gather table.
  132. */
  133. struct drm_gem_object *
  134. amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
  135. struct dma_buf_attachment *attach,
  136. struct sg_table *sg)
  137. {
  138. struct reservation_object *resv = attach->dmabuf->resv;
  139. struct amdgpu_device *adev = dev->dev_private;
  140. struct amdgpu_bo *bo;
  141. struct amdgpu_bo_param bp;
  142. int ret;
  143. memset(&bp, 0, sizeof(bp));
  144. bp.size = attach->dmabuf->size;
  145. bp.byte_align = PAGE_SIZE;
  146. bp.domain = AMDGPU_GEM_DOMAIN_CPU;
  147. bp.flags = 0;
  148. bp.type = ttm_bo_type_sg;
  149. bp.resv = resv;
  150. ww_mutex_lock(&resv->lock, NULL);
  151. ret = amdgpu_bo_create(adev, &bp, &bo);
  152. if (ret)
  153. goto error;
  154. bo->tbo.sg = sg;
  155. bo->tbo.ttm->sg = sg;
  156. bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
  157. bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
  158. if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
  159. bo->prime_shared_count = 1;
  160. ww_mutex_unlock(&resv->lock);
  161. return &bo->gem_base;
  162. error:
  163. ww_mutex_unlock(&resv->lock);
  164. return ERR_PTR(ret);
  165. }
  166. /**
  167. * amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
  168. * @dma_buf: shared DMA buffer
  169. * @target_dev: target device
  170. * @attach: DMA-buf attachment
  171. *
  172. * Makes sure that the shared DMA buffer can be accessed by the target device.
  173. * For now, simply pins it to the GTT domain, where it should be accessible by
  174. * all DMA devices.
  175. *
  176. * Returns:
  177. * 0 on success or negative error code.
  178. */
  179. static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
  180. struct device *target_dev,
  181. struct dma_buf_attachment *attach)
  182. {
  183. struct drm_gem_object *obj = dma_buf->priv;
  184. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  185. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  186. long r;
  187. r = drm_gem_map_attach(dma_buf, target_dev, attach);
  188. if (r)
  189. return r;
  190. r = amdgpu_bo_reserve(bo, false);
  191. if (unlikely(r != 0))
  192. goto error_detach;
  193. if (attach->dev->driver != adev->dev->driver) {
  194. /*
  195. * Wait for all shared fences to complete before we switch to future
  196. * use of exclusive fence on this prime shared bo.
  197. */
  198. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  199. true, false,
  200. MAX_SCHEDULE_TIMEOUT);
  201. if (unlikely(r < 0)) {
  202. DRM_DEBUG_PRIME("Fence wait failed: %li\n", r);
  203. goto error_unreserve;
  204. }
  205. }
  206. /* pin buffer into GTT */
  207. r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT, NULL);
  208. if (r)
  209. goto error_unreserve;
  210. if (attach->dev->driver != adev->dev->driver)
  211. bo->prime_shared_count++;
  212. error_unreserve:
  213. amdgpu_bo_unreserve(bo);
  214. error_detach:
  215. if (r)
  216. drm_gem_map_detach(dma_buf, attach);
  217. return r;
  218. }
  219. /**
  220. * amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
  221. * @dma_buf: shared DMA buffer
  222. * @attach: DMA-buf attachment
  223. *
  224. * This is called when a shared DMA buffer no longer needs to be accessible by
  225. * the other device. For now, simply unpins the buffer from GTT.
  226. */
  227. static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
  228. struct dma_buf_attachment *attach)
  229. {
  230. struct drm_gem_object *obj = dma_buf->priv;
  231. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  232. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  233. int ret = 0;
  234. ret = amdgpu_bo_reserve(bo, true);
  235. if (unlikely(ret != 0))
  236. goto error;
  237. amdgpu_bo_unpin(bo);
  238. if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
  239. bo->prime_shared_count--;
  240. amdgpu_bo_unreserve(bo);
  241. error:
  242. drm_gem_map_detach(dma_buf, attach);
  243. }
  244. /**
  245. * amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
  246. * @obj: GEM buffer object
  247. *
  248. * Returns:
  249. * The buffer object's reservation object.
  250. */
  251. struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
  252. {
  253. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  254. return bo->tbo.resv;
  255. }
  256. /**
  257. * amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
  258. * @dma_buf: shared DMA buffer
  259. * @direction: direction of DMA transfer
  260. *
  261. * This is called before CPU access to the shared DMA buffer's memory. If it's
  262. * a read access, the buffer is moved to the GTT domain if possible, for optimal
  263. * CPU read performance.
  264. *
  265. * Returns:
  266. * 0 on success or negative error code.
  267. */
  268. static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
  269. enum dma_data_direction direction)
  270. {
  271. struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
  272. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  273. struct ttm_operation_ctx ctx = { true, false };
  274. u32 domain = amdgpu_display_supported_domains(adev);
  275. int ret;
  276. bool reads = (direction == DMA_BIDIRECTIONAL ||
  277. direction == DMA_FROM_DEVICE);
  278. if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
  279. return 0;
  280. /* move to gtt */
  281. ret = amdgpu_bo_reserve(bo, false);
  282. if (unlikely(ret != 0))
  283. return ret;
  284. if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
  285. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  286. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  287. }
  288. amdgpu_bo_unreserve(bo);
  289. return ret;
  290. }
  291. static const struct dma_buf_ops amdgpu_dmabuf_ops = {
  292. .attach = amdgpu_gem_map_attach,
  293. .detach = amdgpu_gem_map_detach,
  294. .map_dma_buf = drm_gem_map_dma_buf,
  295. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  296. .release = drm_gem_dmabuf_release,
  297. .begin_cpu_access = amdgpu_gem_begin_cpu_access,
  298. .map = drm_gem_dmabuf_kmap,
  299. .map_atomic = drm_gem_dmabuf_kmap_atomic,
  300. .unmap = drm_gem_dmabuf_kunmap,
  301. .unmap_atomic = drm_gem_dmabuf_kunmap_atomic,
  302. .mmap = drm_gem_dmabuf_mmap,
  303. .vmap = drm_gem_dmabuf_vmap,
  304. .vunmap = drm_gem_dmabuf_vunmap,
  305. };
  306. /**
  307. * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
  308. * @dev: DRM device
  309. * @gobj: GEM buffer object
  310. * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  311. *
  312. * The main work is done by the &drm_gem_prime_export helper, which in turn
  313. * uses &amdgpu_gem_prime_res_obj.
  314. *
  315. * Returns:
  316. * Shared DMA buffer representing the GEM buffer object from the given device.
  317. */
  318. struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
  319. struct drm_gem_object *gobj,
  320. int flags)
  321. {
  322. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  323. struct dma_buf *buf;
  324. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
  325. bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
  326. return ERR_PTR(-EPERM);
  327. buf = drm_gem_prime_export(dev, gobj, flags);
  328. if (!IS_ERR(buf)) {
  329. buf->file->f_mapping = dev->anon_inode->i_mapping;
  330. buf->ops = &amdgpu_dmabuf_ops;
  331. }
  332. return buf;
  333. }
  334. /**
  335. * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
  336. * @dev: DRM device
  337. * @dma_buf: Shared DMA buffer
  338. *
  339. * The main work is done by the &drm_gem_prime_import helper, which in turn
  340. * uses &amdgpu_gem_prime_import_sg_table.
  341. *
  342. * Returns:
  343. * GEM buffer object representing the shared DMA buffer for the given device.
  344. */
  345. struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
  346. struct dma_buf *dma_buf)
  347. {
  348. struct drm_gem_object *obj;
  349. if (dma_buf->ops == &amdgpu_dmabuf_ops) {
  350. obj = dma_buf->priv;
  351. if (obj->dev == dev) {
  352. /*
  353. * Importing dmabuf exported from out own gem increases
  354. * refcount on gem itself instead of f_count of dmabuf.
  355. */
  356. drm_gem_object_get(obj);
  357. return obj;
  358. }
  359. }
  360. return drm_gem_prime_import(dev, dma_buf);
  361. }