Skip to content

Commit fb55c73

Browse files
yanzhao56zhenyw
authored andcommitted
drm/i915/gvt: skip populate shadow context if guest context not changed
Software is not expected to populate engine context except when using restore inhibit bit or golden state to initialize it for the first time. Therefore, if a newly submitted guest context is the same as the last shadowed one, no need to populate its engine context from guest again. Currently using lrca + ring_context_gpa to identify whether two guest contexts are the same. The reason of why context id is not included as an identifier is that i915 recently changed the code and context id is only unique for a context when OA is enabled. And when OA is on, context id is generated based on lrca. Therefore, in that case, if two contexts are of the same lrca, they have identical context ids as well. (This patch also works with old guest kernel like 4.20.) for guest context, if its ggtt entry is modified after last context shadowing, it is also deemed as not the same context as last shadowed one. v7: -removed local variable "valid". use the one in s->last_ctx diretly v6: -change type of lrca of last ctx to be u32. as currently it's all protected by vgpu lock (Kevin Tian) -reset valid of last ctx to false once it needs to be repopulated before population completes successfully (Kevin Tian) v5: -merge all 3 patches into one patch (Zhenyu Wang) v4: - split the series into 3 patches. - don't turn on optimization until last patch in this series (Kevin Tian) - define lrca to be atomic in this patch rather than update its type in the second patch (Kevin Tian) v3: updated commit message to describe engine context and context id clearly (Kevin Tian) v2: rebased to 5.6.0-rc4+Signed-off-by: Yan Zhao <[email protected]> Reviewed-by: Zhenyu Wang <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Cc: Kevin Tian <[email protected]> Suggested-by: Zhenyu Wang <[email protected]> Signed-off-by: Yan Zhao <[email protected]> Signed-off-by: Zhenyu Wang <[email protected]> Link: http://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent e5e1130 commit fb55c73

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

drivers/gpu/drm/i915/gvt/gtt.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,12 +2337,27 @@ int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
23372337
{
23382338
const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
23392339
int ret;
2340+
struct intel_vgpu_submission *s = &vgpu->submission;
2341+
struct intel_engine_cs *engine;
2342+
int i;
23402343

23412344
if (bytes != 4 && bytes != 8)
23422345
return -EINVAL;
23432346

23442347
off -= info->gtt_start_offset;
23452348
ret = emulate_ggtt_mmio_write(vgpu, off, p_data, bytes);
2349+
2350+
/* if ggtt of last submitted context is written,
2351+
* that context is probably got unpinned.
2352+
* Set last shadowed ctx to invalid.
2353+
*/
2354+
for_each_engine(engine, vgpu->gvt->gt, i) {
2355+
if (!s->last_ctx[i].valid)
2356+
continue;
2357+
2358+
if (s->last_ctx[i].lrca == (off >> info->gtt_entry_size_shift))
2359+
s->last_ctx[i].valid = false;
2360+
}
23462361
return ret;
23472362
}
23482363

drivers/gpu/drm/i915/gvt/gvt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ struct intel_vgpu_submission {
163163
const struct intel_vgpu_submission_ops *ops;
164164
int virtual_submission_interface;
165165
bool active;
166+
struct {
167+
u32 lrca;
168+
bool valid;
169+
u64 ring_context_gpa;
170+
} last_ctx[I915_NUM_ENGINES];
166171
};
167172

168173
struct intel_vgpu {

drivers/gpu/drm/i915/gvt/scheduler.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ static int populate_shadow_context(struct intel_vgpu_workload *workload)
135135
unsigned long context_gpa, context_page_num;
136136
unsigned long gpa_base; /* first gpa of consecutive GPAs */
137137
unsigned long gpa_size; /* size of consecutive GPAs */
138+
struct intel_vgpu_submission *s = &vgpu->submission;
138139
int i;
140+
bool skip = false;
141+
int ring_id = workload->engine->id;
139142

140143
GEM_BUG_ON(!intel_context_is_pinned(ctx));
141144

@@ -175,13 +178,31 @@ static int populate_shadow_context(struct intel_vgpu_workload *workload)
175178

176179
sr_oa_regs(workload, (u32 *)shadow_ring_context, false);
177180

178-
if (IS_RESTORE_INHIBIT(shadow_ring_context->ctx_ctrl.val))
179-
return 0;
181+
gvt_dbg_sched("ring %s workload lrca %x, ctx_id %x, ctx gpa %llx",
182+
workload->engine->name, workload->ctx_desc.lrca,
183+
workload->ctx_desc.context_id,
184+
workload->ring_context_gpa);
180185

181-
gvt_dbg_sched("ring %s workload lrca %x",
182-
workload->engine->name,
183-
workload->ctx_desc.lrca);
186+
/* only need to ensure this context is not pinned/unpinned during the
187+
* period from last submission to this this submission.
188+
* Upon reaching this function, the currently submitted context is not
189+
* supposed to get unpinned. If a misbehaving guest driver ever does
190+
* this, it would corrupt itself.
191+
*/
192+
if (s->last_ctx[ring_id].valid &&
193+
(s->last_ctx[ring_id].lrca ==
194+
workload->ctx_desc.lrca) &&
195+
(s->last_ctx[ring_id].ring_context_gpa ==
196+
workload->ring_context_gpa))
197+
skip = true;
184198

199+
s->last_ctx[ring_id].lrca = workload->ctx_desc.lrca;
200+
s->last_ctx[ring_id].ring_context_gpa = workload->ring_context_gpa;
201+
202+
if (IS_RESTORE_INHIBIT(shadow_ring_context->ctx_ctrl.val) || skip)
203+
return 0;
204+
205+
s->last_ctx[ring_id].valid = false;
185206
context_page_num = workload->engine->context_size;
186207
context_page_num = context_page_num >> PAGE_SHIFT;
187208

@@ -220,6 +241,7 @@ static int populate_shadow_context(struct intel_vgpu_workload *workload)
220241
gpa_size = I915_GTT_PAGE_SIZE;
221242
dst = context_base + (i << I915_GTT_PAGE_SHIFT);
222243
}
244+
s->last_ctx[ring_id].valid = true;
223245
return 0;
224246
}
225247

@@ -1296,6 +1318,8 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)
12961318
atomic_set(&s->running_workload_num, 0);
12971319
bitmap_zero(s->tlb_handle_pending, I915_NUM_ENGINES);
12981320

1321+
memset(s->last_ctx, 0, sizeof(s->last_ctx));
1322+
12991323
i915_vm_put(&ppgtt->vm);
13001324
return 0;
13011325

0 commit comments

Comments
 (0)