amdgpu_prime.c 6.7 KB

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