2012-05-10 21:25:09 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Red Hat 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 (including the next
|
|
|
|
* paragraph) 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 AUTHORS OR COPYRIGHT HOLDERS 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.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Dave Airlie <airlied@redhat.com>
|
|
|
|
*/
|
2016-08-04 23:32:42 +08:00
|
|
|
|
|
|
|
#include <linux/dma-buf.h>
|
|
|
|
#include <linux/reservation.h>
|
|
|
|
|
2012-10-03 01:01:07 +08:00
|
|
|
#include <drm/drmP.h>
|
2016-08-04 23:32:42 +08:00
|
|
|
|
2012-05-10 21:25:09 +08:00
|
|
|
#include "i915_drv.h"
|
|
|
|
|
2013-08-08 15:10:38 +08:00
|
|
|
static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
|
|
|
|
{
|
|
|
|
return to_intel_bo(buf->priv);
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:09:32 +08:00
|
|
|
static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
|
2012-06-01 22:20:22 +08:00
|
|
|
enum dma_data_direction dir)
|
2012-05-10 21:25:09 +08:00
|
|
|
{
|
2013-08-08 15:10:38 +08:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
|
2012-06-01 22:20:22 +08:00
|
|
|
struct sg_table *st;
|
|
|
|
struct scatterlist *src, *dst;
|
|
|
|
int ret, i;
|
2012-05-10 21:25:09 +08:00
|
|
|
|
2012-06-01 22:20:22 +08:00
|
|
|
ret = i915_mutex_lock_interruptible(obj->base.dev);
|
2012-05-10 21:25:09 +08:00
|
|
|
if (ret)
|
2013-08-27 06:50:55 +08:00
|
|
|
goto err;
|
2012-05-10 21:25:09 +08:00
|
|
|
|
2012-06-07 22:38:42 +08:00
|
|
|
ret = i915_gem_object_get_pages(obj);
|
2013-08-27 06:50:55 +08:00
|
|
|
if (ret)
|
|
|
|
goto err_unlock;
|
|
|
|
|
|
|
|
i915_gem_object_pin_pages(obj);
|
2012-06-01 22:20:22 +08:00
|
|
|
|
|
|
|
/* Copy sg so that we make an independent mapping */
|
|
|
|
st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
|
|
|
|
if (st == NULL) {
|
2013-08-27 06:50:55 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_unpin;
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
2012-06-01 22:20:22 +08:00
|
|
|
ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
|
2013-08-27 06:50:55 +08:00
|
|
|
if (ret)
|
|
|
|
goto err_free;
|
2012-06-01 22:20:22 +08:00
|
|
|
|
|
|
|
src = obj->pages->sgl;
|
|
|
|
dst = st->sgl;
|
|
|
|
for (i = 0; i < obj->pages->nents; i++) {
|
2013-02-19 01:28:02 +08:00
|
|
|
sg_set_page(dst, sg_page(src), src->length, 0);
|
2012-06-01 22:20:22 +08:00
|
|
|
dst = sg_next(dst);
|
|
|
|
src = sg_next(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
|
2013-08-27 06:50:55 +08:00
|
|
|
ret =-ENOMEM;
|
|
|
|
goto err_free_sg;
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
2012-06-01 22:20:22 +08:00
|
|
|
mutex_unlock(&obj->base.dev->struct_mutex);
|
|
|
|
return st;
|
2013-08-27 06:50:55 +08:00
|
|
|
|
|
|
|
err_free_sg:
|
|
|
|
sg_free_table(st);
|
|
|
|
err_free:
|
|
|
|
kfree(st);
|
|
|
|
err_unpin:
|
|
|
|
i915_gem_object_unpin_pages(obj);
|
|
|
|
err_unlock:
|
|
|
|
mutex_unlock(&obj->base.dev->struct_mutex);
|
|
|
|
err:
|
|
|
|
return ERR_PTR(ret);
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:09:32 +08:00
|
|
|
static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
|
2012-09-05 04:02:58 +08:00
|
|
|
struct sg_table *sg,
|
|
|
|
enum dma_data_direction dir)
|
2012-05-10 21:25:09 +08:00
|
|
|
{
|
2013-08-08 15:10:38 +08:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
|
2013-08-08 15:10:37 +08:00
|
|
|
|
2012-05-10 21:25:09 +08:00
|
|
|
dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
|
|
|
|
sg_free_table(sg);
|
|
|
|
kfree(sg);
|
2013-08-08 15:10:37 +08:00
|
|
|
|
2016-04-08 19:11:09 +08:00
|
|
|
mutex_lock(&obj->base.dev->struct_mutex);
|
2013-08-08 15:10:37 +08:00
|
|
|
i915_gem_object_unpin_pages(obj);
|
|
|
|
mutex_unlock(&obj->base.dev->struct_mutex);
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
2012-05-22 20:09:21 +08:00
|
|
|
static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
|
|
|
|
{
|
2013-08-08 15:10:38 +08:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
2012-05-22 20:09:21 +08:00
|
|
|
struct drm_device *dev = obj->base.dev;
|
2016-04-08 19:11:11 +08:00
|
|
|
void *addr;
|
|
|
|
int ret;
|
2012-05-22 20:09:21 +08:00
|
|
|
|
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
drm/i915: Support for creating write combined type vmaps
vmaps has a provision for controlling the page protection bits, with which
we can use to control the mapping type, e.g. WB, WC, UC or even WT.
To allow the caller to choose their mapping type, we add a parameter to
i915_gem_object_pin_map - but we still only allow one vmap to be cached
per object. If the object is currently not pinned, then we recreate the
previous vmap with the new access type, but if it was pinned we report an
error. This effectively limits the access via i915_gem_object_pin_map to a
single mapping type for the lifetime of the object. Not usually a problem,
but something to be aware of when setting up the object's vmap.
We will want to vary the access type to enable WC mappings of ringbuffer
and context objects on !llc platforms, as well as other objects where we
need coherent access to the GPU's pages without going through the GTT
v2: Remove the redundant braces around pin count check and fix the marker
in documentation (Chris)
v3:
- Add a new enum for the vmalloc mapping type & pass that as an argument to
i915_object_pin_map. (Tvrtko)
- Use PAGE_MASK to extract or filter the mapping type info and remove a
superfluous BUG_ON.(Tvrtko)
v4:
- Rename the enums and clean up the pin_map function. (Chris)
v5: Drop the VM_NO_GUARD, minor cosmetics.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: Akash Goel <akash.goel@intel.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
Link: http://patchwork.freedesktop.org/patch/msgid/1471001999-17787-1-git-send-email-chris@chris-wilson.co.uk
2016-08-12 19:39:58 +08:00
|
|
|
addr = i915_gem_object_pin_map(obj, I915_MAP_WB);
|
2012-05-22 20:09:21 +08:00
|
|
|
mutex_unlock(&dev->struct_mutex);
|
2012-06-01 22:20:22 +08:00
|
|
|
|
2016-04-08 19:11:11 +08:00
|
|
|
return addr;
|
2012-05-22 20:09:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
|
|
|
|
{
|
2013-08-08 15:10:38 +08:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
2012-05-22 20:09:21 +08:00
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
|
2014-04-08 04:01:47 +08:00
|
|
|
mutex_lock(&dev->struct_mutex);
|
2016-04-08 19:11:11 +08:00
|
|
|
i915_gem_object_unpin_map(obj);
|
2012-05-22 20:09:21 +08:00
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:25:09 +08:00
|
|
|
static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-05-29 22:11:22 +08:00
|
|
|
static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
|
|
|
|
{
|
2015-12-23 05:36:48 +08:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (obj->base.size < vma->vm_end - vma->vm_start)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!obj->base.filp)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
ret = obj->base.filp->f_op->mmap(obj->base.filp, vma);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
fput(vma->vm_file);
|
|
|
|
vma->vm_file = get_file(obj->base.filp);
|
|
|
|
|
|
|
|
return 0;
|
2012-05-29 22:11:22 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 05:36:45 +08:00
|
|
|
static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
|
2012-08-16 08:15:34 +08:00
|
|
|
{
|
2013-08-08 15:10:38 +08:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
2012-08-16 08:15:34 +08:00
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
int ret;
|
|
|
|
bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
|
|
|
|
|
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = i915_gem_object_set_to_cpu_domain(obj, write);
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
dma-buf, drm, ion: Propagate error code from dma_buf_start_cpu_access()
Drivers, especially i915.ko, can fail during the initial migration of a
dma-buf for CPU access. However, the error code from the driver was not
being propagated back to ioctl and so userspace was blissfully ignorant
of the failure. Rendering corruption ensues.
Whilst fixing the ioctl to return the error code from
dma_buf_start_cpu_access(), also do the same for
dma_buf_end_cpu_access(). For most drivers, dma_buf_end_cpu_access()
cannot fail. i915.ko however, as most drivers would, wants to avoid being
uninterruptible (as would be required to guarrantee no failure when
flushing the buffer to the device). As userspace already has to handle
errors from the SYNC_IOCTL, take advantage of this to be able to restart
the syscall across signals.
This fixes a coherency issue for i915.ko as well as reducing the
uninterruptible hold upon its BKL, the struct_mutex.
Fixes commit c11e391da2a8fe973c3c2398452000bed505851e
Author: Daniel Vetter <daniel.vetter@ffwll.ch>
Date: Thu Feb 11 20:04:51 2016 -0200
dma-buf: Add ioctls to allow userspace to flush
Testcase: igt/gem_concurrent_blit/*dmabuf*interruptible
Testcase: igt/prime_mmap_coherency/ioctl-errors
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tiago Vignatti <tiago.vignatti@intel.com>
Cc: Stéphane Marchesin <marcheu@chromium.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Daniel Vetter <daniel.vetter@intel.com>
CC: linux-media@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
Cc: linaro-mm-sig@lists.linaro.org
Cc: intel-gfx@lists.freedesktop.org
Cc: devel@driverdev.osuosl.org
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/1458331359-2634-1-git-send-email-chris@chris-wilson.co.uk
2016-03-19 04:02:39 +08:00
|
|
|
static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
|
2015-12-23 05:36:47 +08:00
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
int ret;
|
|
|
|
|
dma-buf, drm, ion: Propagate error code from dma_buf_start_cpu_access()
Drivers, especially i915.ko, can fail during the initial migration of a
dma-buf for CPU access. However, the error code from the driver was not
being propagated back to ioctl and so userspace was blissfully ignorant
of the failure. Rendering corruption ensues.
Whilst fixing the ioctl to return the error code from
dma_buf_start_cpu_access(), also do the same for
dma_buf_end_cpu_access(). For most drivers, dma_buf_end_cpu_access()
cannot fail. i915.ko however, as most drivers would, wants to avoid being
uninterruptible (as would be required to guarrantee no failure when
flushing the buffer to the device). As userspace already has to handle
errors from the SYNC_IOCTL, take advantage of this to be able to restart
the syscall across signals.
This fixes a coherency issue for i915.ko as well as reducing the
uninterruptible hold upon its BKL, the struct_mutex.
Fixes commit c11e391da2a8fe973c3c2398452000bed505851e
Author: Daniel Vetter <daniel.vetter@ffwll.ch>
Date: Thu Feb 11 20:04:51 2016 -0200
dma-buf: Add ioctls to allow userspace to flush
Testcase: igt/gem_concurrent_blit/*dmabuf*interruptible
Testcase: igt/prime_mmap_coherency/ioctl-errors
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tiago Vignatti <tiago.vignatti@intel.com>
Cc: Stéphane Marchesin <marcheu@chromium.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Daniel Vetter <daniel.vetter@intel.com>
CC: linux-media@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
Cc: linaro-mm-sig@lists.linaro.org
Cc: intel-gfx@lists.freedesktop.org
Cc: devel@driverdev.osuosl.org
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/1458331359-2634-1-git-send-email-chris@chris-wilson.co.uk
2016-03-19 04:02:39 +08:00
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-12-23 05:36:47 +08:00
|
|
|
|
|
|
|
ret = i915_gem_object_set_to_gtt_domain(obj, false);
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
|
dma-buf, drm, ion: Propagate error code from dma_buf_start_cpu_access()
Drivers, especially i915.ko, can fail during the initial migration of a
dma-buf for CPU access. However, the error code from the driver was not
being propagated back to ioctl and so userspace was blissfully ignorant
of the failure. Rendering corruption ensues.
Whilst fixing the ioctl to return the error code from
dma_buf_start_cpu_access(), also do the same for
dma_buf_end_cpu_access(). For most drivers, dma_buf_end_cpu_access()
cannot fail. i915.ko however, as most drivers would, wants to avoid being
uninterruptible (as would be required to guarrantee no failure when
flushing the buffer to the device). As userspace already has to handle
errors from the SYNC_IOCTL, take advantage of this to be able to restart
the syscall across signals.
This fixes a coherency issue for i915.ko as well as reducing the
uninterruptible hold upon its BKL, the struct_mutex.
Fixes commit c11e391da2a8fe973c3c2398452000bed505851e
Author: Daniel Vetter <daniel.vetter@ffwll.ch>
Date: Thu Feb 11 20:04:51 2016 -0200
dma-buf: Add ioctls to allow userspace to flush
Testcase: igt/gem_concurrent_blit/*dmabuf*interruptible
Testcase: igt/prime_mmap_coherency/ioctl-errors
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tiago Vignatti <tiago.vignatti@intel.com>
Cc: Stéphane Marchesin <marcheu@chromium.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Daniel Vetter <daniel.vetter@intel.com>
CC: linux-media@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
Cc: linaro-mm-sig@lists.linaro.org
Cc: intel-gfx@lists.freedesktop.org
Cc: devel@driverdev.osuosl.org
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/1458331359-2634-1-git-send-email-chris@chris-wilson.co.uk
2016-03-19 04:02:39 +08:00
|
|
|
return ret;
|
2015-12-23 05:36:47 +08:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:09:32 +08:00
|
|
|
static const struct dma_buf_ops i915_dmabuf_ops = {
|
2012-05-10 21:25:09 +08:00
|
|
|
.map_dma_buf = i915_gem_map_dma_buf,
|
|
|
|
.unmap_dma_buf = i915_gem_unmap_dma_buf,
|
2013-08-15 06:02:30 +08:00
|
|
|
.release = drm_gem_dmabuf_release,
|
2012-05-10 21:25:09 +08:00
|
|
|
.kmap = i915_gem_dmabuf_kmap,
|
|
|
|
.kmap_atomic = i915_gem_dmabuf_kmap_atomic,
|
|
|
|
.kunmap = i915_gem_dmabuf_kunmap,
|
|
|
|
.kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
|
2012-05-29 22:11:22 +08:00
|
|
|
.mmap = i915_gem_dmabuf_mmap,
|
2012-05-22 20:09:21 +08:00
|
|
|
.vmap = i915_gem_dmabuf_vmap,
|
|
|
|
.vunmap = i915_gem_dmabuf_vunmap,
|
2012-08-16 08:15:34 +08:00
|
|
|
.begin_cpu_access = i915_gem_begin_cpu_access,
|
2015-12-23 05:36:47 +08:00
|
|
|
.end_cpu_access = i915_gem_end_cpu_access,
|
2012-05-10 21:25:09 +08:00
|
|
|
};
|
|
|
|
|
2016-08-04 23:32:42 +08:00
|
|
|
static void export_fences(struct drm_i915_gem_object *obj,
|
|
|
|
struct dma_buf *dma_buf)
|
|
|
|
{
|
|
|
|
struct reservation_object *resv = dma_buf->resv;
|
|
|
|
struct drm_i915_gem_request *req;
|
|
|
|
unsigned long active;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
active = __I915_BO_ACTIVE(obj);
|
|
|
|
if (!active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Serialise with execbuf to prevent concurrent fence-loops */
|
|
|
|
mutex_lock(&obj->base.dev->struct_mutex);
|
|
|
|
|
|
|
|
/* Mark the object for future fences before racily adding old fences */
|
|
|
|
obj->base.dma_buf = dma_buf;
|
|
|
|
|
|
|
|
ww_mutex_lock(&resv->lock, NULL);
|
|
|
|
|
|
|
|
for_each_active(active, idx) {
|
|
|
|
req = i915_gem_active_get(&obj->last_read[idx],
|
|
|
|
&obj->base.dev->struct_mutex);
|
|
|
|
if (!req)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (reservation_object_reserve_shared(resv) == 0)
|
|
|
|
reservation_object_add_shared_fence(resv, &req->fence);
|
|
|
|
|
|
|
|
i915_gem_request_put(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
req = i915_gem_active_get(&obj->last_write,
|
|
|
|
&obj->base.dev->struct_mutex);
|
|
|
|
if (req) {
|
|
|
|
reservation_object_add_excl_fence(resv, &req->fence);
|
|
|
|
i915_gem_request_put(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
ww_mutex_unlock(&resv->lock);
|
|
|
|
mutex_unlock(&obj->base.dev->struct_mutex);
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:25:09 +08:00
|
|
|
struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
|
2012-06-01 22:20:22 +08:00
|
|
|
struct drm_gem_object *gem_obj, int flags)
|
2012-05-10 21:25:09 +08:00
|
|
|
{
|
drm/i915: Introduce mapping of user pages into video memory (userptr) ioctl
By exporting the ability to map user address and inserting PTEs
representing their backing pages into the GTT, we can exploit UMA in order
to utilize normal application data as a texture source or even as a
render target (depending upon the capabilities of the chipset). This has
a number of uses, with zero-copy downloads to the GPU and efficient
readback making the intermixed streaming of CPU and GPU operations
fairly efficient. This ability has many widespread implications from
faster rendering of client-side software rasterisers (chromium),
mitigation of stalls due to read back (firefox) and to faster pipelining
of texture data (such as pixel buffer objects in GL or data blobs in CL).
v2: Compile with CONFIG_MMU_NOTIFIER
v3: We can sleep while performing invalidate-range, which we can utilise
to drop our page references prior to the kernel manipulating the vma
(for either discard or cloning) and so protect normal users.
v4: Only run the invalidate notifier if the range intercepts the bo.
v5: Prevent userspace from attempting to GTT mmap non-page aligned buffers
v6: Recheck after reacquire mutex for lost mmu.
v7: Fix implicit padding of ioctl struct by rounding to next 64bit boundary.
v8: Fix rebasing error after forwarding porting the back port.
v9: Limit the userptr to page aligned entries. We now expect userspace
to handle all the offset-in-page adjustments itself.
v10: Prevent vma from being copied across fork to avoid issues with cow.
v11: Drop vma behaviour changes -- locking is nigh on impossible.
Use a worker to load user pages to avoid lock inversions.
v12: Use get_task_mm()/mmput() for correct refcounting of mm.
v13: Use a worker to release the mmu_notifier to avoid lock inversion
v14: Decouple mmu_notifier from struct_mutex using a custom mmu_notifer
with its own locking and tree of objects for each mm/mmu_notifier.
v15: Prevent overlapping userptr objects, and invalidate all objects
within the mmu_notifier range
v16: Fix a typo for iterating over multiple objects in the range and
rearrange error path to destroy the mmu_notifier locklessly.
Also close a race between invalidate_range and the get_pages_worker.
v17: Close a race between get_pages_worker/invalidate_range and fresh
allocations of the same userptr range - and notice that
struct_mutex was presumed to be held when during creation it wasn't.
v18: Sigh. Fix the refactor of st_set_pages() to allocate enough memory
for the struct sg_table and to clear it before reporting an error.
v19: Always error out on read-only userptr requests as we don't have the
hardware infrastructure to support them at the moment.
v20: Refuse to implement read-only support until we have the required
infrastructure - but reserve the bit in flags for future use.
v21: use_mm() is not required for get_user_pages(). It is only meant to
be used to fix up the kernel thread's current->mm for use with
copy_user().
v22: Use sg_alloc_table_from_pages for that chunky feeling
v23: Export a function for sanity checking dma-buf rather than encode
userptr details elsewhere, and clean up comments based on
suggestions by Bradley.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
Cc: "Gong, Zhipeng" <zhipeng.gong@intel.com>
Cc: Akash Goel <akash.goel@intel.com>
Cc: "Volkin, Bradley D" <bradley.d.volkin@intel.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
Reviewed-by: Brad Volkin <bradley.d.volkin@intel.com>
[danvet: Frob ioctl allocation to pick the next one - will cause a bit
of fuss with create2 apparently, but such are the rules.]
[danvet2: oops, forgot to git add after manual patch application]
[danvet3: Appease sparse.]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
2014-05-16 21:22:37 +08:00
|
|
|
struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
|
2015-01-23 15:23:43 +08:00
|
|
|
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
|
2016-08-04 23:32:42 +08:00
|
|
|
struct dma_buf *dma_buf;
|
2015-01-23 15:23:43 +08:00
|
|
|
|
|
|
|
exp_info.ops = &i915_dmabuf_ops;
|
|
|
|
exp_info.size = gem_obj->size;
|
|
|
|
exp_info.flags = flags;
|
|
|
|
exp_info.priv = gem_obj;
|
|
|
|
|
drm/i915: Introduce mapping of user pages into video memory (userptr) ioctl
By exporting the ability to map user address and inserting PTEs
representing their backing pages into the GTT, we can exploit UMA in order
to utilize normal application data as a texture source or even as a
render target (depending upon the capabilities of the chipset). This has
a number of uses, with zero-copy downloads to the GPU and efficient
readback making the intermixed streaming of CPU and GPU operations
fairly efficient. This ability has many widespread implications from
faster rendering of client-side software rasterisers (chromium),
mitigation of stalls due to read back (firefox) and to faster pipelining
of texture data (such as pixel buffer objects in GL or data blobs in CL).
v2: Compile with CONFIG_MMU_NOTIFIER
v3: We can sleep while performing invalidate-range, which we can utilise
to drop our page references prior to the kernel manipulating the vma
(for either discard or cloning) and so protect normal users.
v4: Only run the invalidate notifier if the range intercepts the bo.
v5: Prevent userspace from attempting to GTT mmap non-page aligned buffers
v6: Recheck after reacquire mutex for lost mmu.
v7: Fix implicit padding of ioctl struct by rounding to next 64bit boundary.
v8: Fix rebasing error after forwarding porting the back port.
v9: Limit the userptr to page aligned entries. We now expect userspace
to handle all the offset-in-page adjustments itself.
v10: Prevent vma from being copied across fork to avoid issues with cow.
v11: Drop vma behaviour changes -- locking is nigh on impossible.
Use a worker to load user pages to avoid lock inversions.
v12: Use get_task_mm()/mmput() for correct refcounting of mm.
v13: Use a worker to release the mmu_notifier to avoid lock inversion
v14: Decouple mmu_notifier from struct_mutex using a custom mmu_notifer
with its own locking and tree of objects for each mm/mmu_notifier.
v15: Prevent overlapping userptr objects, and invalidate all objects
within the mmu_notifier range
v16: Fix a typo for iterating over multiple objects in the range and
rearrange error path to destroy the mmu_notifier locklessly.
Also close a race between invalidate_range and the get_pages_worker.
v17: Close a race between get_pages_worker/invalidate_range and fresh
allocations of the same userptr range - and notice that
struct_mutex was presumed to be held when during creation it wasn't.
v18: Sigh. Fix the refactor of st_set_pages() to allocate enough memory
for the struct sg_table and to clear it before reporting an error.
v19: Always error out on read-only userptr requests as we don't have the
hardware infrastructure to support them at the moment.
v20: Refuse to implement read-only support until we have the required
infrastructure - but reserve the bit in flags for future use.
v21: use_mm() is not required for get_user_pages(). It is only meant to
be used to fix up the kernel thread's current->mm for use with
copy_user().
v22: Use sg_alloc_table_from_pages for that chunky feeling
v23: Export a function for sanity checking dma-buf rather than encode
userptr details elsewhere, and clean up comments based on
suggestions by Bradley.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
Cc: "Gong, Zhipeng" <zhipeng.gong@intel.com>
Cc: Akash Goel <akash.goel@intel.com>
Cc: "Volkin, Bradley D" <bradley.d.volkin@intel.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
Reviewed-by: Brad Volkin <bradley.d.volkin@intel.com>
[danvet: Frob ioctl allocation to pick the next one - will cause a bit
of fuss with create2 apparently, but such are the rules.]
[danvet2: oops, forgot to git add after manual patch application]
[danvet3: Appease sparse.]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
2014-05-16 21:22:37 +08:00
|
|
|
if (obj->ops->dmabuf_export) {
|
|
|
|
int ret = obj->ops->dmabuf_export(obj);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
2016-10-05 20:21:44 +08:00
|
|
|
dma_buf = drm_gem_dmabuf_export(dev, &exp_info);
|
2016-08-04 23:32:42 +08:00
|
|
|
if (IS_ERR(dma_buf))
|
|
|
|
return dma_buf;
|
|
|
|
|
|
|
|
export_fences(obj, dma_buf);
|
|
|
|
return dma_buf;
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
2012-09-05 04:02:58 +08:00
|
|
|
static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct sg_table *sg;
|
|
|
|
|
|
|
|
sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL);
|
|
|
|
if (IS_ERR(sg))
|
|
|
|
return PTR_ERR(sg);
|
|
|
|
|
|
|
|
obj->pages = sg;
|
|
|
|
return 0;
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
2012-09-05 04:02:58 +08:00
|
|
|
static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj)
|
|
|
|
{
|
|
|
|
dma_buf_unmap_attachment(obj->base.import_attach,
|
|
|
|
obj->pages, DMA_BIDIRECTIONAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
|
|
|
|
.get_pages = i915_gem_object_get_pages_dmabuf,
|
|
|
|
.put_pages = i915_gem_object_put_pages_dmabuf,
|
|
|
|
};
|
|
|
|
|
2012-05-10 21:25:09 +08:00
|
|
|
struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
|
2012-06-01 22:20:22 +08:00
|
|
|
struct dma_buf *dma_buf)
|
2012-05-10 21:25:09 +08:00
|
|
|
{
|
|
|
|
struct dma_buf_attachment *attach;
|
|
|
|
struct drm_i915_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* is this one of own objects? */
|
|
|
|
if (dma_buf->ops == &i915_dmabuf_ops) {
|
2013-08-08 15:10:38 +08:00
|
|
|
obj = dma_buf_to_obj(dma_buf);
|
2012-05-10 21:25:09 +08:00
|
|
|
/* is it from our device? */
|
|
|
|
if (obj->base.dev == dev) {
|
2012-09-27 14:30:06 +08:00
|
|
|
/*
|
|
|
|
* Importing dmabuf exported from out own gem increases
|
|
|
|
* refcount on gem itself instead of f_count of dmabuf.
|
|
|
|
*/
|
2016-07-20 20:31:52 +08:00
|
|
|
return &i915_gem_object_get(obj)->base;
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to attach */
|
|
|
|
attach = dma_buf_attach(dma_buf, dev->dev);
|
|
|
|
if (IS_ERR(attach))
|
|
|
|
return ERR_CAST(attach);
|
|
|
|
|
2013-04-19 09:11:56 +08:00
|
|
|
get_dma_buf(dma_buf);
|
|
|
|
|
2012-11-15 19:32:30 +08:00
|
|
|
obj = i915_gem_object_alloc(dev);
|
2012-05-10 21:25:09 +08:00
|
|
|
if (obj == NULL) {
|
|
|
|
ret = -ENOMEM;
|
2012-09-05 04:02:58 +08:00
|
|
|
goto fail_detach;
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:56:32 +08:00
|
|
|
drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
|
2012-09-05 04:02:58 +08:00
|
|
|
i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
|
2012-05-10 21:25:09 +08:00
|
|
|
obj->base.import_attach = attach;
|
|
|
|
|
2016-07-20 16:21:14 +08:00
|
|
|
/* We use GTT as shorthand for a coherent domain, one that is
|
|
|
|
* neither in the GPU cache nor in the CPU cache, where all
|
|
|
|
* writes are immediately visible in memory. (That's not strictly
|
|
|
|
* true, but it's close! There are internal buffers such as the
|
|
|
|
* write-combined buffer or a delay through the chipset for GTT
|
|
|
|
* writes that do require us to treat GTT as a separate cache domain.)
|
|
|
|
*/
|
|
|
|
obj->base.read_domains = I915_GEM_DOMAIN_GTT;
|
|
|
|
obj->base.write_domain = 0;
|
|
|
|
|
2012-05-10 21:25:09 +08:00
|
|
|
return &obj->base;
|
|
|
|
|
|
|
|
fail_detach:
|
|
|
|
dma_buf_detach(dma_buf, attach);
|
2013-04-19 09:11:56 +08:00
|
|
|
dma_buf_put(dma_buf);
|
|
|
|
|
2012-05-10 21:25:09 +08:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|