amdgpu_prime.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #include <linux/dma-fence-array.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
  43. *
  44. * Returns:
  45. * A scatter/gather table for the pinned pages of the buffer object'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 buffer object
  56. *
  57. * Sets up an in-kernel virtual mapping of the buffer object'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 buffer object
  75. * @vaddr: virtual address (unused)
  76. *
  77. * Tears down the in-kernel virtual mapping of the buffer object'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 buffer object
  87. * @vma: virtual memory area
  88. *
  89. * Sets up a userspace mapping of the buffer object's memory in the given
  90. * virtual memory area.
  91. *
  92. * Returns:
  93. * 0 on success or negative error code.
  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. * Import shared DMA buffer memory exported by another device.
  129. *
  130. * Returns:
  131. * A new GEM buffer object 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. static int
  168. __reservation_object_make_exclusive(struct reservation_object *obj)
  169. {
  170. struct dma_fence **fences;
  171. unsigned int count;
  172. int r;
  173. if (!reservation_object_get_list(obj)) /* no shared fences to convert */
  174. return 0;
  175. r = reservation_object_get_fences_rcu(obj, NULL, &count, &fences);
  176. if (r)
  177. return r;
  178. if (count == 0) {
  179. /* Now that was unexpected. */
  180. } else if (count == 1) {
  181. reservation_object_add_excl_fence(obj, fences[0]);
  182. dma_fence_put(fences[0]);
  183. kfree(fences);
  184. } else {
  185. struct dma_fence_array *array;
  186. array = dma_fence_array_create(count, fences,
  187. dma_fence_context_alloc(1), 0,
  188. false);
  189. if (!array)
  190. goto err_fences_put;
  191. reservation_object_add_excl_fence(obj, &array->base);
  192. dma_fence_put(&array->base);
  193. }
  194. return 0;
  195. err_fences_put:
  196. while (count--)
  197. dma_fence_put(fences[count]);
  198. kfree(fences);
  199. return -ENOMEM;
  200. }
  201. /**
  202. * amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
  203. * @dma_buf: shared DMA buffer
  204. * @attach: DMA-buf attachment
  205. *
  206. * Makes sure that the shared DMA buffer can be accessed by the target device.
  207. * For now, simply pins it to the GTT domain, where it should be accessible by
  208. * all DMA devices.
  209. *
  210. * Returns:
  211. * 0 on success or negative error code.
  212. */
  213. static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
  214. struct dma_buf_attachment *attach)
  215. {
  216. struct drm_gem_object *obj = dma_buf->priv;
  217. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  218. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  219. long r;
  220. r = drm_gem_map_attach(dma_buf, attach);
  221. if (r)
  222. return r;
  223. r = amdgpu_bo_reserve(bo, false);
  224. if (unlikely(r != 0))
  225. goto error_detach;
  226. if (attach->dev->driver != adev->dev->driver) {
  227. /*
  228. * We only create shared fences for internal use, but importers
  229. * of the dmabuf rely on exclusive fences for implicitly
  230. * tracking write hazards. As any of the current fences may
  231. * correspond to a write, we need to convert all existing
  232. * fences on the reservation object into a single exclusive
  233. * fence.
  234. */
  235. r = __reservation_object_make_exclusive(bo->tbo.resv);
  236. if (r)
  237. goto error_unreserve;
  238. }
  239. /* pin buffer into GTT */
  240. r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
  241. if (r)
  242. goto error_unreserve;
  243. if (attach->dev->driver != adev->dev->driver)
  244. bo->prime_shared_count++;
  245. error_unreserve:
  246. amdgpu_bo_unreserve(bo);
  247. error_detach:
  248. if (r)
  249. drm_gem_map_detach(dma_buf, attach);
  250. return r;
  251. }
  252. /**
  253. * amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
  254. * @dma_buf: shared DMA buffer
  255. * @attach: DMA-buf attachment
  256. *
  257. * This is called when a shared DMA buffer no longer needs to be accessible by
  258. * the other device. For now, simply unpins the buffer from GTT.
  259. */
  260. static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
  261. struct dma_buf_attachment *attach)
  262. {
  263. struct drm_gem_object *obj = dma_buf->priv;
  264. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  265. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  266. int ret = 0;
  267. ret = amdgpu_bo_reserve(bo, true);
  268. if (unlikely(ret != 0))
  269. goto error;
  270. amdgpu_bo_unpin(bo);
  271. if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
  272. bo->prime_shared_count--;
  273. amdgpu_bo_unreserve(bo);
  274. error:
  275. drm_gem_map_detach(dma_buf, attach);
  276. }
  277. /**
  278. * amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
  279. * @obj: GEM buffer object
  280. *
  281. * Returns:
  282. * The buffer object's reservation object.
  283. */
  284. struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
  285. {
  286. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  287. return bo->tbo.resv;
  288. }
  289. /**
  290. * amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
  291. * @dma_buf: shared DMA buffer
  292. * @direction: direction of DMA transfer
  293. *
  294. * This is called before CPU access to the shared DMA buffer's memory. If it's
  295. * a read access, the buffer is moved to the GTT domain if possible, for optimal
  296. * CPU read performance.
  297. *
  298. * Returns:
  299. * 0 on success or negative error code.
  300. */
  301. static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
  302. enum dma_data_direction direction)
  303. {
  304. struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
  305. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  306. struct ttm_operation_ctx ctx = { true, false };
  307. u32 domain = amdgpu_display_supported_domains(adev);
  308. int ret;
  309. bool reads = (direction == DMA_BIDIRECTIONAL ||
  310. direction == DMA_FROM_DEVICE);
  311. if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
  312. return 0;
  313. /* move to gtt */
  314. ret = amdgpu_bo_reserve(bo, false);
  315. if (unlikely(ret != 0))
  316. return ret;
  317. if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
  318. amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  319. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  320. }
  321. amdgpu_bo_unreserve(bo);
  322. return ret;
  323. }
  324. static const struct dma_buf_ops amdgpu_dmabuf_ops = {
  325. .attach = amdgpu_gem_map_attach,
  326. .detach = amdgpu_gem_map_detach,
  327. .map_dma_buf = drm_gem_map_dma_buf,
  328. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  329. .release = drm_gem_dmabuf_release,
  330. .begin_cpu_access = amdgpu_gem_begin_cpu_access,
  331. .map = drm_gem_dmabuf_kmap,
  332. .unmap = drm_gem_dmabuf_kunmap,
  333. .mmap = drm_gem_dmabuf_mmap,
  334. .vmap = drm_gem_dmabuf_vmap,
  335. .vunmap = drm_gem_dmabuf_vunmap,
  336. };
  337. /**
  338. * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
  339. * @dev: DRM device
  340. * @gobj: GEM buffer object
  341. * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  342. *
  343. * The main work is done by the &drm_gem_prime_export helper, which in turn
  344. * uses &amdgpu_gem_prime_res_obj.
  345. *
  346. * Returns:
  347. * Shared DMA buffer representing the GEM buffer object from the given device.
  348. */
  349. struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
  350. struct drm_gem_object *gobj,
  351. int flags)
  352. {
  353. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  354. struct dma_buf *buf;
  355. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
  356. bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
  357. return ERR_PTR(-EPERM);
  358. buf = drm_gem_prime_export(dev, gobj, flags);
  359. if (!IS_ERR(buf)) {
  360. buf->file->f_mapping = dev->anon_inode->i_mapping;
  361. buf->ops = &amdgpu_dmabuf_ops;
  362. }
  363. return buf;
  364. }
  365. /**
  366. * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
  367. * @dev: DRM device
  368. * @dma_buf: Shared DMA buffer
  369. *
  370. * The main work is done by the &drm_gem_prime_import helper, which in turn
  371. * uses &amdgpu_gem_prime_import_sg_table.
  372. *
  373. * Returns:
  374. * GEM buffer object representing the shared DMA buffer for the given device.
  375. */
  376. struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
  377. struct dma_buf *dma_buf)
  378. {
  379. struct drm_gem_object *obj;
  380. if (dma_buf->ops == &amdgpu_dmabuf_ops) {
  381. obj = dma_buf->priv;
  382. if (obj->dev == dev) {
  383. /*
  384. * Importing dmabuf exported from out own gem increases
  385. * refcount on gem itself instead of f_count of dmabuf.
  386. */
  387. drm_gem_object_get(obj);
  388. return obj;
  389. }
  390. }
  391. return drm_gem_prime_import(dev, dma_buf);
  392. }