amdgpu_prime.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include <drm/drmP.h>
  27. #include "amdgpu.h"
  28. #include "amdgpu_display.h"
  29. #include <drm/amdgpu_drm.h>
  30. #include <linux/dma-buf.h>
  31. static const struct dma_buf_ops amdgpu_dmabuf_ops;
  32. struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
  33. {
  34. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  35. int npages = bo->tbo.num_pages;
  36. return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
  37. }
  38. void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
  39. {
  40. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  41. int ret;
  42. ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
  43. &bo->dma_buf_vmap);
  44. if (ret)
  45. return ERR_PTR(ret);
  46. return bo->dma_buf_vmap.virtual;
  47. }
  48. void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  49. {
  50. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  51. ttm_bo_kunmap(&bo->dma_buf_vmap);
  52. }
  53. int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  54. {
  55. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  56. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  57. unsigned asize = amdgpu_bo_size(bo);
  58. int ret;
  59. if (!vma->vm_file)
  60. return -ENODEV;
  61. if (adev == NULL)
  62. return -ENODEV;
  63. /* Check for valid size. */
  64. if (asize < vma->vm_end - vma->vm_start)
  65. return -EINVAL;
  66. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
  67. (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
  68. return -EPERM;
  69. }
  70. vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
  71. /* prime mmap does not need to check access, so allow here */
  72. ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
  73. if (ret)
  74. return ret;
  75. ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
  76. drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
  77. return ret;
  78. }
  79. struct drm_gem_object *
  80. amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
  81. struct dma_buf_attachment *attach,
  82. struct sg_table *sg)
  83. {
  84. struct reservation_object *resv = attach->dmabuf->resv;
  85. struct amdgpu_device *adev = dev->dev_private;
  86. struct amdgpu_bo *bo;
  87. struct amdgpu_bo_param bp;
  88. int ret;
  89. memset(&bp, 0, sizeof(bp));
  90. bp.size = attach->dmabuf->size;
  91. bp.byte_align = PAGE_SIZE;
  92. bp.domain = AMDGPU_GEM_DOMAIN_CPU;
  93. bp.flags = 0;
  94. bp.type = ttm_bo_type_sg;
  95. bp.resv = resv;
  96. ww_mutex_lock(&resv->lock, NULL);
  97. ret = amdgpu_bo_create(adev, &bp, &bo);
  98. if (ret)
  99. goto error;
  100. bo->tbo.sg = sg;
  101. bo->tbo.ttm->sg = sg;
  102. bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
  103. bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
  104. if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
  105. bo->prime_shared_count = 1;
  106. ww_mutex_unlock(&resv->lock);
  107. return &bo->gem_base;
  108. error:
  109. ww_mutex_unlock(&resv->lock);
  110. return ERR_PTR(ret);
  111. }
  112. static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
  113. struct device *target_dev,
  114. struct dma_buf_attachment *attach)
  115. {
  116. struct drm_gem_object *obj = dma_buf->priv;
  117. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  118. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  119. long r;
  120. r = drm_gem_map_attach(dma_buf, target_dev, attach);
  121. if (r)
  122. return r;
  123. r = amdgpu_bo_reserve(bo, false);
  124. if (unlikely(r != 0))
  125. goto error_detach;
  126. if (attach->dev->driver != adev->dev->driver) {
  127. /*
  128. * Wait for all shared fences to complete before we switch to future
  129. * use of exclusive fence on this prime shared bo.
  130. */
  131. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  132. true, false,
  133. MAX_SCHEDULE_TIMEOUT);
  134. if (unlikely(r < 0)) {
  135. DRM_DEBUG_PRIME("Fence wait failed: %li\n", r);
  136. goto error_unreserve;
  137. }
  138. }
  139. /* pin buffer into GTT */
  140. r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT, NULL);
  141. if (r)
  142. goto error_unreserve;
  143. if (attach->dev->driver != adev->dev->driver)
  144. bo->prime_shared_count++;
  145. error_unreserve:
  146. amdgpu_bo_unreserve(bo);
  147. error_detach:
  148. if (r)
  149. drm_gem_map_detach(dma_buf, attach);
  150. return r;
  151. }
  152. static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
  153. struct dma_buf_attachment *attach)
  154. {
  155. struct drm_gem_object *obj = dma_buf->priv;
  156. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  157. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  158. int ret = 0;
  159. ret = amdgpu_bo_reserve(bo, true);
  160. if (unlikely(ret != 0))
  161. goto error;
  162. amdgpu_bo_unpin(bo);
  163. if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
  164. bo->prime_shared_count--;
  165. amdgpu_bo_unreserve(bo);
  166. error:
  167. drm_gem_map_detach(dma_buf, attach);
  168. }
  169. struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
  170. {
  171. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  172. return bo->tbo.resv;
  173. }
  174. static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
  175. enum dma_data_direction direction)
  176. {
  177. struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
  178. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  179. struct ttm_operation_ctx ctx = { true, false };
  180. u32 domain = amdgpu_display_supported_domains(adev);
  181. int ret;
  182. bool reads = (direction == DMA_BIDIRECTIONAL ||
  183. direction == DMA_FROM_DEVICE);
  184. if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
  185. return 0;
  186. /* move to gtt */
  187. ret = amdgpu_bo_reserve(bo, false);
  188. if (unlikely(ret != 0))
  189. return ret;
  190. if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
  191. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  192. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  193. }
  194. amdgpu_bo_unreserve(bo);
  195. return ret;
  196. }
  197. static const struct dma_buf_ops amdgpu_dmabuf_ops = {
  198. .attach = amdgpu_gem_map_attach,
  199. .detach = amdgpu_gem_map_detach,
  200. .map_dma_buf = drm_gem_map_dma_buf,
  201. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  202. .release = drm_gem_dmabuf_release,
  203. .begin_cpu_access = amdgpu_gem_begin_cpu_access,
  204. .map = drm_gem_dmabuf_kmap,
  205. .map_atomic = drm_gem_dmabuf_kmap_atomic,
  206. .unmap = drm_gem_dmabuf_kunmap,
  207. .unmap_atomic = drm_gem_dmabuf_kunmap_atomic,
  208. .mmap = drm_gem_dmabuf_mmap,
  209. .vmap = drm_gem_dmabuf_vmap,
  210. .vunmap = drm_gem_dmabuf_vunmap,
  211. };
  212. struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
  213. struct drm_gem_object *gobj,
  214. int flags)
  215. {
  216. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  217. struct dma_buf *buf;
  218. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
  219. bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
  220. return ERR_PTR(-EPERM);
  221. buf = drm_gem_prime_export(dev, gobj, flags);
  222. if (!IS_ERR(buf)) {
  223. buf->file->f_mapping = dev->anon_inode->i_mapping;
  224. buf->ops = &amdgpu_dmabuf_ops;
  225. }
  226. return buf;
  227. }
  228. struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
  229. struct dma_buf *dma_buf)
  230. {
  231. struct drm_gem_object *obj;
  232. if (dma_buf->ops == &amdgpu_dmabuf_ops) {
  233. obj = dma_buf->priv;
  234. if (obj->dev == dev) {
  235. /*
  236. * Importing dmabuf exported from out own gem increases
  237. * refcount on gem itself instead of f_count of dmabuf.
  238. */
  239. drm_gem_object_get(obj);
  240. return obj;
  241. }
  242. }
  243. return drm_gem_prime_import(dev, dma_buf);
  244. }