amdgpu_prime.c 11 KB

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