2019-05-29 20:31:08 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/*
|
|
|
|
* Copyright © 2019 Intel Corporation
|
|
|
|
*/
|
|
|
|
|
2019-08-06 18:07:30 +08:00
|
|
|
#include "i915_drv.h"
|
2019-08-10 17:17:47 +08:00
|
|
|
#include "gt/intel_context.h"
|
2019-08-10 17:29:45 +08:00
|
|
|
#include "gt/intel_engine_pm.h"
|
|
|
|
#include "gt/intel_engine_pool.h"
|
2019-08-06 19:39:33 +08:00
|
|
|
#include "i915_gem_client_blt.h"
|
2019-05-29 20:31:08 +08:00
|
|
|
#include "i915_gem_object_blt.h"
|
|
|
|
|
|
|
|
struct i915_sleeve {
|
|
|
|
struct i915_vma *vma;
|
|
|
|
struct drm_i915_gem_object *obj;
|
|
|
|
struct sg_table *pages;
|
|
|
|
struct i915_page_sizes page_sizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int vma_set_pages(struct i915_vma *vma)
|
|
|
|
{
|
|
|
|
struct i915_sleeve *sleeve = vma->private;
|
|
|
|
|
|
|
|
vma->pages = sleeve->pages;
|
|
|
|
vma->page_sizes = sleeve->page_sizes;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vma_clear_pages(struct i915_vma *vma)
|
|
|
|
{
|
|
|
|
GEM_BUG_ON(!vma->pages);
|
|
|
|
vma->pages = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vma_bind(struct i915_vma *vma,
|
|
|
|
enum i915_cache_level cache_level,
|
|
|
|
u32 flags)
|
|
|
|
{
|
|
|
|
return vma->vm->vma_ops.bind_vma(vma, cache_level, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vma_unbind(struct i915_vma *vma)
|
|
|
|
{
|
|
|
|
vma->vm->vma_ops.unbind_vma(vma);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i915_vma_ops proxy_vma_ops = {
|
|
|
|
.set_pages = vma_set_pages,
|
|
|
|
.clear_pages = vma_clear_pages,
|
|
|
|
.bind_vma = vma_bind,
|
|
|
|
.unbind_vma = vma_unbind,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct i915_sleeve *create_sleeve(struct i915_address_space *vm,
|
|
|
|
struct drm_i915_gem_object *obj,
|
|
|
|
struct sg_table *pages,
|
|
|
|
struct i915_page_sizes *page_sizes)
|
|
|
|
{
|
|
|
|
struct i915_sleeve *sleeve;
|
|
|
|
struct i915_vma *vma;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
sleeve = kzalloc(sizeof(*sleeve), GFP_KERNEL);
|
|
|
|
if (!sleeve)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
vma = i915_vma_instance(obj, vm, NULL);
|
|
|
|
if (IS_ERR(vma)) {
|
|
|
|
err = PTR_ERR(vma);
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
vma->private = sleeve;
|
|
|
|
vma->ops = &proxy_vma_ops;
|
|
|
|
|
|
|
|
sleeve->vma = vma;
|
|
|
|
sleeve->pages = pages;
|
|
|
|
sleeve->page_sizes = *page_sizes;
|
|
|
|
|
|
|
|
return sleeve;
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
kfree(sleeve);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_sleeve(struct i915_sleeve *sleeve)
|
|
|
|
{
|
|
|
|
kfree(sleeve);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct clear_pages_work {
|
|
|
|
struct dma_fence dma;
|
|
|
|
struct dma_fence_cb cb;
|
|
|
|
struct i915_sw_fence wait;
|
|
|
|
struct work_struct work;
|
|
|
|
struct irq_work irq_work;
|
|
|
|
struct i915_sleeve *sleeve;
|
|
|
|
struct intel_context *ce;
|
|
|
|
u32 value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *clear_pages_work_driver_name(struct dma_fence *fence)
|
|
|
|
{
|
|
|
|
return DRIVER_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *clear_pages_work_timeline_name(struct dma_fence *fence)
|
|
|
|
{
|
|
|
|
return "clear";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_pages_work_release(struct dma_fence *fence)
|
|
|
|
{
|
|
|
|
struct clear_pages_work *w = container_of(fence, typeof(*w), dma);
|
|
|
|
|
|
|
|
destroy_sleeve(w->sleeve);
|
|
|
|
|
|
|
|
i915_sw_fence_fini(&w->wait);
|
|
|
|
|
|
|
|
BUILD_BUG_ON(offsetof(typeof(*w), dma));
|
|
|
|
dma_fence_free(&w->dma);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dma_fence_ops clear_pages_work_ops = {
|
|
|
|
.get_driver_name = clear_pages_work_driver_name,
|
|
|
|
.get_timeline_name = clear_pages_work_timeline_name,
|
|
|
|
.release = clear_pages_work_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void clear_pages_signal_irq_worker(struct irq_work *work)
|
|
|
|
{
|
|
|
|
struct clear_pages_work *w = container_of(work, typeof(*w), irq_work);
|
|
|
|
|
|
|
|
dma_fence_signal(&w->dma);
|
|
|
|
dma_fence_put(&w->dma);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_pages_dma_fence_cb(struct dma_fence *fence,
|
|
|
|
struct dma_fence_cb *cb)
|
|
|
|
{
|
|
|
|
struct clear_pages_work *w = container_of(cb, typeof(*w), cb);
|
|
|
|
|
|
|
|
if (fence->error)
|
|
|
|
dma_fence_set_error(&w->dma, fence->error);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Push the signalling of the fence into yet another worker to avoid
|
|
|
|
* the nightmare locking around the fence spinlock.
|
|
|
|
*/
|
|
|
|
irq_work_queue(&w->irq_work);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_pages_worker(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct clear_pages_work *w = container_of(work, typeof(*w), work);
|
2019-06-22 05:57:33 +08:00
|
|
|
struct drm_i915_gem_object *obj = w->sleeve->vma->obj;
|
2019-05-29 20:31:08 +08:00
|
|
|
struct i915_vma *vma = w->sleeve->vma;
|
|
|
|
struct i915_request *rq;
|
2019-08-10 17:29:45 +08:00
|
|
|
struct i915_vma *batch;
|
2019-05-29 20:31:08 +08:00
|
|
|
int err = w->dma.error;
|
|
|
|
|
|
|
|
if (unlikely(err))
|
|
|
|
goto out_signal;
|
|
|
|
|
|
|
|
if (obj->cache_dirty) {
|
|
|
|
if (i915_gem_object_has_struct_page(obj))
|
|
|
|
drm_clflush_sg(w->sleeve->pages);
|
|
|
|
obj->cache_dirty = false;
|
|
|
|
}
|
2019-06-24 22:16:30 +08:00
|
|
|
obj->read_domains = I915_GEM_GPU_DOMAINS;
|
|
|
|
obj->write_domain = 0;
|
2019-05-29 20:31:08 +08:00
|
|
|
|
|
|
|
err = i915_vma_pin(vma, 0, 0, PIN_USER);
|
|
|
|
if (unlikely(err))
|
2019-10-04 21:40:02 +08:00
|
|
|
goto out_signal;
|
2019-05-29 20:31:08 +08:00
|
|
|
|
2019-08-10 17:29:45 +08:00
|
|
|
batch = intel_emit_vma_fill_blt(w->ce, vma, w->value);
|
|
|
|
if (IS_ERR(batch)) {
|
|
|
|
err = PTR_ERR(batch);
|
|
|
|
goto out_unpin;
|
|
|
|
}
|
|
|
|
|
2019-08-10 17:17:47 +08:00
|
|
|
rq = intel_context_create_request(w->ce);
|
2019-05-29 20:31:08 +08:00
|
|
|
if (IS_ERR(rq)) {
|
|
|
|
err = PTR_ERR(rq);
|
2019-08-10 17:29:45 +08:00
|
|
|
goto out_batch;
|
2019-05-29 20:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* There's no way the fence has signalled */
|
|
|
|
if (dma_fence_add_callback(&rq->fence, &w->cb,
|
|
|
|
clear_pages_dma_fence_cb))
|
|
|
|
GEM_BUG_ON(1);
|
|
|
|
|
2019-08-10 17:29:45 +08:00
|
|
|
err = intel_emit_vma_mark_active(batch, rq);
|
|
|
|
if (unlikely(err))
|
|
|
|
goto out_request;
|
|
|
|
|
2019-05-29 20:31:08 +08:00
|
|
|
if (w->ce->engine->emit_init_breadcrumb) {
|
|
|
|
err = w->ce->engine->emit_init_breadcrumb(rq);
|
|
|
|
if (unlikely(err))
|
|
|
|
goto out_request;
|
|
|
|
}
|
|
|
|
|
2019-06-22 05:57:33 +08:00
|
|
|
/*
|
|
|
|
* w->dma is already exported via (vma|obj)->resv we need only
|
|
|
|
* keep track of the GPU activity within this vma/request, and
|
|
|
|
* propagate the signal from the request to w->dma.
|
|
|
|
*/
|
drm/i915: Pull i915_vma_pin under the vm->mutex
Replace the struct_mutex requirement for pinning the i915_vma with the
local vm->mutex instead. Note that the vm->mutex is tainted by the
shrinker (we require unbinding from inside fs-reclaim) and so we cannot
allocate while holding that mutex. Instead we have to preallocate
workers to do allocate and apply the PTE updates after we have we
reserved their slot in the drm_mm (using fences to order the PTE writes
with the GPU work and with later unbind).
In adding the asynchronous vma binding, one subtle requirement is to
avoid coupling the binding fence into the backing object->resv. That is
the asynchronous binding only applies to the vma timeline itself and not
to the pages as that is a more global timeline (the binding of one vma
does not need to be ordered with another vma, nor does the implicit GEM
fencing depend on a vma, only on writes to the backing store). Keeping
the vma binding distinct from the backing store timelines is verified by
a number of async gem_exec_fence and gem_exec_schedule tests. The way we
do this is quite simple, we keep the fence for the vma binding separate
and only wait on it as required, and never add it to the obj->resv
itself.
Another consequence in reducing the locking around the vma is the
destruction of the vma is no longer globally serialised by struct_mutex.
A natural solution would be to add a kref to i915_vma, but that requires
decoupling the reference cycles, possibly by introducing a new
i915_mm_pages object that is own by both obj->mm and vma->pages.
However, we have not taken that route due to the overshadowing lmem/ttm
discussions, and instead play a series of complicated games with
trylocks to (hopefully) ensure that only one destruction path is called!
v2: Add some commentary, and some helpers to reduce patch churn.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20191004134015.13204-4-chris@chris-wilson.co.uk
2019-10-04 21:39:58 +08:00
|
|
|
err = __i915_vma_move_to_active(vma, rq);
|
2019-05-29 20:31:08 +08:00
|
|
|
if (err)
|
|
|
|
goto out_request;
|
|
|
|
|
2019-08-10 17:29:45 +08:00
|
|
|
err = w->ce->engine->emit_bb_start(rq,
|
|
|
|
batch->node.start, batch->node.size,
|
|
|
|
0);
|
2019-05-29 20:31:08 +08:00
|
|
|
out_request:
|
|
|
|
if (unlikely(err)) {
|
|
|
|
i915_request_skip(rq, err);
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i915_request_add(rq);
|
2019-08-10 17:29:45 +08:00
|
|
|
out_batch:
|
|
|
|
intel_emit_vma_release(w->ce, batch);
|
2019-05-29 20:31:08 +08:00
|
|
|
out_unpin:
|
|
|
|
i915_vma_unpin(vma);
|
|
|
|
out_signal:
|
|
|
|
if (unlikely(err)) {
|
|
|
|
dma_fence_set_error(&w->dma, err);
|
|
|
|
dma_fence_signal(&w->dma);
|
|
|
|
dma_fence_put(&w->dma);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __i915_sw_fence_call
|
|
|
|
clear_pages_work_notify(struct i915_sw_fence *fence,
|
|
|
|
enum i915_sw_fence_notify state)
|
|
|
|
{
|
|
|
|
struct clear_pages_work *w = container_of(fence, typeof(*w), wait);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case FENCE_COMPLETE:
|
|
|
|
schedule_work(&w->work);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FENCE_FREE:
|
|
|
|
dma_fence_put(&w->dma);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(fence_lock);
|
|
|
|
|
|
|
|
/* XXX: better name please */
|
|
|
|
int i915_gem_schedule_fill_pages_blt(struct drm_i915_gem_object *obj,
|
|
|
|
struct intel_context *ce,
|
|
|
|
struct sg_table *pages,
|
|
|
|
struct i915_page_sizes *page_sizes,
|
|
|
|
u32 value)
|
|
|
|
{
|
|
|
|
struct clear_pages_work *work;
|
|
|
|
struct i915_sleeve *sleeve;
|
|
|
|
int err;
|
|
|
|
|
2019-07-30 22:32:09 +08:00
|
|
|
sleeve = create_sleeve(ce->vm, obj, pages, page_sizes);
|
2019-05-29 20:31:08 +08:00
|
|
|
if (IS_ERR(sleeve))
|
|
|
|
return PTR_ERR(sleeve);
|
|
|
|
|
|
|
|
work = kmalloc(sizeof(*work), GFP_KERNEL);
|
|
|
|
if (!work) {
|
|
|
|
destroy_sleeve(sleeve);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
work->value = value;
|
|
|
|
work->sleeve = sleeve;
|
|
|
|
work->ce = ce;
|
|
|
|
|
|
|
|
INIT_WORK(&work->work, clear_pages_worker);
|
|
|
|
|
|
|
|
init_irq_work(&work->irq_work, clear_pages_signal_irq_worker);
|
|
|
|
|
2019-08-20 02:44:03 +08:00
|
|
|
dma_fence_init(&work->dma, &clear_pages_work_ops, &fence_lock, 0, 0);
|
2019-05-29 20:31:08 +08:00
|
|
|
i915_sw_fence_init(&work->wait, clear_pages_work_notify);
|
|
|
|
|
|
|
|
i915_gem_object_lock(obj);
|
|
|
|
err = i915_sw_fence_await_reservation(&work->wait,
|
2019-06-18 20:58:58 +08:00
|
|
|
obj->base.resv, NULL,
|
2019-05-29 20:31:08 +08:00
|
|
|
true, I915_FENCE_TIMEOUT,
|
|
|
|
I915_FENCE_GFP);
|
|
|
|
if (err < 0) {
|
|
|
|
dma_fence_set_error(&work->dma, err);
|
|
|
|
} else {
|
2019-08-11 16:06:32 +08:00
|
|
|
dma_resv_add_excl_fence(obj->base.resv, &work->dma);
|
2019-05-29 20:31:08 +08:00
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
i915_gem_object_unlock(obj);
|
|
|
|
|
|
|
|
dma_fence_get(&work->dma);
|
|
|
|
i915_sw_fence_commit(&work->wait);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
|
|
|
|
#include "selftests/i915_gem_client_blt.c"
|
|
|
|
#endif
|