Skip to content

Commit 4c60b1a

Browse files
committed
drm/i915/gt: Make deferred context allocation explicit
Refactor the backends to handle the deferred context allocation in a consistent manner, and allow calling it as an explicit first step in pinning a context for the first time. This should make it easier for backends to keep track of partially constructed contexts from initialisation. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Reviewed-by: Mika Kuoppala <mika.kuoppala@linux.intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20190809182518.20486-2-chris@chris-wilson.co.uk
1 parent 72e2777 commit 4c60b1a

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

drivers/gpu/drm/i915/gt/intel_context.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ int __intel_context_do_pin(struct intel_context *ce)
5353
if (likely(!atomic_read(&ce->pin_count))) {
5454
intel_wakeref_t wakeref;
5555

56+
if (unlikely(!test_bit(CONTEXT_ALLOC_BIT, &ce->flags))) {
57+
err = ce->ops->alloc(ce);
58+
if (unlikely(err))
59+
goto err;
60+
61+
__set_bit(CONTEXT_ALLOC_BIT, &ce->flags);
62+
}
63+
5664
err = 0;
5765
with_intel_runtime_pm(&ce->engine->i915->runtime_pm, wakeref)
5866
err = ce->ops->pin(ce);

drivers/gpu/drm/i915/gt/intel_context_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ struct intel_context;
2323
struct intel_ring;
2424

2525
struct intel_context_ops {
26+
int (*alloc)(struct intel_context *ce);
27+
2628
int (*pin)(struct intel_context *ce);
2729
void (*unpin)(struct intel_context *ce);
2830

@@ -52,6 +54,9 @@ struct intel_context {
5254
struct i915_vma *state;
5355
struct intel_ring *ring;
5456

57+
unsigned long flags;
58+
#define CONTEXT_ALLOC_BIT 0
59+
5560
u32 *lrc_reg_state;
5661
u64 lrc_desc;
5762

drivers/gpu/drm/i915/gt/intel_lrc.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ static struct virtual_engine *to_virtual_engine(struct intel_engine_cs *engine)
219219
return container_of(engine, struct virtual_engine, base);
220220
}
221221

222-
static int execlists_context_deferred_alloc(struct intel_context *ce,
223-
struct intel_engine_cs *engine);
222+
static int __execlists_context_alloc(struct intel_context *ce,
223+
struct intel_engine_cs *engine);
224+
224225
static void execlists_init_reg_state(u32 *reg_state,
225226
struct intel_context *ce,
226227
struct intel_engine_cs *engine,
@@ -1614,9 +1615,6 @@ __execlists_context_pin(struct intel_context *ce,
16141615
void *vaddr;
16151616
int ret;
16161617

1617-
ret = execlists_context_deferred_alloc(ce, engine);
1618-
if (ret)
1619-
goto err;
16201618
GEM_BUG_ON(!ce->state);
16211619

16221620
ret = intel_context_active_acquire(ce);
@@ -1655,6 +1653,11 @@ static int execlists_context_pin(struct intel_context *ce)
16551653
return __execlists_context_pin(ce, ce->engine);
16561654
}
16571655

1656+
static int execlists_context_alloc(struct intel_context *ce)
1657+
{
1658+
return __execlists_context_alloc(ce, ce->engine);
1659+
}
1660+
16581661
static void execlists_context_reset(struct intel_context *ce)
16591662
{
16601663
/*
@@ -1678,6 +1681,8 @@ static void execlists_context_reset(struct intel_context *ce)
16781681
}
16791682

16801683
static const struct intel_context_ops execlists_context_ops = {
1684+
.alloc = execlists_context_alloc,
1685+
16811686
.pin = execlists_context_pin,
16821687
.unpin = execlists_context_unpin,
16831688

@@ -3075,8 +3080,8 @@ get_timeline(struct i915_gem_context *ctx, struct intel_gt *gt)
30753080
return intel_timeline_create(gt, NULL);
30763081
}
30773082

3078-
static int execlists_context_deferred_alloc(struct intel_context *ce,
3079-
struct intel_engine_cs *engine)
3083+
static int __execlists_context_alloc(struct intel_context *ce,
3084+
struct intel_engine_cs *engine)
30803085
{
30813086
struct drm_i915_gem_object *ctx_obj;
30823087
struct i915_vma *vma;
@@ -3085,9 +3090,7 @@ static int execlists_context_deferred_alloc(struct intel_context *ce,
30853090
struct intel_timeline *timeline;
30863091
int ret;
30873092

3088-
if (ce->state)
3089-
return 0;
3090-
3093+
GEM_BUG_ON(ce->state);
30913094
context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
30923095

30933096
/*
@@ -3533,6 +3536,12 @@ intel_execlists_create_virtual(struct i915_gem_context *ctx,
35333536

35343537
ve->base.flags |= I915_ENGINE_IS_VIRTUAL;
35353538

3539+
err = __execlists_context_alloc(&ve->context, siblings[0]);
3540+
if (err)
3541+
goto err_put;
3542+
3543+
__set_bit(CONTEXT_ALLOC_BIT, &ve->context.flags);
3544+
35363545
return &ve->context;
35373546

35383547
err_put:

drivers/gpu/drm/i915/gt/intel_ringbuffer.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,16 +1480,16 @@ alloc_context_vma(struct intel_engine_cs *engine)
14801480
return ERR_PTR(err);
14811481
}
14821482

1483-
static int ring_context_pin(struct intel_context *ce)
1483+
static int ring_context_alloc(struct intel_context *ce)
14841484
{
14851485
struct intel_engine_cs *engine = ce->engine;
1486-
int err;
14871486

14881487
/* One ringbuffer to rule them all */
14891488
GEM_BUG_ON(!engine->buffer);
14901489
ce->ring = engine->buffer;
14911490

1492-
if (!ce->state && engine->context_size) {
1491+
GEM_BUG_ON(ce->state);
1492+
if (engine->context_size) {
14931493
struct i915_vma *vma;
14941494

14951495
vma = alloc_context_vma(engine);
@@ -1499,6 +1499,13 @@ static int ring_context_pin(struct intel_context *ce)
14991499
ce->state = vma;
15001500
}
15011501

1502+
return 0;
1503+
}
1504+
1505+
static int ring_context_pin(struct intel_context *ce)
1506+
{
1507+
int err;
1508+
15021509
err = intel_context_active_acquire(ce);
15031510
if (err)
15041511
return err;
@@ -1520,6 +1527,8 @@ static void ring_context_reset(struct intel_context *ce)
15201527
}
15211528

15221529
static const struct intel_context_ops ring_context_ops = {
1530+
.alloc = ring_context_alloc,
1531+
15231532
.pin = ring_context_pin,
15241533
.unpin = ring_context_unpin,
15251534

drivers/gpu/drm/i915/gt/mock_engine.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,19 @@ static void mock_context_destroy(struct kref *ref)
147147
intel_context_free(ce);
148148
}
149149

150+
static int mock_context_alloc(struct intel_context *ce)
151+
{
152+
ce->ring = mock_ring(ce->engine);
153+
if (!ce->ring)
154+
return -ENOMEM;
155+
156+
return 0;
157+
}
158+
150159
static int mock_context_pin(struct intel_context *ce)
151160
{
152161
int ret;
153162

154-
if (!ce->ring) {
155-
ce->ring = mock_ring(ce->engine);
156-
if (!ce->ring)
157-
return -ENOMEM;
158-
}
159-
160163
ret = intel_context_active_acquire(ce);
161164
if (ret)
162165
return ret;
@@ -166,6 +169,8 @@ static int mock_context_pin(struct intel_context *ce)
166169
}
167170

168171
static const struct intel_context_ops mock_context_ops = {
172+
.alloc = mock_context_alloc,
173+
169174
.pin = mock_context_pin,
170175
.unpin = mock_context_unpin,
171176

0 commit comments

Comments
 (0)