2 * Copyright 2012 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * based on nouveau_prime.c
24 * Authors: Alex Deucher
28 * DOC: PRIME Buffer Sharing
30 * The following callback implementations are used for :ref:`sharing GEM buffer
31 * objects between different devices via PRIME <prime_buffer_sharing>`.
37 #include "amdgpu_display.h"
38 #include "amdgpu_gem.h"
39 #include <drm/amdgpu_drm.h>
40 #include <linux/dma-buf.h>
41 #include <linux/dma-fence-array.h>
44 * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
46 * @obj: GEM buffer object (BO)
49 * A scatter/gather table for the pinned pages of the BO's memory.
51 struct sg_table
*amdgpu_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
53 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(obj
);
54 int npages
= bo
->tbo
.num_pages
;
56 return drm_prime_pages_to_sg(bo
->tbo
.ttm
->pages
, npages
);
60 * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
63 * Sets up an in-kernel virtual mapping of the BO's memory.
66 * The virtual address of the mapping or an error pointer.
68 void *amdgpu_gem_prime_vmap(struct drm_gem_object
*obj
)
70 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(obj
);
73 ret
= ttm_bo_kmap(&bo
->tbo
, 0, bo
->tbo
.num_pages
,
78 return bo
->dma_buf_vmap
.virtual;
82 * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
84 * @vaddr: Virtual address (unused)
86 * Tears down the in-kernel virtual mapping of the BO's memory.
88 void amdgpu_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
90 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(obj
);
92 ttm_bo_kunmap(&bo
->dma_buf_vmap
);
96 * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
98 * @vma: Virtual memory area
100 * Sets up a userspace mapping of the BO's memory in the given
101 * virtual memory area.
104 * 0 on success or a negative error code on failure.
106 int amdgpu_gem_prime_mmap(struct drm_gem_object
*obj
, struct vm_area_struct
*vma
)
108 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(obj
);
109 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->tbo
.bdev
);
110 unsigned asize
= amdgpu_bo_size(bo
);
119 /* Check for valid size. */
120 if (asize
< vma
->vm_end
- vma
->vm_start
)
123 if (amdgpu_ttm_tt_get_usermm(bo
->tbo
.ttm
) ||
124 (bo
->flags
& AMDGPU_GEM_CREATE_NO_CPU_ACCESS
)) {
127 vma
->vm_pgoff
+= amdgpu_bo_mmap_offset(bo
) >> PAGE_SHIFT
;
129 /* prime mmap does not need to check access, so allow here */
130 ret
= drm_vma_node_allow(&obj
->vma_node
, vma
->vm_file
->private_data
);
134 ret
= ttm_bo_mmap(vma
->vm_file
, vma
, &adev
->mman
.bdev
);
135 drm_vma_node_revoke(&obj
->vma_node
, vma
->vm_file
->private_data
);
141 * amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
144 * @attach: DMA-buf attachment
145 * @sg: Scatter/gather table
147 * Imports shared DMA buffer memory exported by another device.
150 * A new GEM BO of the given DRM device, representing the memory
151 * described by the given DMA-buf attachment and scatter/gather table.
153 struct drm_gem_object
*
154 amdgpu_gem_prime_import_sg_table(struct drm_device
*dev
,
155 struct dma_buf_attachment
*attach
,
158 struct reservation_object
*resv
= attach
->dmabuf
->resv
;
159 struct amdgpu_device
*adev
= dev
->dev_private
;
160 struct amdgpu_bo
*bo
;
161 struct amdgpu_bo_param bp
;
164 memset(&bp
, 0, sizeof(bp
));
165 bp
.size
= attach
->dmabuf
->size
;
166 bp
.byte_align
= PAGE_SIZE
;
167 bp
.domain
= AMDGPU_GEM_DOMAIN_CPU
;
169 bp
.type
= ttm_bo_type_sg
;
171 ww_mutex_lock(&resv
->lock
, NULL
);
172 ret
= amdgpu_bo_create(adev
, &bp
, &bo
);
177 bo
->tbo
.ttm
->sg
= sg
;
178 bo
->allowed_domains
= AMDGPU_GEM_DOMAIN_GTT
;
179 bo
->preferred_domains
= AMDGPU_GEM_DOMAIN_GTT
;
180 if (attach
->dmabuf
->ops
!= &amdgpu_dmabuf_ops
)
181 bo
->prime_shared_count
= 1;
183 ww_mutex_unlock(&resv
->lock
);
184 return &bo
->gem_base
;
187 ww_mutex_unlock(&resv
->lock
);
192 __reservation_object_make_exclusive(struct reservation_object
*obj
)
194 struct dma_fence
**fences
;
198 if (!reservation_object_get_list(obj
)) /* no shared fences to convert */
201 r
= reservation_object_get_fences_rcu(obj
, NULL
, &count
, &fences
);
206 /* Now that was unexpected. */
207 } else if (count
== 1) {
208 reservation_object_add_excl_fence(obj
, fences
[0]);
209 dma_fence_put(fences
[0]);
212 struct dma_fence_array
*array
;
214 array
= dma_fence_array_create(count
, fences
,
215 dma_fence_context_alloc(1), 0,
220 reservation_object_add_excl_fence(obj
, &array
->base
);
221 dma_fence_put(&array
->base
);
228 dma_fence_put(fences
[count
]);
234 * amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
235 * @dma_buf: Shared DMA buffer
236 * @attach: DMA-buf attachment
238 * Makes sure that the shared DMA buffer can be accessed by the target device.
239 * For now, simply pins it to the GTT domain, where it should be accessible by
243 * 0 on success or a negative error code on failure.
245 static int amdgpu_gem_map_attach(struct dma_buf
*dma_buf
,
246 struct dma_buf_attachment
*attach
)
248 struct drm_gem_object
*obj
= dma_buf
->priv
;
249 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(obj
);
250 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->tbo
.bdev
);
253 r
= drm_gem_map_attach(dma_buf
, attach
);
257 r
= amdgpu_bo_reserve(bo
, false);
258 if (unlikely(r
!= 0))
262 if (attach
->dev
->driver
!= adev
->dev
->driver
) {
264 * We only create shared fences for internal use, but importers
265 * of the dmabuf rely on exclusive fences for implicitly
266 * tracking write hazards. As any of the current fences may
267 * correspond to a write, we need to convert all existing
268 * fences on the reservation object into a single exclusive
271 r
= __reservation_object_make_exclusive(bo
->tbo
.resv
);
273 goto error_unreserve
;
276 /* pin buffer into GTT */
277 r
= amdgpu_bo_pin(bo
, AMDGPU_GEM_DOMAIN_GTT
);
279 goto error_unreserve
;
281 if (attach
->dev
->driver
!= adev
->dev
->driver
)
282 bo
->prime_shared_count
++;
285 amdgpu_bo_unreserve(bo
);
289 drm_gem_map_detach(dma_buf
, attach
);
294 * amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
295 * @dma_buf: Shared DMA buffer
296 * @attach: DMA-buf attachment
298 * This is called when a shared DMA buffer no longer needs to be accessible by
299 * another device. For now, simply unpins the buffer from GTT.
301 static void amdgpu_gem_map_detach(struct dma_buf
*dma_buf
,
302 struct dma_buf_attachment
*attach
)
304 struct drm_gem_object
*obj
= dma_buf
->priv
;
305 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(obj
);
306 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->tbo
.bdev
);
309 ret
= amdgpu_bo_reserve(bo
, true);
310 if (unlikely(ret
!= 0))
314 if (attach
->dev
->driver
!= adev
->dev
->driver
&& bo
->prime_shared_count
)
315 bo
->prime_shared_count
--;
316 amdgpu_bo_unreserve(bo
);
319 drm_gem_map_detach(dma_buf
, attach
);
323 * amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
327 * The BO's reservation object.
329 struct reservation_object
*amdgpu_gem_prime_res_obj(struct drm_gem_object
*obj
)
331 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(obj
);
337 * amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
338 * @dma_buf: Shared DMA buffer
339 * @direction: Direction of DMA transfer
341 * This is called before CPU access to the shared DMA buffer's memory. If it's
342 * a read access, the buffer is moved to the GTT domain if possible, for optimal
343 * CPU read performance.
346 * 0 on success or a negative error code on failure.
348 static int amdgpu_gem_begin_cpu_access(struct dma_buf
*dma_buf
,
349 enum dma_data_direction direction
)
351 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(dma_buf
->priv
);
352 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->tbo
.bdev
);
353 struct ttm_operation_ctx ctx
= { true, false };
354 u32 domain
= amdgpu_display_supported_domains(adev
);
356 bool reads
= (direction
== DMA_BIDIRECTIONAL
||
357 direction
== DMA_FROM_DEVICE
);
359 if (!reads
|| !(domain
& AMDGPU_GEM_DOMAIN_GTT
))
363 ret
= amdgpu_bo_reserve(bo
, false);
364 if (unlikely(ret
!= 0))
367 if (!bo
->pin_count
&& (bo
->allowed_domains
& AMDGPU_GEM_DOMAIN_GTT
)) {
368 amdgpu_bo_placement_from_domain(bo
, AMDGPU_GEM_DOMAIN_GTT
);
369 ret
= ttm_bo_validate(&bo
->tbo
, &bo
->placement
, &ctx
);
372 amdgpu_bo_unreserve(bo
);
376 const struct dma_buf_ops amdgpu_dmabuf_ops
= {
377 .attach
= amdgpu_gem_map_attach
,
378 .detach
= amdgpu_gem_map_detach
,
379 .map_dma_buf
= drm_gem_map_dma_buf
,
380 .unmap_dma_buf
= drm_gem_unmap_dma_buf
,
381 .release
= drm_gem_dmabuf_release
,
382 .begin_cpu_access
= amdgpu_gem_begin_cpu_access
,
383 .mmap
= drm_gem_dmabuf_mmap
,
384 .vmap
= drm_gem_dmabuf_vmap
,
385 .vunmap
= drm_gem_dmabuf_vunmap
,
389 * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
392 * @flags: Flags such as DRM_CLOEXEC and DRM_RDWR.
394 * The main work is done by the &drm_gem_prime_export helper, which in turn
395 * uses &amdgpu_gem_prime_res_obj.
398 * Shared DMA buffer representing the GEM BO from the given device.
400 struct dma_buf
*amdgpu_gem_prime_export(struct drm_device
*dev
,
401 struct drm_gem_object
*gobj
,
404 struct amdgpu_bo
*bo
= gem_to_amdgpu_bo(gobj
);
407 if (amdgpu_ttm_tt_get_usermm(bo
->tbo
.ttm
) ||
408 bo
->flags
& AMDGPU_GEM_CREATE_VM_ALWAYS_VALID
)
409 return ERR_PTR(-EPERM
);
411 buf
= drm_gem_prime_export(dev
, gobj
, flags
);
413 buf
->file
->f_mapping
= dev
->anon_inode
->i_mapping
;
414 buf
->ops
= &amdgpu_dmabuf_ops
;
421 * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
423 * @dma_buf: Shared DMA buffer
425 * The main work is done by the &drm_gem_prime_import helper, which in turn
426 * uses &amdgpu_gem_prime_import_sg_table.
429 * GEM BO representing the shared DMA buffer for the given device.
431 struct drm_gem_object
*amdgpu_gem_prime_import(struct drm_device
*dev
,
432 struct dma_buf
*dma_buf
)
434 struct drm_gem_object
*obj
;
436 if (dma_buf
->ops
== &amdgpu_dmabuf_ops
) {
438 if (obj
->dev
== dev
) {
440 * Importing dmabuf exported from out own gem increases
441 * refcount on gem itself instead of f_count of dmabuf.
443 drm_gem_object_get(obj
);
448 return drm_gem_prime_import(dev
, dma_buf
);