amdgpu_prime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 dma_buf_attachment *attach)
  181. {
  182. struct drm_gem_object *obj = dma_buf->priv;
  183. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  184. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  185. long r;
  186. r = drm_gem_map_attach(dma_buf, attach);
  187. if (r)
  188. return r;
  189. r = amdgpu_bo_reserve(bo, false);
  190. if (unlikely(r != 0))
  191. goto error_detach;
  192. if (attach->dev->driver != adev->dev->driver) {
  193. /*
  194. * Wait for all shared fences to complete before we switch to future
  195. * use of exclusive fence on this prime shared bo.
  196. */
  197. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  198. true, false,
  199. MAX_SCHEDULE_TIMEOUT);
  200. if (unlikely(r < 0)) {
  201. DRM_DEBUG_PRIME("Fence wait failed: %li\n", r);
  202. goto error_unreserve;
  203. }
  204. }
  205. /* pin buffer into GTT */
  206. r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT, NULL);
  207. if (r)
  208. goto error_unreserve;
  209. if (attach->dev->driver != adev->dev->driver)
  210. bo->prime_shared_count++;
  211. error_unreserve:
  212. amdgpu_bo_unreserve(bo);
  213. error_detach:
  214. if (r)
  215. drm_gem_map_detach(dma_buf, attach);
  216. return r;
  217. }
  218. /**
  219. * amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
  220. * @dma_buf: shared DMA buffer
  221. * @attach: DMA-buf attachment
  222. *
  223. * This is called when a shared DMA buffer no longer needs to be accessible by
  224. * the other device. For now, simply unpins the buffer from GTT.
  225. */
  226. static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
  227. struct dma_buf_attachment *attach)
  228. {
  229. struct drm_gem_object *obj = dma_buf->priv;
  230. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  231. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  232. int ret = 0;
  233. ret = amdgpu_bo_reserve(bo, true);
  234. if (unlikely(ret != 0))
  235. goto error;
  236. amdgpu_bo_unpin(bo);
  237. if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
  238. bo->prime_shared_count--;
  239. amdgpu_bo_unreserve(bo);
  240. error:
  241. drm_gem_map_detach(dma_buf, attach);
  242. }
  243. /**
  244. * amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
  245. * @obj: GEM buffer object
  246. *
  247. * Returns:
  248. * The buffer object's reservation object.
  249. */
  250. struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
  251. {
  252. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  253. return bo->tbo.resv;
  254. }
  255. /**
  256. * amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
  257. * @dma_buf: shared DMA buffer
  258. * @direction: direction of DMA transfer
  259. *
  260. * This is called before CPU access to the shared DMA buffer's memory. If it's
  261. * a read access, the buffer is moved to the GTT domain if possible, for optimal
  262. * CPU read performance.
  263. *
  264. * Returns:
  265. * 0 on success or negative error code.
  266. */
  267. static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
  268. enum dma_data_direction direction)
  269. {
  270. struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
  271. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  272. struct ttm_operation_ctx ctx = { true, false };
  273. u32 domain = amdgpu_display_supported_domains(adev);
  274. int ret;
  275. bool reads = (direction == DMA_BIDIRECTIONAL ||
  276. direction == DMA_FROM_DEVICE);
  277. if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
  278. return 0;
  279. /* move to gtt */
  280. ret = amdgpu_bo_reserve(bo, false);
  281. if (unlikely(ret != 0))
  282. return ret;
  283. if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
  284. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  285. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  286. }
  287. amdgpu_bo_unreserve(bo);
  288. return ret;
  289. }
  290. static const struct dma_buf_ops amdgpu_dmabuf_ops = {
  291. .attach = amdgpu_gem_map_attach,
  292. .detach = amdgpu_gem_map_detach,
  293. .map_dma_buf = drm_gem_map_dma_buf,
  294. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  295. .release = drm_gem_dmabuf_release,
  296. .begin_cpu_access = amdgpu_gem_begin_cpu_access,
  297. .map = drm_gem_dmabuf_kmap,
  298. .unmap = drm_gem_dmabuf_kunmap,
  299. .mmap = drm_gem_dmabuf_mmap,
  300. .vmap = drm_gem_dmabuf_vmap,
  301. .vunmap = drm_gem_dmabuf_vunmap,
  302. };
  303. /**
  304. * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
  305. * @dev: DRM device
  306. * @gobj: GEM buffer object
  307. * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  308. *
  309. * The main work is done by the &drm_gem_prime_export helper, which in turn
  310. * uses &amdgpu_gem_prime_res_obj.
  311. *
  312. * Returns:
  313. * Shared DMA buffer representing the GEM buffer object from the given device.
  314. */
  315. struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
  316. struct drm_gem_object *gobj,
  317. int flags)
  318. {
  319. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  320. struct dma_buf *buf;
  321. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
  322. bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
  323. return ERR_PTR(-EPERM);
  324. buf = drm_gem_prime_export(dev, gobj, flags);
  325. if (!IS_ERR(buf)) {
  326. buf->file->f_mapping = dev->anon_inode->i_mapping;
  327. buf->ops = &amdgpu_dmabuf_ops;
  328. }
  329. return buf;
  330. }
  331. /**
  332. * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
  333. * @dev: DRM device
  334. * @dma_buf: Shared DMA buffer
  335. *
  336. * The main work is done by the &drm_gem_prime_import helper, which in turn
  337. * uses &amdgpu_gem_prime_import_sg_table.
  338. *
  339. * Returns:
  340. * GEM buffer object representing the shared DMA buffer for the given device.
  341. */
  342. struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
  343. struct dma_buf *dma_buf)
  344. {
  345. struct drm_gem_object *obj;
  346. if (dma_buf->ops == &amdgpu_dmabuf_ops) {
  347. obj = dma_buf->priv;
  348. if (obj->dev == dev) {
  349. /*
  350. * Importing dmabuf exported from out own gem increases
  351. * refcount on gem itself instead of f_count of dmabuf.
  352. */
  353. drm_gem_object_get(obj);
  354. return obj;
  355. }
  356. }
  357. return drm_gem_prime_import(dev, dma_buf);
  358. }