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 "amdgpu_gem.h"
  36. #include <drm/amdgpu_drm.h>
  37. #include <linux/dma-buf.h>
  38. static const struct dma_buf_ops amdgpu_dmabuf_ops;
  39. /**
  40. * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
  41. * implementation
  42. * @obj: GEM buffer object (BO)
  43. *
  44. * Returns:
  45. * A scatter/gather table for the pinned pages of the BO's memory.
  46. */
  47. struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
  48. {
  49. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  50. int npages = bo->tbo.num_pages;
  51. return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
  52. }
  53. /**
  54. * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
  55. * @obj: GEM BO
  56. *
  57. * Sets up an in-kernel virtual mapping of the BO's memory.
  58. *
  59. * Returns:
  60. * The virtual address of the mapping or an error pointer.
  61. */
  62. void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
  63. {
  64. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  65. int ret;
  66. ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
  67. &bo->dma_buf_vmap);
  68. if (ret)
  69. return ERR_PTR(ret);
  70. return bo->dma_buf_vmap.virtual;
  71. }
  72. /**
  73. * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
  74. * @obj: GEM BO
  75. * @vaddr: Virtual address (unused)
  76. *
  77. * Tears down the in-kernel virtual mapping of the BO's memory.
  78. */
  79. void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  80. {
  81. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  82. ttm_bo_kunmap(&bo->dma_buf_vmap);
  83. }
  84. /**
  85. * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
  86. * @obj: GEM BO
  87. * @vma: Virtual memory area
  88. *
  89. * Sets up a userspace mapping of the BO's memory in the given
  90. * virtual memory area.
  91. *
  92. * Returns:
  93. * 0 on success or a negative error code on failure.
  94. */
  95. int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  96. {
  97. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  98. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  99. unsigned asize = amdgpu_bo_size(bo);
  100. int ret;
  101. if (!vma->vm_file)
  102. return -ENODEV;
  103. if (adev == NULL)
  104. return -ENODEV;
  105. /* Check for valid size. */
  106. if (asize < vma->vm_end - vma->vm_start)
  107. return -EINVAL;
  108. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
  109. (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
  110. return -EPERM;
  111. }
  112. vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
  113. /* prime mmap does not need to check access, so allow here */
  114. ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
  115. if (ret)
  116. return ret;
  117. ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
  118. drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
  119. return ret;
  120. }
  121. /**
  122. * amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
  123. * implementation
  124. * @dev: DRM device
  125. * @attach: DMA-buf attachment
  126. * @sg: Scatter/gather table
  127. *
  128. * Imports shared DMA buffer memory exported by another device.
  129. *
  130. * Returns:
  131. * A new GEM BO of the given DRM device, representing the memory
  132. * described by the given DMA-buf attachment and scatter/gather table.
  133. */
  134. struct drm_gem_object *
  135. amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
  136. struct dma_buf_attachment *attach,
  137. struct sg_table *sg)
  138. {
  139. struct reservation_object *resv = attach->dmabuf->resv;
  140. struct amdgpu_device *adev = dev->dev_private;
  141. struct amdgpu_bo *bo;
  142. struct amdgpu_bo_param bp;
  143. int ret;
  144. memset(&bp, 0, sizeof(bp));
  145. bp.size = attach->dmabuf->size;
  146. bp.byte_align = PAGE_SIZE;
  147. bp.domain = AMDGPU_GEM_DOMAIN_CPU;
  148. bp.flags = 0;
  149. bp.type = ttm_bo_type_sg;
  150. bp.resv = resv;
  151. ww_mutex_lock(&resv->lock, NULL);
  152. ret = amdgpu_bo_create(adev, &bp, &bo);
  153. if (ret)
  154. goto error;
  155. bo->tbo.sg = sg;
  156. bo->tbo.ttm->sg = sg;
  157. bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
  158. bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
  159. if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
  160. bo->prime_shared_count = 1;
  161. ww_mutex_unlock(&resv->lock);
  162. return &bo->gem_base;
  163. error:
  164. ww_mutex_unlock(&resv->lock);
  165. return ERR_PTR(ret);
  166. }
  167. /**
  168. * amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
  169. * @dma_buf: Shared DMA buffer
  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 a negative error code on failure.
  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);
  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. * another 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 BO
  246. *
  247. * Returns:
  248. * The BO'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 a negative error code on failure.
  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_bo_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 BO
  307. * @flags: Flags such as 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 BO 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 BO 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. }