Commit ebece753 authored by Chris Wilson's avatar Chris Wilson

drm/i915: Keep timeline HWSP allocated until idle across the system

In preparation for enabling HW semaphores, we need to keep in flight
timeline HWSP alive until its use across entire system has completed,
as any other timeline active on the GPU may still refer back to the
already retired timeline. We both have to delay recycling available
cachelines and unpinning old HWSP until the next idle point.

An easy option would be to simply keep all used HWSP until the system as
a whole was idle, i.e. we could release them all at once on parking.
However, on a busy system, we may never see a global idle point,
essentially meaning the resource will be leaked until we are forced to
do a GC pass. We already employ a fine-grained idle detection mechanism
for vma, which we can reuse here so that each cacheline can be freed
immediately after the last request using it is retired.

v3: Keep track of the activity of each cacheline.
v4: cacheline_free() on canceling the seqno tracking
v5: Finally with a testcase to exercise wraparound
v6: Pack cacheline into empty bits of page-aligned vaddr
v7: Use i915_utils to hide the pointer casting around bit manipulation
Signed-off-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
Reviewed-by: default avatarTvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190301170901.8340-2-chris@chris-wilson.co.uk
parent 1e3f697e
...@@ -325,11 +325,6 @@ void i915_request_retire_upto(struct i915_request *rq) ...@@ -325,11 +325,6 @@ void i915_request_retire_upto(struct i915_request *rq)
} while (tmp != rq); } while (tmp != rq);
} }
static u32 timeline_get_seqno(struct i915_timeline *tl)
{
return tl->seqno += 1 + tl->has_initial_breadcrumb;
}
static void move_to_timeline(struct i915_request *request, static void move_to_timeline(struct i915_request *request,
struct i915_timeline *timeline) struct i915_timeline *timeline)
{ {
...@@ -532,8 +527,10 @@ struct i915_request * ...@@ -532,8 +527,10 @@ struct i915_request *
i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx) i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx)
{ {
struct drm_i915_private *i915 = engine->i915; struct drm_i915_private *i915 = engine->i915;
struct i915_request *rq;
struct intel_context *ce; struct intel_context *ce;
struct i915_timeline *tl;
struct i915_request *rq;
u32 seqno;
int ret; int ret;
lockdep_assert_held(&i915->drm.struct_mutex); lockdep_assert_held(&i915->drm.struct_mutex);
...@@ -610,24 +607,27 @@ i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx) ...@@ -610,24 +607,27 @@ i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx)
} }
} }
rq->rcustate = get_state_synchronize_rcu();
INIT_LIST_HEAD(&rq->active_list); INIT_LIST_HEAD(&rq->active_list);
tl = ce->ring->timeline;
ret = i915_timeline_get_seqno(tl, rq, &seqno);
if (ret)
goto err_free;
rq->i915 = i915; rq->i915 = i915;
rq->engine = engine; rq->engine = engine;
rq->gem_context = ctx; rq->gem_context = ctx;
rq->hw_context = ce; rq->hw_context = ce;
rq->ring = ce->ring; rq->ring = ce->ring;
rq->timeline = ce->ring->timeline; rq->timeline = tl;
GEM_BUG_ON(rq->timeline == &engine->timeline); GEM_BUG_ON(rq->timeline == &engine->timeline);
rq->hwsp_seqno = rq->timeline->hwsp_seqno; rq->hwsp_seqno = tl->hwsp_seqno;
rq->hwsp_cacheline = tl->hwsp_cacheline;
rq->rcustate = get_state_synchronize_rcu(); /* acts as smp_mb() */
spin_lock_init(&rq->lock); spin_lock_init(&rq->lock);
dma_fence_init(&rq->fence, dma_fence_init(&rq->fence, &i915_fence_ops, &rq->lock,
&i915_fence_ops, tl->fence_context, seqno);
&rq->lock,
rq->timeline->fence_context,
timeline_get_seqno(rq->timeline));
/* We bump the ref for the fence chain */ /* We bump the ref for the fence chain */
i915_sw_fence_init(&i915_request_get(rq)->submit, submit_notify); i915_sw_fence_init(&i915_request_get(rq)->submit, submit_notify);
...@@ -687,6 +687,7 @@ i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx) ...@@ -687,6 +687,7 @@ i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx)
GEM_BUG_ON(!list_empty(&rq->sched.signalers_list)); GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
GEM_BUG_ON(!list_empty(&rq->sched.waiters_list)); GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
err_free:
kmem_cache_free(global.slab_requests, rq); kmem_cache_free(global.slab_requests, rq);
err_unreserve: err_unreserve:
mutex_unlock(&ce->ring->timeline->mutex); mutex_unlock(&ce->ring->timeline->mutex);
......
...@@ -38,6 +38,7 @@ struct drm_file; ...@@ -38,6 +38,7 @@ struct drm_file;
struct drm_i915_gem_object; struct drm_i915_gem_object;
struct i915_request; struct i915_request;
struct i915_timeline; struct i915_timeline;
struct i915_timeline_cacheline;
struct i915_capture_list { struct i915_capture_list {
struct i915_capture_list *next; struct i915_capture_list *next;
...@@ -148,6 +149,16 @@ struct i915_request { ...@@ -148,6 +149,16 @@ struct i915_request {
*/ */
const u32 *hwsp_seqno; const u32 *hwsp_seqno;
/*
* If we need to access the timeline's seqno for this request in
* another request, we need to keep a read reference to this associated
* cacheline, so that we do not free and recycle it before the foreign
* observers have completed. Hence, we keep a pointer to the cacheline
* inside the timeline's HWSP vma, but it is only valid while this
* request has not completed and guarded by the timeline mutex.
*/
struct i915_timeline_cacheline *hwsp_cacheline;
/** Position in the ring of the start of the request */ /** Position in the ring of the start of the request */
u32 head; u32 head;
......
This diff is collapsed.
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
#include "i915_utils.h" #include "i915_utils.h"
struct i915_vma; struct i915_vma;
struct i915_timeline_hwsp; struct i915_timeline_cacheline;
struct i915_timeline { struct i915_timeline {
u64 fence_context; u64 fence_context;
...@@ -51,6 +51,8 @@ struct i915_timeline { ...@@ -51,6 +51,8 @@ struct i915_timeline {
struct i915_vma *hwsp_ggtt; struct i915_vma *hwsp_ggtt;
u32 hwsp_offset; u32 hwsp_offset;
struct i915_timeline_cacheline *hwsp_cacheline;
bool has_initial_breadcrumb; bool has_initial_breadcrumb;
/** /**
...@@ -162,8 +164,15 @@ static inline bool i915_timeline_sync_is_later(struct i915_timeline *tl, ...@@ -162,8 +164,15 @@ static inline bool i915_timeline_sync_is_later(struct i915_timeline *tl,
} }
int i915_timeline_pin(struct i915_timeline *tl); int i915_timeline_pin(struct i915_timeline *tl);
int i915_timeline_get_seqno(struct i915_timeline *tl,
struct i915_request *rq,
u32 *seqno);
void i915_timeline_unpin(struct i915_timeline *tl); void i915_timeline_unpin(struct i915_timeline *tl);
int i915_timeline_read_hwsp(struct i915_request *from,
struct i915_request *until,
u32 *hwsp_offset);
void i915_timelines_init(struct drm_i915_private *i915); void i915_timelines_init(struct drm_i915_private *i915);
void i915_timelines_park(struct drm_i915_private *i915); void i915_timelines_park(struct drm_i915_private *i915);
void i915_timelines_fini(struct drm_i915_private *i915); void i915_timelines_fini(struct drm_i915_private *i915);
......
...@@ -641,6 +641,118 @@ static int live_hwsp_alternate(void *arg) ...@@ -641,6 +641,118 @@ static int live_hwsp_alternate(void *arg)
#undef NUM_TIMELINES #undef NUM_TIMELINES
} }
static int live_hwsp_wrap(void *arg)
{
struct drm_i915_private *i915 = arg;
struct intel_engine_cs *engine;
struct i915_timeline *tl;
enum intel_engine_id id;
intel_wakeref_t wakeref;
int err = 0;
/*
* Across a seqno wrap, we need to keep the old cacheline alive for
* foreign GPU references.
*/
mutex_lock(&i915->drm.struct_mutex);
wakeref = intel_runtime_pm_get(i915);
tl = i915_timeline_create(i915, __func__, NULL);
if (IS_ERR(tl)) {
err = PTR_ERR(tl);
goto out_rpm;
}
if (!tl->has_initial_breadcrumb || !tl->hwsp_cacheline)
goto out_free;
err = i915_timeline_pin(tl);
if (err)
goto out_free;
for_each_engine(engine, i915, id) {
const u32 *hwsp_seqno[2];
struct i915_request *rq;
u32 seqno[2];
if (!intel_engine_can_store_dword(engine))
continue;
rq = i915_request_alloc(engine, i915->kernel_context);
if (IS_ERR(rq)) {
err = PTR_ERR(rq);
goto out;
}
tl->seqno = -4u;
err = i915_timeline_get_seqno(tl, rq, &seqno[0]);
if (err) {
i915_request_add(rq);
goto out;
}
pr_debug("seqno[0]:%08x, hwsp_offset:%08x\n",
seqno[0], tl->hwsp_offset);
err = emit_ggtt_store_dw(rq, tl->hwsp_offset, seqno[0]);
if (err) {
i915_request_add(rq);
goto out;
}
hwsp_seqno[0] = tl->hwsp_seqno;
err = i915_timeline_get_seqno(tl, rq, &seqno[1]);
if (err) {
i915_request_add(rq);
goto out;
}
pr_debug("seqno[1]:%08x, hwsp_offset:%08x\n",
seqno[1], tl->hwsp_offset);
err = emit_ggtt_store_dw(rq, tl->hwsp_offset, seqno[1]);
if (err) {
i915_request_add(rq);
goto out;
}
hwsp_seqno[1] = tl->hwsp_seqno;
/* With wrap should come a new hwsp */
GEM_BUG_ON(seqno[1] >= seqno[0]);
GEM_BUG_ON(hwsp_seqno[0] == hwsp_seqno[1]);
i915_request_add(rq);
if (i915_request_wait(rq, I915_WAIT_LOCKED, HZ / 5) < 0) {
pr_err("Wait for timeline writes timed out!\n");
err = -EIO;
goto out;
}
if (*hwsp_seqno[0] != seqno[0] || *hwsp_seqno[1] != seqno[1]) {
pr_err("Bad timeline values: found (%x, %x), expected (%x, %x)\n",
*hwsp_seqno[0], *hwsp_seqno[1],
seqno[0], seqno[1]);
err = -EINVAL;
goto out;
}
i915_retire_requests(i915); /* recycle HWSP */
}
out:
if (igt_flush_test(i915, I915_WAIT_LOCKED))
err = -EIO;
i915_timeline_unpin(tl);
out_free:
i915_timeline_put(tl);
out_rpm:
intel_runtime_pm_put(i915, wakeref);
mutex_unlock(&i915->drm.struct_mutex);
return err;
}
static int live_hwsp_recycle(void *arg) static int live_hwsp_recycle(void *arg)
{ {
struct drm_i915_private *i915 = arg; struct drm_i915_private *i915 = arg;
...@@ -723,6 +835,7 @@ int i915_timeline_live_selftests(struct drm_i915_private *i915) ...@@ -723,6 +835,7 @@ int i915_timeline_live_selftests(struct drm_i915_private *i915)
SUBTEST(live_hwsp_recycle), SUBTEST(live_hwsp_recycle),
SUBTEST(live_hwsp_engine), SUBTEST(live_hwsp_engine),
SUBTEST(live_hwsp_alternate), SUBTEST(live_hwsp_alternate),
SUBTEST(live_hwsp_wrap),
}; };
return i915_subtests(tests, i915); return i915_subtests(tests, i915);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment