2012-05-10 13:25:09 +00: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>
|
|
|
|
*/
|
2012-10-02 17:01:07 +00:00
|
|
|
#include <drm/drmP.h>
|
2012-05-10 13:25:09 +00:00
|
|
|
#include "i915_drv.h"
|
|
|
|
#include <linux/dma-buf.h>
|
|
|
|
|
2013-08-08 07:10:38 +00:00
|
|
|
static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
|
|
|
|
{
|
|
|
|
return to_intel_bo(buf->priv);
|
|
|
|
}
|
|
|
|
|
2012-05-23 13:09:32 +00:00
|
|
|
static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
|
2012-06-01 14:20:22 +00:00
|
|
|
enum dma_data_direction dir)
|
2012-05-10 13:25:09 +00:00
|
|
|
{
|
2013-08-08 07:10:38 +00:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
|
2012-06-01 14:20:22 +00:00
|
|
|
struct sg_table *st;
|
|
|
|
struct scatterlist *src, *dst;
|
|
|
|
int ret, i;
|
2012-05-10 13:25:09 +00:00
|
|
|
|
2012-06-01 14:20:22 +00:00
|
|
|
ret = i915_mutex_lock_interruptible(obj->base.dev);
|
2012-05-10 13:25:09 +00:00
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
2012-06-07 14:38:42 +00:00
|
|
|
ret = i915_gem_object_get_pages(obj);
|
drm/i915: Track unbound pages
When dealing with a working set larger than the GATT, or even the
mappable aperture when touching through the GTT, we end up with evicting
objects only to rebind them at a new offset again later. Moving an
object into and out of the GTT requires clflushing the pages, thus
causing a double-clflush penalty for rebinding.
To avoid having to clflush on rebinding, we can track the pages as they
are evicted from the GTT and only relinquish those pages on memory
pressure.
As usual, if it were not for the handling of out-of-memory condition and
having to manually shrink our own bo caches, it would be a net reduction
of code. Alas.
Note: The patch also contains a few changes to the last-hope
evict_everything logic in i916_gem_execbuffer.c - we no longer try to
only evict the purgeable stuff in a first try (since that's superflous
and only helps in OOM corner-cases, not fragmented-gtt trashing
situations).
Also, the extraction of the get_pages retry loop from bind_to_gtt (and
other callsites) to get_pages should imo have been a separate patch.
v2: Ditch the newly added put_pages (for unbound objects only) in
i915_gem_reset. A quick irc discussion hasn't revealed any important
reason for this, so if we need this, I'd like to have a git blame'able
explanation for it.
v3: Undo the s/drm_malloc_ab/kmalloc/ in get_pages that Chris noticed.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
[danvet: Split out code movements and rant a bit in the commit message
with a few Notes. Done v2]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
2012-08-20 09:40:46 +00:00
|
|
|
if (ret) {
|
2012-06-01 14:20:22 +00:00
|
|
|
st = ERR_PTR(ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy sg so that we make an independent mapping */
|
|
|
|
st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
|
|
|
|
if (st == NULL) {
|
|
|
|
st = ERR_PTR(-ENOMEM);
|
|
|
|
goto out;
|
2012-05-10 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 14:20:22 +00:00
|
|
|
ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
|
|
|
|
if (ret) {
|
|
|
|
kfree(st);
|
|
|
|
st = ERR_PTR(ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = obj->pages->sgl;
|
|
|
|
dst = st->sgl;
|
|
|
|
for (i = 0; i < obj->pages->nents; i++) {
|
2013-02-18 17:28:02 +00:00
|
|
|
sg_set_page(dst, sg_page(src), src->length, 0);
|
2012-06-01 14:20:22 +00:00
|
|
|
dst = sg_next(dst);
|
|
|
|
src = sg_next(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
|
|
|
|
sg_free_table(st);
|
|
|
|
kfree(st);
|
|
|
|
st = ERR_PTR(-ENOMEM);
|
drm/i915: Track unbound pages
When dealing with a working set larger than the GATT, or even the
mappable aperture when touching through the GTT, we end up with evicting
objects only to rebind them at a new offset again later. Moving an
object into and out of the GTT requires clflushing the pages, thus
causing a double-clflush penalty for rebinding.
To avoid having to clflush on rebinding, we can track the pages as they
are evicted from the GTT and only relinquish those pages on memory
pressure.
As usual, if it were not for the handling of out-of-memory condition and
having to manually shrink our own bo caches, it would be a net reduction
of code. Alas.
Note: The patch also contains a few changes to the last-hope
evict_everything logic in i916_gem_execbuffer.c - we no longer try to
only evict the purgeable stuff in a first try (since that's superflous
and only helps in OOM corner-cases, not fragmented-gtt trashing
situations).
Also, the extraction of the get_pages retry loop from bind_to_gtt (and
other callsites) to get_pages should imo have been a separate patch.
v2: Ditch the newly added put_pages (for unbound objects only) in
i915_gem_reset. A quick irc discussion hasn't revealed any important
reason for this, so if we need this, I'd like to have a git blame'able
explanation for it.
v3: Undo the s/drm_malloc_ab/kmalloc/ in get_pages that Chris noticed.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
[danvet: Split out code movements and rant a bit in the commit message
with a few Notes. Done v2]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
2012-08-20 09:40:46 +00:00
|
|
|
goto out;
|
2012-05-10 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 20:02:54 +00:00
|
|
|
i915_gem_object_pin_pages(obj);
|
|
|
|
|
2012-05-10 13:25:09 +00:00
|
|
|
out:
|
2012-06-01 14:20:22 +00:00
|
|
|
mutex_unlock(&obj->base.dev->struct_mutex);
|
|
|
|
return st;
|
2012-05-10 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 13:09:32 +00:00
|
|
|
static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
|
2012-09-04 20:02:58 +00:00
|
|
|
struct sg_table *sg,
|
|
|
|
enum dma_data_direction dir)
|
2012-05-10 13:25:09 +00:00
|
|
|
{
|
2013-08-08 07:10:38 +00:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
|
2013-08-08 07:10:37 +00:00
|
|
|
|
|
|
|
mutex_lock(&obj->base.dev->struct_mutex);
|
|
|
|
|
2012-05-10 13:25:09 +00:00
|
|
|
dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
|
|
|
|
sg_free_table(sg);
|
|
|
|
kfree(sg);
|
2013-08-08 07:10:37 +00:00
|
|
|
|
|
|
|
i915_gem_object_unpin_pages(obj);
|
|
|
|
|
|
|
|
mutex_unlock(&obj->base.dev->struct_mutex);
|
2012-05-10 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 12:09:21 +00:00
|
|
|
static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
|
|
|
|
{
|
2013-08-08 07:10:38 +00:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
2012-05-22 12:09:21 +00:00
|
|
|
struct drm_device *dev = obj->base.dev;
|
2013-02-18 17:28:02 +00:00
|
|
|
struct sg_page_iter sg_iter;
|
2012-06-01 14:20:22 +00:00
|
|
|
struct page **pages;
|
|
|
|
int ret, i;
|
2012-05-22 12:09:21 +00:00
|
|
|
|
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
if (obj->dma_buf_vmapping) {
|
|
|
|
obj->vmapping_count++;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2012-06-07 14:38:42 +00:00
|
|
|
ret = i915_gem_object_get_pages(obj);
|
2012-06-01 14:20:22 +00:00
|
|
|
if (ret)
|
|
|
|
goto error;
|
2012-05-22 12:09:21 +00:00
|
|
|
|
2012-06-01 14:20:22 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
|
2013-02-18 17:28:02 +00:00
|
|
|
pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
|
2012-06-01 14:20:22 +00:00
|
|
|
if (pages == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2013-02-18 17:28:02 +00:00
|
|
|
i = 0;
|
2013-05-01 04:23:41 +00:00
|
|
|
for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0)
|
2013-03-26 13:14:18 +00:00
|
|
|
pages[i++] = sg_page_iter_page(&sg_iter);
|
2012-06-01 14:20:22 +00:00
|
|
|
|
2013-02-18 17:28:02 +00:00
|
|
|
obj->dma_buf_vmapping = vmap(pages, i, 0, PAGE_KERNEL);
|
2012-06-01 14:20:22 +00:00
|
|
|
drm_free_large(pages);
|
|
|
|
|
|
|
|
if (!obj->dma_buf_vmapping)
|
|
|
|
goto error;
|
2012-05-22 12:09:21 +00:00
|
|
|
|
|
|
|
obj->vmapping_count = 1;
|
2012-09-04 20:02:54 +00:00
|
|
|
i915_gem_object_pin_pages(obj);
|
2012-05-22 12:09:21 +00:00
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
return obj->dma_buf_vmapping;
|
2012-06-01 14:20:22 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
return ERR_PTR(ret);
|
2012-05-22 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
|
|
|
|
{
|
2013-08-08 07:10:38 +00:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
2012-05-22 12:09:21 +00:00
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
|
2012-09-04 20:02:54 +00:00
|
|
|
if (--obj->vmapping_count == 0) {
|
2012-05-22 12:09:21 +00:00
|
|
|
vunmap(obj->dma_buf_vmapping);
|
|
|
|
obj->dma_buf_vmapping = NULL;
|
2012-09-04 20:02:54 +00:00
|
|
|
|
|
|
|
i915_gem_object_unpin_pages(obj);
|
2012-05-22 12:09:21 +00:00
|
|
|
}
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
}
|
|
|
|
|
2012-05-10 13:25:09 +00: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 14:11:22 +00:00
|
|
|
static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-08-16 00:15:34 +00:00
|
|
|
static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
|
|
|
|
{
|
2013-08-08 07:10:38 +00:00
|
|
|
struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
|
2012-08-16 00:15:34 +00: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;
|
|
|
|
}
|
|
|
|
|
2012-05-23 13:09:32 +00:00
|
|
|
static const struct dma_buf_ops i915_dmabuf_ops = {
|
2012-05-10 13:25:09 +00:00
|
|
|
.map_dma_buf = i915_gem_map_dma_buf,
|
|
|
|
.unmap_dma_buf = i915_gem_unmap_dma_buf,
|
2013-08-14 22:02:30 +00:00
|
|
|
.release = drm_gem_dmabuf_release,
|
2012-05-10 13:25:09 +00: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 14:11:22 +00:00
|
|
|
.mmap = i915_gem_dmabuf_mmap,
|
2012-05-22 12:09:21 +00:00
|
|
|
.vmap = i915_gem_dmabuf_vmap,
|
|
|
|
.vunmap = i915_gem_dmabuf_vunmap,
|
2012-08-16 00:15:34 +00:00
|
|
|
.begin_cpu_access = i915_gem_begin_cpu_access,
|
2012-05-10 13:25:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
|
2012-06-01 14:20:22 +00:00
|
|
|
struct drm_gem_object *gem_obj, int flags)
|
2012-05-10 13:25:09 +00:00
|
|
|
{
|
2013-08-08 07:10:38 +00:00
|
|
|
return dma_buf_export(gem_obj, &i915_dmabuf_ops, gem_obj->size, flags);
|
2012-05-10 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 20:02:58 +00: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;
|
|
|
|
obj->has_dma_mapping = true;
|
|
|
|
return 0;
|
2012-05-10 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 20:02:58 +00: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);
|
|
|
|
obj->has_dma_mapping = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 13:25:09 +00:00
|
|
|
struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
|
2012-06-01 14:20:22 +00:00
|
|
|
struct dma_buf *dma_buf)
|
2012-05-10 13:25:09 +00: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 07:10:38 +00:00
|
|
|
obj = dma_buf_to_obj(dma_buf);
|
2012-05-10 13:25:09 +00:00
|
|
|
/* is it from our device? */
|
|
|
|
if (obj->base.dev == dev) {
|
2012-09-27 06:30:06 +00:00
|
|
|
/*
|
|
|
|
* Importing dmabuf exported from out own gem increases
|
|
|
|
* refcount on gem itself instead of f_count of dmabuf.
|
|
|
|
*/
|
2012-05-10 13:25:09 +00:00
|
|
|
drm_gem_object_reference(&obj->base);
|
|
|
|
return &obj->base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to attach */
|
|
|
|
attach = dma_buf_attach(dma_buf, dev->dev);
|
|
|
|
if (IS_ERR(attach))
|
|
|
|
return ERR_CAST(attach);
|
|
|
|
|
2013-04-19 01:11:56 +00:00
|
|
|
get_dma_buf(dma_buf);
|
|
|
|
|
2012-11-15 11:32:30 +00:00
|
|
|
obj = i915_gem_object_alloc(dev);
|
2012-05-10 13:25:09 +00:00
|
|
|
if (obj == NULL) {
|
|
|
|
ret = -ENOMEM;
|
2012-09-04 20:02:58 +00:00
|
|
|
goto fail_detach;
|
2012-05-10 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 09:56:32 +00:00
|
|
|
drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
|
2012-09-04 20:02:58 +00:00
|
|
|
i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
|
2012-05-10 13:25:09 +00:00
|
|
|
obj->base.import_attach = attach;
|
|
|
|
|
|
|
|
return &obj->base;
|
|
|
|
|
|
|
|
fail_detach:
|
|
|
|
dma_buf_detach(dma_buf, attach);
|
2013-04-19 01:11:56 +00:00
|
|
|
dma_buf_put(dma_buf);
|
|
|
|
|
2012-05-10 13:25:09 +00:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|