2015-04-21 04:55:21 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* based on nouveau_prime.c
|
|
|
|
*
|
|
|
|
* Authors: Alex Deucher
|
|
|
|
*/
|
2018-05-30 00:33:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DOC: PRIME Buffer Sharing
|
|
|
|
*
|
|
|
|
* The following callback implementations are used for :ref:`sharing GEM buffer
|
|
|
|
* objects between different devices via PRIME <prime_buffer_sharing>`.
|
|
|
|
*/
|
|
|
|
|
2015-04-21 04:55:21 +08:00
|
|
|
#include <drm/drmP.h>
|
|
|
|
|
|
|
|
#include "amdgpu.h"
|
2017-12-09 05:18:59 +08:00
|
|
|
#include "amdgpu_display.h"
|
2018-08-14 00:41:35 +08:00
|
|
|
#include "amdgpu_gem.h"
|
2015-04-21 04:55:21 +08:00
|
|
|
#include <drm/amdgpu_drm.h>
|
|
|
|
#include <linux/dma-buf.h>
|
|
|
|
|
2018-02-19 18:29:35 +08:00
|
|
|
static const struct dma_buf_ops amdgpu_dmabuf_ops;
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
|
|
|
|
* implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @obj: GEM buffer object (BO)
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* A scatter/gather table for the pinned pages of the BO's memory.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2015-04-21 04:55:21 +08:00
|
|
|
struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
|
|
|
|
int npages = bo->tbo.num_pages;
|
|
|
|
|
|
|
|
return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @obj: GEM BO
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
2018-09-14 02:47:39 +08:00
|
|
|
* Sets up an in-kernel virtual mapping of the BO's memory.
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* The virtual address of the mapping or an error pointer.
|
|
|
|
*/
|
2015-04-21 04:55:21 +08:00
|
|
|
void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
|
|
|
|
&bo->dma_buf_vmap);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
return bo->dma_buf_vmap.virtual;
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @obj: GEM BO
|
|
|
|
* @vaddr: Virtual address (unused)
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
2018-09-14 02:47:39 +08:00
|
|
|
* Tears down the in-kernel virtual mapping of the BO's memory.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2015-04-21 04:55:21 +08:00
|
|
|
void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
|
|
|
|
{
|
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
|
|
|
|
|
|
|
|
ttm_bo_kunmap(&bo->dma_buf_vmap);
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @obj: GEM BO
|
|
|
|
* @vma: Virtual memory area
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
2018-09-14 02:47:39 +08:00
|
|
|
* Sets up a userspace mapping of the BO's memory in the given
|
2018-05-30 00:33:41 +08:00
|
|
|
* virtual memory area.
|
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* 0 on success or a negative error code on failure.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2017-08-23 03:25:33 +08:00
|
|
|
int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
|
|
|
|
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
|
|
|
|
unsigned asize = amdgpu_bo_size(bo);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!vma->vm_file)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (adev == NULL)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
/* Check for valid size. */
|
|
|
|
if (asize < vma->vm_end - vma->vm_start)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
|
|
|
|
(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
|
|
|
|
|
|
|
|
/* prime mmap does not need to check access, so allow here */
|
|
|
|
ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
|
|
|
|
drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
|
|
|
|
* implementation
|
|
|
|
* @dev: DRM device
|
|
|
|
* @attach: DMA-buf attachment
|
|
|
|
* @sg: Scatter/gather table
|
|
|
|
*
|
2018-09-14 02:47:39 +08:00
|
|
|
* Imports shared DMA buffer memory exported by another device.
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* A new GEM BO of the given DRM device, representing the memory
|
2018-05-30 00:33:41 +08:00
|
|
|
* described by the given DMA-buf attachment and scatter/gather table.
|
|
|
|
*/
|
2016-05-04 00:46:19 +08:00
|
|
|
struct drm_gem_object *
|
|
|
|
amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
|
|
|
|
struct dma_buf_attachment *attach,
|
|
|
|
struct sg_table *sg)
|
2015-04-21 04:55:21 +08:00
|
|
|
{
|
2015-09-03 23:34:59 +08:00
|
|
|
struct reservation_object *resv = attach->dmabuf->resv;
|
2015-04-21 04:55:21 +08:00
|
|
|
struct amdgpu_device *adev = dev->dev_private;
|
|
|
|
struct amdgpu_bo *bo;
|
2018-04-16 18:27:50 +08:00
|
|
|
struct amdgpu_bo_param bp;
|
2015-04-21 04:55:21 +08:00
|
|
|
int ret;
|
|
|
|
|
2018-04-16 18:27:50 +08:00
|
|
|
memset(&bp, 0, sizeof(bp));
|
|
|
|
bp.size = attach->dmabuf->size;
|
|
|
|
bp.byte_align = PAGE_SIZE;
|
|
|
|
bp.domain = AMDGPU_GEM_DOMAIN_CPU;
|
|
|
|
bp.flags = 0;
|
|
|
|
bp.type = ttm_bo_type_sg;
|
|
|
|
bp.resv = resv;
|
2015-09-03 23:34:59 +08:00
|
|
|
ww_mutex_lock(&resv->lock, NULL);
|
2018-04-16 18:27:50 +08:00
|
|
|
ret = amdgpu_bo_create(adev, &bp, &bo);
|
2015-04-21 04:55:21 +08:00
|
|
|
if (ret)
|
2018-02-21 02:51:02 +08:00
|
|
|
goto error;
|
|
|
|
|
2018-03-15 03:48:17 +08:00
|
|
|
bo->tbo.sg = sg;
|
|
|
|
bo->tbo.ttm->sg = sg;
|
2018-02-21 02:42:40 +08:00
|
|
|
bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
|
|
|
|
bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
|
2018-02-21 02:51:02 +08:00
|
|
|
if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
|
|
|
|
bo->prime_shared_count = 1;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2018-02-21 02:51:02 +08:00
|
|
|
ww_mutex_unlock(&resv->lock);
|
2015-04-21 04:55:21 +08:00
|
|
|
return &bo->gem_base;
|
2018-02-21 02:51:02 +08:00
|
|
|
|
|
|
|
error:
|
|
|
|
ww_mutex_unlock(&resv->lock);
|
|
|
|
return ERR_PTR(ret);
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @dma_buf: Shared DMA buffer
|
2018-05-30 00:33:41 +08:00
|
|
|
* @attach: DMA-buf attachment
|
|
|
|
*
|
|
|
|
* Makes sure that the shared DMA buffer can be accessed by the target device.
|
|
|
|
* For now, simply pins it to the GTT domain, where it should be accessible by
|
|
|
|
* all DMA devices.
|
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* 0 on success or a negative error code on failure.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2018-02-16 20:16:11 +08:00
|
|
|
static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
|
|
|
|
struct dma_buf_attachment *attach)
|
2015-04-21 04:55:21 +08:00
|
|
|
{
|
2018-02-16 20:16:11 +08:00
|
|
|
struct drm_gem_object *obj = dma_buf->priv;
|
2015-04-21 04:55:21 +08:00
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
|
2018-03-21 20:58:05 +08:00
|
|
|
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
|
2018-02-16 20:16:11 +08:00
|
|
|
long r;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2018-05-28 17:47:52 +08:00
|
|
|
r = drm_gem_map_attach(dma_buf, attach);
|
2018-02-16 20:16:11 +08:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = amdgpu_bo_reserve(bo, false);
|
|
|
|
if (unlikely(r != 0))
|
|
|
|
goto error_detach;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2018-02-19 18:29:35 +08:00
|
|
|
|
2018-03-21 20:58:05 +08:00
|
|
|
if (attach->dev->driver != adev->dev->driver) {
|
2018-02-19 18:29:35 +08:00
|
|
|
/*
|
|
|
|
* Wait for all shared fences to complete before we switch to future
|
|
|
|
* use of exclusive fence on this prime shared bo.
|
|
|
|
*/
|
|
|
|
r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
|
|
|
|
true, false,
|
|
|
|
MAX_SCHEDULE_TIMEOUT);
|
|
|
|
if (unlikely(r < 0)) {
|
|
|
|
DRM_DEBUG_PRIME("Fence wait failed: %li\n", r);
|
|
|
|
goto error_unreserve;
|
|
|
|
}
|
drm/amdgpu: Attach exclusive fence to prime exported bo's. (v5)
External clients which import our bo's wait only
for exclusive dmabuf-fences, not on shared ones,
ditto for bo's which we import from external
providers and write to.
Therefore attach exclusive fences on prime shared buffers
if our exported buffer gets imported by an external
client, or if we import a buffer from an external
exporter.
See discussion in thread:
https://lists.freedesktop.org/archives/dri-devel/2016-October/122370.html
Prime export tested on Intel iGPU + AMD Tonga dGPU as
DRI3/Present Prime render offload, and with the Tonga
standalone as primary gpu.
v2: Add a wait for all shared fences before prime export,
as suggested by Christian Koenig.
v3: - Mark buffer prime_exported in amdgpu_gem_prime_pin,
so we only use the exclusive fence when exporting a
bo to external clients like a separate iGPU, but not
when exporting/importing from/to ourselves as part of
regular DRI3 fd passing.
- Propagate failure of reservation_object_wait_rcu back
to caller.
v4: - Switch to a prime_shared_count counter instead of a
flag, which gets in/decremented on prime_pin/unpin, so
we can switch back to shared fences if all clients
detach from our exported bo.
- Also switch to exclusive fence for prime imported bo's.
v5: - Drop lret, instead use int ret -> long ret, as proposed
by Christian.
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=95472
Tested-by: Mike Lothian <mike@fireburn.co.uk> (v1)
Signed-off-by: Mario Kleiner <mario.kleiner.de@gmail.com>
Reviewed-by: Christian König <christian.koenig@amd.com>.
Cc: Christian König <christian.koenig@amd.com>
Cc: Michel Dänzer <michel.daenzer@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
Cc: stable@vger.kernel.org
2016-11-09 09:25:15 +08:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:55:21 +08:00
|
|
|
/* pin buffer into GTT */
|
2018-06-25 12:51:14 +08:00
|
|
|
r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
|
2018-02-19 18:29:35 +08:00
|
|
|
if (r)
|
|
|
|
goto error_unreserve;
|
|
|
|
|
2018-03-21 20:58:05 +08:00
|
|
|
if (attach->dev->driver != adev->dev->driver)
|
drm/amdgpu: Attach exclusive fence to prime exported bo's. (v5)
External clients which import our bo's wait only
for exclusive dmabuf-fences, not on shared ones,
ditto for bo's which we import from external
providers and write to.
Therefore attach exclusive fences on prime shared buffers
if our exported buffer gets imported by an external
client, or if we import a buffer from an external
exporter.
See discussion in thread:
https://lists.freedesktop.org/archives/dri-devel/2016-October/122370.html
Prime export tested on Intel iGPU + AMD Tonga dGPU as
DRI3/Present Prime render offload, and with the Tonga
standalone as primary gpu.
v2: Add a wait for all shared fences before prime export,
as suggested by Christian Koenig.
v3: - Mark buffer prime_exported in amdgpu_gem_prime_pin,
so we only use the exclusive fence when exporting a
bo to external clients like a separate iGPU, but not
when exporting/importing from/to ourselves as part of
regular DRI3 fd passing.
- Propagate failure of reservation_object_wait_rcu back
to caller.
v4: - Switch to a prime_shared_count counter instead of a
flag, which gets in/decremented on prime_pin/unpin, so
we can switch back to shared fences if all clients
detach from our exported bo.
- Also switch to exclusive fence for prime imported bo's.
v5: - Drop lret, instead use int ret -> long ret, as proposed
by Christian.
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=95472
Tested-by: Mike Lothian <mike@fireburn.co.uk> (v1)
Signed-off-by: Mario Kleiner <mario.kleiner.de@gmail.com>
Reviewed-by: Christian König <christian.koenig@amd.com>.
Cc: Christian König <christian.koenig@amd.com>
Cc: Michel Dänzer <michel.daenzer@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
Cc: stable@vger.kernel.org
2016-11-09 09:25:15 +08:00
|
|
|
bo->prime_shared_count++;
|
|
|
|
|
2018-02-16 20:16:11 +08:00
|
|
|
error_unreserve:
|
2015-04-21 04:55:21 +08:00
|
|
|
amdgpu_bo_unreserve(bo);
|
2018-02-16 20:16:11 +08:00
|
|
|
|
|
|
|
error_detach:
|
|
|
|
if (r)
|
|
|
|
drm_gem_map_detach(dma_buf, attach);
|
|
|
|
return r;
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @dma_buf: Shared DMA buffer
|
2018-05-30 00:33:41 +08:00
|
|
|
* @attach: DMA-buf attachment
|
|
|
|
*
|
|
|
|
* This is called when a shared DMA buffer no longer needs to be accessible by
|
2018-09-14 02:47:39 +08:00
|
|
|
* another device. For now, simply unpins the buffer from GTT.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2018-02-16 20:16:11 +08:00
|
|
|
static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
|
|
|
|
struct dma_buf_attachment *attach)
|
2015-04-21 04:55:21 +08:00
|
|
|
{
|
2018-02-16 20:16:11 +08:00
|
|
|
struct drm_gem_object *obj = dma_buf->priv;
|
2015-04-21 04:55:21 +08:00
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
|
2018-03-21 20:58:05 +08:00
|
|
|
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
|
2015-04-21 04:55:21 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2017-04-28 16:28:14 +08:00
|
|
|
ret = amdgpu_bo_reserve(bo, true);
|
2015-04-21 04:55:21 +08:00
|
|
|
if (unlikely(ret != 0))
|
2018-02-16 20:16:11 +08:00
|
|
|
goto error;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
|
|
|
amdgpu_bo_unpin(bo);
|
2018-03-21 20:58:05 +08:00
|
|
|
if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
|
drm/amdgpu: Attach exclusive fence to prime exported bo's. (v5)
External clients which import our bo's wait only
for exclusive dmabuf-fences, not on shared ones,
ditto for bo's which we import from external
providers and write to.
Therefore attach exclusive fences on prime shared buffers
if our exported buffer gets imported by an external
client, or if we import a buffer from an external
exporter.
See discussion in thread:
https://lists.freedesktop.org/archives/dri-devel/2016-October/122370.html
Prime export tested on Intel iGPU + AMD Tonga dGPU as
DRI3/Present Prime render offload, and with the Tonga
standalone as primary gpu.
v2: Add a wait for all shared fences before prime export,
as suggested by Christian Koenig.
v3: - Mark buffer prime_exported in amdgpu_gem_prime_pin,
so we only use the exclusive fence when exporting a
bo to external clients like a separate iGPU, but not
when exporting/importing from/to ourselves as part of
regular DRI3 fd passing.
- Propagate failure of reservation_object_wait_rcu back
to caller.
v4: - Switch to a prime_shared_count counter instead of a
flag, which gets in/decremented on prime_pin/unpin, so
we can switch back to shared fences if all clients
detach from our exported bo.
- Also switch to exclusive fence for prime imported bo's.
v5: - Drop lret, instead use int ret -> long ret, as proposed
by Christian.
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=95472
Tested-by: Mike Lothian <mike@fireburn.co.uk> (v1)
Signed-off-by: Mario Kleiner <mario.kleiner.de@gmail.com>
Reviewed-by: Christian König <christian.koenig@amd.com>.
Cc: Christian König <christian.koenig@amd.com>
Cc: Michel Dänzer <michel.daenzer@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
Cc: stable@vger.kernel.org
2016-11-09 09:25:15 +08:00
|
|
|
bo->prime_shared_count--;
|
2015-04-21 04:55:21 +08:00
|
|
|
amdgpu_bo_unreserve(bo);
|
2018-02-16 20:16:11 +08:00
|
|
|
|
|
|
|
error:
|
|
|
|
drm_gem_map_detach(dma_buf, attach);
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @obj: GEM BO
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* The BO's reservation object.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2015-04-21 04:55:21 +08:00
|
|
|
struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
|
|
|
|
|
|
|
|
return bo->tbo.resv;
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
|
2018-09-14 02:47:39 +08:00
|
|
|
* @dma_buf: Shared DMA buffer
|
|
|
|
* @direction: Direction of DMA transfer
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
|
|
|
* This is called before CPU access to the shared DMA buffer's memory. If it's
|
|
|
|
* a read access, the buffer is moved to the GTT domain if possible, for optimal
|
|
|
|
* CPU read performance.
|
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* 0 on success or a negative error code on failure.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2017-12-09 05:18:59 +08:00
|
|
|
static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
|
|
|
|
enum dma_data_direction direction)
|
|
|
|
{
|
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
|
|
|
|
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
|
|
|
|
struct ttm_operation_ctx ctx = { true, false };
|
2018-04-19 03:06:02 +08:00
|
|
|
u32 domain = amdgpu_display_supported_domains(adev);
|
2017-12-09 05:18:59 +08:00
|
|
|
int ret;
|
|
|
|
bool reads = (direction == DMA_BIDIRECTIONAL ||
|
|
|
|
direction == DMA_FROM_DEVICE);
|
|
|
|
|
|
|
|
if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* move to gtt */
|
|
|
|
ret = amdgpu_bo_reserve(bo, false);
|
|
|
|
if (unlikely(ret != 0))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
|
2018-07-16 22:12:24 +08:00
|
|
|
amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
|
2017-12-09 05:18:59 +08:00
|
|
|
ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
amdgpu_bo_unreserve(bo);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dma_buf_ops amdgpu_dmabuf_ops = {
|
2018-02-16 20:16:11 +08:00
|
|
|
.attach = amdgpu_gem_map_attach,
|
|
|
|
.detach = amdgpu_gem_map_detach,
|
2017-12-09 05:18:59 +08:00
|
|
|
.map_dma_buf = drm_gem_map_dma_buf,
|
|
|
|
.unmap_dma_buf = drm_gem_unmap_dma_buf,
|
|
|
|
.release = drm_gem_dmabuf_release,
|
|
|
|
.begin_cpu_access = amdgpu_gem_begin_cpu_access,
|
|
|
|
.mmap = drm_gem_dmabuf_mmap,
|
|
|
|
.vmap = drm_gem_dmabuf_vmap,
|
|
|
|
.vunmap = drm_gem_dmabuf_vunmap,
|
|
|
|
};
|
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
|
|
|
|
* @dev: DRM device
|
2018-09-14 02:47:39 +08:00
|
|
|
* @gobj: GEM BO
|
|
|
|
* @flags: Flags such as DRM_CLOEXEC and DRM_RDWR.
|
2018-05-30 00:33:41 +08:00
|
|
|
*
|
|
|
|
* The main work is done by the &drm_gem_prime_export helper, which in turn
|
|
|
|
* uses &amdgpu_gem_prime_res_obj.
|
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* Shared DMA buffer representing the GEM BO from the given device.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2015-04-21 04:55:21 +08:00
|
|
|
struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
|
|
|
|
struct drm_gem_object *gobj,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
|
2017-11-14 00:20:50 +08:00
|
|
|
struct dma_buf *buf;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2017-08-25 15:14:43 +08:00
|
|
|
if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
|
|
|
|
bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
|
2015-04-21 04:55:21 +08:00
|
|
|
return ERR_PTR(-EPERM);
|
|
|
|
|
2017-11-14 00:20:50 +08:00
|
|
|
buf = drm_gem_prime_export(dev, gobj, flags);
|
2017-12-09 05:18:59 +08:00
|
|
|
if (!IS_ERR(buf)) {
|
2017-11-14 00:20:50 +08:00
|
|
|
buf->file->f_mapping = dev->anon_inode->i_mapping;
|
2017-12-09 05:18:59 +08:00
|
|
|
buf->ops = &amdgpu_dmabuf_ops;
|
|
|
|
}
|
|
|
|
|
2017-11-14 00:20:50 +08:00
|
|
|
return buf;
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
2017-12-09 05:18:59 +08:00
|
|
|
|
2018-05-30 00:33:41 +08:00
|
|
|
/**
|
|
|
|
* amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
|
|
|
|
* @dev: DRM device
|
|
|
|
* @dma_buf: Shared DMA buffer
|
|
|
|
*
|
|
|
|
* The main work is done by the &drm_gem_prime_import helper, which in turn
|
|
|
|
* uses &amdgpu_gem_prime_import_sg_table.
|
|
|
|
*
|
|
|
|
* Returns:
|
2018-09-14 02:47:39 +08:00
|
|
|
* GEM BO representing the shared DMA buffer for the given device.
|
2018-05-30 00:33:41 +08:00
|
|
|
*/
|
2017-12-09 05:18:59 +08:00
|
|
|
struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
|
|
|
|
struct dma_buf *dma_buf)
|
|
|
|
{
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
|
|
|
|
if (dma_buf->ops == &amdgpu_dmabuf_ops) {
|
|
|
|
obj = dma_buf->priv;
|
|
|
|
if (obj->dev == dev) {
|
|
|
|
/*
|
|
|
|
* Importing dmabuf exported from out own gem increases
|
|
|
|
* refcount on gem itself instead of f_count of dmabuf.
|
|
|
|
*/
|
|
|
|
drm_gem_object_get(obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return drm_gem_prime_import(dev, dma_buf);
|
|
|
|
}
|