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>
|
|
|
|
*/
|
|
|
|
#include "drmP.h"
|
|
|
|
#include "i915_drv.h"
|
|
|
|
#include <linux/dma-buf.h>
|
|
|
|
|
2012-05-23 21:09:32 +08:00
|
|
|
static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
|
2012-05-10 21:25:09 +08:00
|
|
|
enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj = attachment->dmabuf->priv;
|
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
int npages = obj->base.size / PAGE_SIZE;
|
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 17:40:46 +08:00
|
|
|
struct sg_table *sg;
|
2012-05-10 21:25:09 +08:00
|
|
|
int ret;
|
|
|
|
int nents;
|
|
|
|
|
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
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 17:40:46 +08:00
|
|
|
ret = i915_gem_object_get_pages_gtt(obj);
|
|
|
|
if (ret) {
|
|
|
|
sg = ERR_PTR(ret);
|
|
|
|
goto out;
|
2012-05-10 21:25:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* link the pages into an SG then map the sg */
|
|
|
|
sg = drm_prime_pages_to_sg(obj->pages, npages);
|
|
|
|
nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
|
|
|
|
out:
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
return sg;
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:09:32 +08:00
|
|
|
static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
|
2012-05-10 21:25:09 +08:00
|
|
|
struct sg_table *sg, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
|
|
|
|
sg_free_table(sg);
|
|
|
|
kfree(sg);
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:09:32 +08:00
|
|
|
static void i915_gem_dmabuf_release(struct dma_buf *dma_buf)
|
2012-05-10 21:25:09 +08:00
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj = dma_buf->priv;
|
|
|
|
|
|
|
|
if (obj->base.export_dma_buf == dma_buf) {
|
|
|
|
/* drop the reference on the export fd holds */
|
|
|
|
obj->base.export_dma_buf = NULL;
|
|
|
|
drm_gem_object_unreference_unlocked(&obj->base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 20:09:21 +08:00
|
|
|
static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj = dma_buf->priv;
|
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
if (obj->dma_buf_vmapping) {
|
|
|
|
obj->vmapping_count++;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
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 17:40:46 +08:00
|
|
|
ret = i915_gem_object_get_pages_gtt(obj);
|
|
|
|
if (ret) {
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
return ERR_PTR(ret);
|
2012-05-22 20:09:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
obj->dma_buf_vmapping = vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL);
|
|
|
|
if (!obj->dma_buf_vmapping) {
|
|
|
|
DRM_ERROR("failed to vmap object\n");
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->vmapping_count = 1;
|
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
return obj->dma_buf_vmapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj = dma_buf->priv;
|
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = i915_mutex_lock_interruptible(dev);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
|
|
|
|
--obj->vmapping_count;
|
|
|
|
if (obj->vmapping_count == 0) {
|
|
|
|
vunmap(obj->dma_buf_vmapping);
|
|
|
|
obj->dma_buf_vmapping = NULL;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-08-16 08:15:34 +08:00
|
|
|
static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj = dma_buf->priv;
|
|
|
|
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 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,
|
|
|
|
.release = i915_gem_dmabuf_release,
|
|
|
|
.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,
|
2012-05-10 21:25:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
|
|
|
|
struct drm_gem_object *gem_obj, int flags)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
|
|
|
|
|
|
|
|
return dma_buf_export(obj, &i915_dmabuf_ops,
|
|
|
|
obj->base.size, 0600);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
|
|
|
|
struct dma_buf *dma_buf)
|
|
|
|
{
|
|
|
|
struct dma_buf_attachment *attach;
|
|
|
|
struct sg_table *sg;
|
|
|
|
struct drm_i915_gem_object *obj;
|
|
|
|
int npages;
|
|
|
|
int size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* is this one of own objects? */
|
|
|
|
if (dma_buf->ops == &i915_dmabuf_ops) {
|
|
|
|
obj = dma_buf->priv;
|
|
|
|
/* is it from our device? */
|
|
|
|
if (obj->base.dev == dev) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
|
|
|
|
if (IS_ERR(sg)) {
|
|
|
|
ret = PTR_ERR(sg);
|
|
|
|
goto fail_detach;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = dma_buf->size;
|
|
|
|
npages = size / PAGE_SIZE;
|
|
|
|
|
|
|
|
obj = kzalloc(sizeof(*obj), GFP_KERNEL);
|
|
|
|
if (obj == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = drm_gem_private_object_init(dev, &obj->base, size);
|
|
|
|
if (ret) {
|
|
|
|
kfree(obj);
|
|
|
|
goto fail_unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->sg_table = sg;
|
|
|
|
obj->base.import_attach = attach;
|
|
|
|
|
|
|
|
return &obj->base;
|
|
|
|
|
|
|
|
fail_unmap:
|
|
|
|
dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
|
|
|
|
fail_detach:
|
|
|
|
dma_buf_detach(dma_buf, attach);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|