blob: 13ccc00f0d408140ace45c80ba4e10ca0d12d63e [file] [log] [blame]
Zhi Wange4734052016-05-01 07:42:16 -04001/*
2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Zhi Wang <zhi.a.wang@intel.com>
25 *
26 * Contributors:
27 * Ping Gao <ping.a.gao@intel.com>
28 * Tina Zhang <tina.zhang@intel.com>
29 * Chanbin Du <changbin.du@intel.com>
30 * Min He <min.he@intel.com>
31 * Bing Niu <bing.niu@intel.com>
32 * Zhenyu Wang <zhenyuw@linux.intel.com>
33 *
34 */
35
Zhi Wange4734052016-05-01 07:42:16 -040036#include <linux/kthread.h>
37
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +080038#include "i915_drv.h"
39#include "gvt.h"
40
Zhi Wange4734052016-05-01 07:42:16 -040041#define RING_CTX_OFF(x) \
42 offsetof(struct execlist_ring_context, x)
43
Du, Changbin999ccb42016-10-20 14:08:47 +080044static void set_context_pdp_root_pointer(
45 struct execlist_ring_context *ring_context,
Zhi Wange4734052016-05-01 07:42:16 -040046 u32 pdp[8])
47{
48 struct execlist_mmio_pair *pdp_pair = &ring_context->pdp3_UDW;
49 int i;
50
51 for (i = 0; i < 8; i++)
52 pdp_pair[i].val = pdp[7 - i];
53}
54
55static int populate_shadow_context(struct intel_vgpu_workload *workload)
56{
57 struct intel_vgpu *vgpu = workload->vgpu;
58 struct intel_gvt *gvt = vgpu->gvt;
59 int ring_id = workload->ring_id;
Zhi Wang1406a142017-09-10 21:15:18 +080060 struct i915_gem_context *shadow_ctx = vgpu->submission.shadow_ctx;
Zhi Wange4734052016-05-01 07:42:16 -040061 struct drm_i915_gem_object *ctx_obj =
62 shadow_ctx->engine[ring_id].state->obj;
63 struct execlist_ring_context *shadow_ring_context;
64 struct page *page;
65 void *dst;
66 unsigned long context_gpa, context_page_num;
67 int i;
68
69 gvt_dbg_sched("ring id %d workload lrca %x", ring_id,
70 workload->ctx_desc.lrca);
71
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +030072 context_page_num = gvt->dev_priv->engine[ring_id]->context_size;
Zhi Wange4734052016-05-01 07:42:16 -040073
74 context_page_num = context_page_num >> PAGE_SHIFT;
75
76 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
77 context_page_num = 19;
78
79 i = 2;
80
81 while (i < context_page_num) {
82 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
83 (u32)((workload->ctx_desc.lrca + i) <<
Zhi Wang9556e112017-10-10 13:51:32 +080084 I915_GTT_PAGE_SHIFT));
Zhi Wange4734052016-05-01 07:42:16 -040085 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -050086 gvt_vgpu_err("Invalid guest context descriptor\n");
fred gao5c568832017-09-20 05:36:47 +080087 return -EFAULT;
Zhi Wange4734052016-05-01 07:42:16 -040088 }
89
Michel Thierry0b29c752017-09-13 09:56:00 +010090 page = i915_gem_object_get_page(ctx_obj, LRC_HEADER_PAGES + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +080091 dst = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -040092 intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst,
Zhi Wang9556e112017-10-10 13:51:32 +080093 I915_GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +080094 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -040095 i++;
96 }
97
98 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +080099 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400100
101#define COPY_REG(name) \
102 intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \
103 + RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
104
105 COPY_REG(ctx_ctrl);
106 COPY_REG(ctx_timestamp);
107
108 if (ring_id == RCS) {
109 COPY_REG(bb_per_ctx_ptr);
110 COPY_REG(rcs_indirect_ctx);
111 COPY_REG(rcs_indirect_ctx_offset);
112 }
113#undef COPY_REG
114
115 set_context_pdp_root_pointer(shadow_ring_context,
116 workload->shadow_mm->shadow_page_table);
117
118 intel_gvt_hypervisor_read_gpa(vgpu,
119 workload->ring_context_gpa +
120 sizeof(*shadow_ring_context),
121 (void *)shadow_ring_context +
122 sizeof(*shadow_ring_context),
Zhi Wang9556e112017-10-10 13:51:32 +0800123 I915_GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
Zhi Wange4734052016-05-01 07:42:16 -0400124
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800125 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400126 return 0;
127}
128
Changbin Dubc2d4b62017-03-22 12:35:31 +0800129static inline bool is_gvt_request(struct drm_i915_gem_request *req)
130{
131 return i915_gem_context_force_single_submission(req->ctx);
132}
133
Xiong Zhang295764c2017-11-07 05:23:02 +0800134static void save_ring_hw_state(struct intel_vgpu *vgpu, int ring_id)
135{
136 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
137 u32 ring_base = dev_priv->engine[ring_id]->mmio_base;
138 i915_reg_t reg;
139
140 reg = RING_INSTDONE(ring_base);
141 vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg);
142 reg = RING_ACTHD(ring_base);
143 vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg);
144 reg = RING_ACTHD_UDW(ring_base);
145 vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg);
146}
147
Zhi Wange4734052016-05-01 07:42:16 -0400148static int shadow_context_status_change(struct notifier_block *nb,
149 unsigned long action, void *data)
150{
Changbin Du3fc03062017-03-13 10:47:11 +0800151 struct drm_i915_gem_request *req = (struct drm_i915_gem_request *)data;
152 struct intel_gvt *gvt = container_of(nb, struct intel_gvt,
153 shadow_ctx_notifier_block[req->engine->id]);
154 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Changbin Du0e86cc92017-05-04 10:52:38 +0800155 enum intel_engine_id ring_id = req->engine->id;
156 struct intel_vgpu_workload *workload;
Zhi Wange4734052016-05-01 07:42:16 -0400157
Changbin Du0e86cc92017-05-04 10:52:38 +0800158 if (!is_gvt_request(req)) {
159 spin_lock_bh(&scheduler->mmio_context_lock);
160 if (action == INTEL_CONTEXT_SCHEDULE_IN &&
161 scheduler->engine_owner[ring_id]) {
162 /* Switch ring from vGPU to host. */
163 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
164 NULL, ring_id);
165 scheduler->engine_owner[ring_id] = NULL;
166 }
167 spin_unlock_bh(&scheduler->mmio_context_lock);
168
169 return NOTIFY_OK;
170 }
171
172 workload = scheduler->current_workload[ring_id];
173 if (unlikely(!workload))
Chuanxiao Dong9272f732017-02-17 19:29:52 +0800174 return NOTIFY_OK;
175
Zhi Wange4734052016-05-01 07:42:16 -0400176 switch (action) {
177 case INTEL_CONTEXT_SCHEDULE_IN:
Changbin Du0e86cc92017-05-04 10:52:38 +0800178 spin_lock_bh(&scheduler->mmio_context_lock);
179 if (workload->vgpu != scheduler->engine_owner[ring_id]) {
180 /* Switch ring from host to vGPU or vGPU to vGPU. */
181 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
182 workload->vgpu, ring_id);
183 scheduler->engine_owner[ring_id] = workload->vgpu;
184 } else
185 gvt_dbg_sched("skip ring %d mmio switch for vgpu%d\n",
186 ring_id, workload->vgpu->id);
187 spin_unlock_bh(&scheduler->mmio_context_lock);
Zhi Wange4734052016-05-01 07:42:16 -0400188 atomic_set(&workload->shadow_ctx_active, 1);
189 break;
190 case INTEL_CONTEXT_SCHEDULE_OUT:
Chris Wilsond6c05112017-10-03 21:34:47 +0100191 case INTEL_CONTEXT_SCHEDULE_PREEMPTED:
Xiong Zhang295764c2017-11-07 05:23:02 +0800192 save_ring_hw_state(workload->vgpu, ring_id);
Zhi Wange4734052016-05-01 07:42:16 -0400193 atomic_set(&workload->shadow_ctx_active, 0);
194 break;
195 default:
196 WARN_ON(1);
197 return NOTIFY_OK;
198 }
199 wake_up(&workload->shadow_ctx_status_wq);
200 return NOTIFY_OK;
201}
202
Kechen Lu9dfb8e52017-08-10 07:41:36 +0800203static void shadow_context_descriptor_update(struct i915_gem_context *ctx,
204 struct intel_engine_cs *engine)
205{
206 struct intel_context *ce = &ctx->engine[engine->id];
207 u64 desc = 0;
208
209 desc = ce->lrc_desc;
210
211 /* Update bits 0-11 of the context descriptor which includes flags
212 * like GEN8_CTX_* cached in desc_template
213 */
214 desc &= U64_MAX << 12;
215 desc |= ctx->desc_template & ((1ULL << 12) - 1);
216
217 ce->lrc_desc = desc;
218}
219
fred gao0a53bc02017-08-18 15:41:06 +0800220static int copy_workload_to_ring_buffer(struct intel_vgpu_workload *workload)
221{
222 struct intel_vgpu *vgpu = workload->vgpu;
223 void *shadow_ring_buffer_va;
224 u32 *cs;
225
226 /* allocate shadow ring buffer */
227 cs = intel_ring_begin(workload->req, workload->rb_len / sizeof(u32));
228 if (IS_ERR(cs)) {
229 gvt_vgpu_err("fail to alloc size =%ld shadow ring buffer\n",
230 workload->rb_len);
231 return PTR_ERR(cs);
232 }
233
234 shadow_ring_buffer_va = workload->shadow_ring_buffer_va;
235
236 /* get shadow ring buffer va */
237 workload->shadow_ring_buffer_va = cs;
238
239 memcpy(cs, shadow_ring_buffer_va,
240 workload->rb_len);
241
242 cs += workload->rb_len / sizeof(u32);
243 intel_ring_advance(workload->req, cs);
244
245 return 0;
246}
247
Chris Wilson7b302552017-11-20 13:29:58 +0000248static void release_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
fred gaoa3cfdca2017-08-18 15:41:07 +0800249{
250 if (!wa_ctx->indirect_ctx.obj)
251 return;
252
253 i915_gem_object_unpin_map(wa_ctx->indirect_ctx.obj);
254 i915_gem_object_put(wa_ctx->indirect_ctx.obj);
255}
256
Ping Gao89ea20b2017-06-29 12:22:42 +0800257/**
258 * intel_gvt_scan_and_shadow_workload - audit the workload by scanning and
259 * shadow it as well, include ringbuffer,wa_ctx and ctx.
260 * @workload: an abstract entity for each execlist submission.
261 *
262 * This function is called before the workload submitting to i915, to make
263 * sure the content of the workload is valid.
264 */
265int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload)
Zhi Wange4734052016-05-01 07:42:16 -0400266{
Zhi Wang1406a142017-09-10 21:15:18 +0800267 struct intel_vgpu *vgpu = workload->vgpu;
268 struct intel_vgpu_submission *s = &vgpu->submission;
269 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
270 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
Zhi Wange4734052016-05-01 07:42:16 -0400271 int ring_id = workload->ring_id;
fred gao0a53bc02017-08-18 15:41:06 +0800272 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
fred gao0a53bc02017-08-18 15:41:06 +0800273 struct intel_ring *ring;
Zhi Wange4734052016-05-01 07:42:16 -0400274 int ret;
275
Ping Gao87e919d2017-07-04 14:53:03 +0800276 lockdep_assert_held(&dev_priv->drm.struct_mutex);
277
Ping Gaod0302e72017-06-29 12:22:43 +0800278 if (workload->shadowed)
279 return 0;
Zhi Wange4734052016-05-01 07:42:16 -0400280
Zhenyu Wang03806ed2017-02-13 17:07:19 +0800281 shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT);
282 shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode <<
Zhi Wange4734052016-05-01 07:42:16 -0400283 GEN8_CTX_ADDRESSING_MODE_SHIFT;
284
Zhi Wang1406a142017-09-10 21:15:18 +0800285 if (!test_and_set_bit(ring_id, s->shadow_ctx_desc_updated))
Kechen Lu9dfb8e52017-08-10 07:41:36 +0800286 shadow_context_descriptor_update(shadow_ctx,
287 dev_priv->engine[ring_id]);
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800288
Ping Gao89ea20b2017-06-29 12:22:42 +0800289 ret = intel_gvt_scan_and_shadow_ringbuffer(workload);
Zhi Wangbe1da702016-05-03 18:26:57 -0400290 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800291 goto err_scan;
Zhi Wangbe1da702016-05-03 18:26:57 -0400292
Tina Zhang17f1b1a2017-03-15 23:16:01 -0400293 if ((workload->ring_id == RCS) &&
294 (workload->wa_ctx.indirect_ctx.size != 0)) {
295 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
296 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800297 goto err_scan;
Tina Zhang17f1b1a2017-03-15 23:16:01 -0400298 }
Zhi Wangbe1da702016-05-03 18:26:57 -0400299
Ping Gao89ea20b2017-06-29 12:22:42 +0800300 /* pin shadow context by gvt even the shadow context will be pinned
301 * when i915 alloc request. That is because gvt will update the guest
302 * context from shadow context when workload is completed, and at that
303 * moment, i915 may already unpined the shadow context to make the
304 * shadow_ctx pages invalid. So gvt need to pin itself. After update
305 * the guest context, gvt can unpin the shadow_ctx safely.
306 */
307 ring = engine->context_pin(engine, shadow_ctx);
308 if (IS_ERR(ring)) {
309 ret = PTR_ERR(ring);
310 gvt_vgpu_err("fail to pin shadow context\n");
fred gaoa3cfdca2017-08-18 15:41:07 +0800311 goto err_shadow;
Ping Gao89ea20b2017-06-29 12:22:42 +0800312 }
Zhi Wange4734052016-05-01 07:42:16 -0400313
fred gao0a53bc02017-08-18 15:41:06 +0800314 ret = populate_shadow_context(workload);
315 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800316 goto err_unpin;
fred gaof2880e02017-11-14 17:09:35 +0800317 workload->shadowed = true;
318 return 0;
319
320err_unpin:
321 engine->context_unpin(engine, shadow_ctx);
322err_shadow:
323 release_shadow_wa_ctx(&workload->wa_ctx);
324err_scan:
325 return ret;
326}
327
328static int intel_gvt_generate_request(struct intel_vgpu_workload *workload)
329{
330 int ring_id = workload->ring_id;
331 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
332 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
333 struct drm_i915_gem_request *rq;
334 struct intel_vgpu *vgpu = workload->vgpu;
335 struct intel_vgpu_submission *s = &vgpu->submission;
336 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
337 int ret;
fred gao0a53bc02017-08-18 15:41:06 +0800338
339 rq = i915_gem_request_alloc(dev_priv->engine[ring_id], shadow_ctx);
340 if (IS_ERR(rq)) {
341 gvt_vgpu_err("fail to allocate gem request\n");
342 ret = PTR_ERR(rq);
fred gaoa3cfdca2017-08-18 15:41:07 +0800343 goto err_unpin;
fred gao0a53bc02017-08-18 15:41:06 +0800344 }
345
346 gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq);
347
348 workload->req = i915_gem_request_get(rq);
349 ret = copy_workload_to_ring_buffer(workload);
350 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800351 goto err_unpin;
fred gaoa3cfdca2017-08-18 15:41:07 +0800352 return 0;
fred gao0a53bc02017-08-18 15:41:06 +0800353
fred gaoa3cfdca2017-08-18 15:41:07 +0800354err_unpin:
355 engine->context_unpin(engine, shadow_ctx);
fred gaoa3cfdca2017-08-18 15:41:07 +0800356 release_shadow_wa_ctx(&workload->wa_ctx);
fred gao0a53bc02017-08-18 15:41:06 +0800357 return ret;
358}
359
Zhi Wangf52c3802017-09-24 21:53:03 +0800360static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload);
361
Zhi Wangd8235b52017-09-12 22:06:39 +0800362static int prepare_shadow_batch_buffer(struct intel_vgpu_workload *workload)
363{
364 struct intel_gvt *gvt = workload->vgpu->gvt;
365 const int gmadr_bytes = gvt->device_info.gmadr_bytes_in_cmd;
Zhi Wangf52c3802017-09-24 21:53:03 +0800366 struct intel_vgpu_shadow_bb *bb;
367 int ret;
Zhi Wangd8235b52017-09-12 22:06:39 +0800368
Zhi Wangf52c3802017-09-24 21:53:03 +0800369 list_for_each_entry(bb, &workload->shadow_bb, list) {
370 bb->vma = i915_gem_object_ggtt_pin(bb->obj, NULL, 0, 0, 0);
371 if (IS_ERR(bb->vma)) {
372 ret = PTR_ERR(bb->vma);
373 goto err;
374 }
Zhi Wangd8235b52017-09-12 22:06:39 +0800375
Zhi Wangf52c3802017-09-24 21:53:03 +0800376 /* relocate shadow batch buffer */
377 bb->bb_start_cmd_va[1] = i915_ggtt_offset(bb->vma);
Zhi Wangd8235b52017-09-12 22:06:39 +0800378 if (gmadr_bytes == 8)
Zhi Wangf52c3802017-09-24 21:53:03 +0800379 bb->bb_start_cmd_va[2] = 0;
380
381 /* No one is going to touch shadow bb from now on. */
382 if (bb->clflush & CLFLUSH_AFTER) {
383 drm_clflush_virt_range(bb->va, bb->obj->base.size);
384 bb->clflush &= ~CLFLUSH_AFTER;
385 }
386
387 ret = i915_gem_object_set_to_gtt_domain(bb->obj, false);
388 if (ret)
389 goto err;
390
391 i915_gem_obj_finish_shmem_access(bb->obj);
392 bb->accessing = false;
393
394 i915_vma_move_to_active(bb->vma, workload->req, 0);
Zhi Wangd8235b52017-09-12 22:06:39 +0800395 }
396 return 0;
Zhi Wangf52c3802017-09-24 21:53:03 +0800397err:
398 release_shadow_batch_buffer(workload);
399 return ret;
Zhi Wangd8235b52017-09-12 22:06:39 +0800400}
401
402static int update_wa_ctx_2_shadow_ctx(struct intel_shadow_wa_ctx *wa_ctx)
403{
404 struct intel_vgpu_workload *workload = container_of(wa_ctx,
405 struct intel_vgpu_workload,
406 wa_ctx);
407 int ring_id = workload->ring_id;
408 struct intel_vgpu_submission *s = &workload->vgpu->submission;
409 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
410 struct drm_i915_gem_object *ctx_obj =
411 shadow_ctx->engine[ring_id].state->obj;
412 struct execlist_ring_context *shadow_ring_context;
413 struct page *page;
414
415 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
416 shadow_ring_context = kmap_atomic(page);
417
418 shadow_ring_context->bb_per_ctx_ptr.val =
419 (shadow_ring_context->bb_per_ctx_ptr.val &
420 (~PER_CTX_ADDR_MASK)) | wa_ctx->per_ctx.shadow_gma;
421 shadow_ring_context->rcs_indirect_ctx.val =
422 (shadow_ring_context->rcs_indirect_ctx.val &
423 (~INDIRECT_CTX_ADDR_MASK)) | wa_ctx->indirect_ctx.shadow_gma;
424
425 kunmap_atomic(shadow_ring_context);
426 return 0;
427}
428
429static int prepare_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
430{
431 struct i915_vma *vma;
432 unsigned char *per_ctx_va =
433 (unsigned char *)wa_ctx->indirect_ctx.shadow_va +
434 wa_ctx->indirect_ctx.size;
435
436 if (wa_ctx->indirect_ctx.size == 0)
437 return 0;
438
439 vma = i915_gem_object_ggtt_pin(wa_ctx->indirect_ctx.obj, NULL,
440 0, CACHELINE_BYTES, 0);
441 if (IS_ERR(vma))
442 return PTR_ERR(vma);
443
444 /* FIXME: we are not tracking our pinned VMA leaving it
445 * up to the core to fix up the stray pin_count upon
446 * free.
447 */
448
449 wa_ctx->indirect_ctx.shadow_gma = i915_ggtt_offset(vma);
450
451 wa_ctx->per_ctx.shadow_gma = *((unsigned int *)per_ctx_va + 1);
452 memset(per_ctx_va, 0, CACHELINE_BYTES);
453
454 update_wa_ctx_2_shadow_ctx(wa_ctx);
455 return 0;
456}
457
458static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload)
459{
Zhi Wangf52c3802017-09-24 21:53:03 +0800460 struct intel_vgpu *vgpu = workload->vgpu;
461 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
462 struct intel_vgpu_shadow_bb *bb, *pos;
Zhi Wangd8235b52017-09-12 22:06:39 +0800463
Zhi Wangf52c3802017-09-24 21:53:03 +0800464 if (list_empty(&workload->shadow_bb))
465 return;
466
467 bb = list_first_entry(&workload->shadow_bb,
468 struct intel_vgpu_shadow_bb, list);
469
470 mutex_lock(&dev_priv->drm.struct_mutex);
471
472 list_for_each_entry_safe(bb, pos, &workload->shadow_bb, list) {
473 if (bb->obj) {
474 if (bb->accessing)
475 i915_gem_obj_finish_shmem_access(bb->obj);
476
477 if (bb->va && !IS_ERR(bb->va))
478 i915_gem_object_unpin_map(bb->obj);
479
480 if (bb->vma && !IS_ERR(bb->vma)) {
481 i915_vma_unpin(bb->vma);
482 i915_vma_close(bb->vma);
483 }
484 __i915_gem_object_release_unless_active(bb->obj);
Zhi Wangd8235b52017-09-12 22:06:39 +0800485 }
Zhi Wangf52c3802017-09-24 21:53:03 +0800486 list_del(&bb->list);
487 kfree(bb);
Zhi Wangd8235b52017-09-12 22:06:39 +0800488 }
Zhi Wangf52c3802017-09-24 21:53:03 +0800489
490 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wangd8235b52017-09-12 22:06:39 +0800491}
492
Zhi Wang497aa3f2017-09-12 21:51:10 +0800493static int prepare_workload(struct intel_vgpu_workload *workload)
494{
Zhi Wangd8235b52017-09-12 22:06:39 +0800495 struct intel_vgpu *vgpu = workload->vgpu;
Zhi Wang497aa3f2017-09-12 21:51:10 +0800496 int ret = 0;
497
Zhi Wangd8235b52017-09-12 22:06:39 +0800498 ret = intel_vgpu_pin_mm(workload->shadow_mm);
499 if (ret) {
500 gvt_vgpu_err("fail to vgpu pin mm\n");
501 return ret;
502 }
Zhi Wang497aa3f2017-09-12 21:51:10 +0800503
Zhi Wangd8235b52017-09-12 22:06:39 +0800504 ret = intel_vgpu_sync_oos_pages(workload->vgpu);
505 if (ret) {
506 gvt_vgpu_err("fail to vgpu sync oos pages\n");
507 goto err_unpin_mm;
508 }
509
510 ret = intel_vgpu_flush_post_shadow(workload->vgpu);
511 if (ret) {
512 gvt_vgpu_err("fail to flush post shadow\n");
513 goto err_unpin_mm;
514 }
515
fred gaof2880e02017-11-14 17:09:35 +0800516 ret = intel_gvt_generate_request(workload);
517 if (ret) {
518 gvt_vgpu_err("fail to generate request\n");
519 goto err_unpin_mm;
520 }
521
Zhi Wangd8235b52017-09-12 22:06:39 +0800522 ret = prepare_shadow_batch_buffer(workload);
523 if (ret) {
524 gvt_vgpu_err("fail to prepare_shadow_batch_buffer\n");
525 goto err_unpin_mm;
526 }
527
528 ret = prepare_shadow_wa_ctx(&workload->wa_ctx);
529 if (ret) {
530 gvt_vgpu_err("fail to prepare_shadow_wa_ctx\n");
531 goto err_shadow_batch;
532 }
533
534 if (workload->prepare) {
535 ret = workload->prepare(workload);
536 if (ret)
537 goto err_shadow_wa_ctx;
538 }
539
540 return 0;
541err_shadow_wa_ctx:
542 release_shadow_wa_ctx(&workload->wa_ctx);
543err_shadow_batch:
544 release_shadow_batch_buffer(workload);
545err_unpin_mm:
546 intel_vgpu_unpin_mm(workload->shadow_mm);
Zhi Wang497aa3f2017-09-12 21:51:10 +0800547 return ret;
548}
549
fred gao0a53bc02017-08-18 15:41:06 +0800550static int dispatch_workload(struct intel_vgpu_workload *workload)
551{
Zhi Wang1406a142017-09-10 21:15:18 +0800552 struct intel_vgpu *vgpu = workload->vgpu;
553 struct intel_vgpu_submission *s = &vgpu->submission;
554 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
555 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
fred gao0a53bc02017-08-18 15:41:06 +0800556 int ring_id = workload->ring_id;
fred gao0a53bc02017-08-18 15:41:06 +0800557 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
558 int ret = 0;
559
560 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
561 ring_id, workload);
562
563 mutex_lock(&dev_priv->drm.struct_mutex);
564
565 ret = intel_gvt_scan_and_shadow_workload(workload);
566 if (ret)
567 goto out;
568
Zhi Wang497aa3f2017-09-12 21:51:10 +0800569 ret = prepare_workload(workload);
570 if (ret) {
571 engine->context_unpin(engine, shadow_ctx);
572 goto out;
fred gao0a53bc02017-08-18 15:41:06 +0800573 }
574
Pei Zhang90d27a12016-11-14 18:02:57 +0800575out:
576 if (ret)
577 workload->status = ret;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800578
Ping Gao89ea20b2017-06-29 12:22:42 +0800579 if (!IS_ERR_OR_NULL(workload->req)) {
580 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
581 ring_id, workload->req);
582 i915_add_request(workload->req);
583 workload->dispatched = true;
584 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800585
Pei Zhang90d27a12016-11-14 18:02:57 +0800586 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400587 return ret;
588}
589
590static struct intel_vgpu_workload *pick_next_workload(
591 struct intel_gvt *gvt, int ring_id)
592{
593 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
594 struct intel_vgpu_workload *workload = NULL;
595
596 mutex_lock(&gvt->lock);
597
598 /*
599 * no current vgpu / will be scheduled out / no workload
600 * bail out
601 */
602 if (!scheduler->current_vgpu) {
603 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
604 goto out;
605 }
606
607 if (scheduler->need_reschedule) {
608 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
609 goto out;
610 }
611
Zhenyu Wang954180a2017-04-12 14:22:50 +0800612 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id)))
Zhi Wange4734052016-05-01 07:42:16 -0400613 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400614
615 /*
616 * still have current workload, maybe the workload disptacher
617 * fail to submit it for some reason, resubmit it.
618 */
619 if (scheduler->current_workload[ring_id]) {
620 workload = scheduler->current_workload[ring_id];
621 gvt_dbg_sched("ring id %d still have current workload %p\n",
622 ring_id, workload);
623 goto out;
624 }
625
626 /*
627 * pick a workload as current workload
628 * once current workload is set, schedule policy routines
629 * will wait the current workload is finished when trying to
630 * schedule out a vgpu.
631 */
632 scheduler->current_workload[ring_id] = container_of(
633 workload_q_head(scheduler->current_vgpu, ring_id)->next,
634 struct intel_vgpu_workload, list);
635
636 workload = scheduler->current_workload[ring_id];
637
638 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
639
Zhi Wang1406a142017-09-10 21:15:18 +0800640 atomic_inc(&workload->vgpu->submission.running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400641out:
642 mutex_unlock(&gvt->lock);
643 return workload;
644}
645
646static void update_guest_context(struct intel_vgpu_workload *workload)
647{
648 struct intel_vgpu *vgpu = workload->vgpu;
649 struct intel_gvt *gvt = vgpu->gvt;
Zhi Wang1406a142017-09-10 21:15:18 +0800650 struct intel_vgpu_submission *s = &vgpu->submission;
651 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
Zhi Wange4734052016-05-01 07:42:16 -0400652 int ring_id = workload->ring_id;
Zhi Wange4734052016-05-01 07:42:16 -0400653 struct drm_i915_gem_object *ctx_obj =
654 shadow_ctx->engine[ring_id].state->obj;
655 struct execlist_ring_context *shadow_ring_context;
656 struct page *page;
657 void *src;
658 unsigned long context_gpa, context_page_num;
659 int i;
660
661 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
662 workload->ctx_desc.lrca);
663
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300664 context_page_num = gvt->dev_priv->engine[ring_id]->context_size;
Zhi Wange4734052016-05-01 07:42:16 -0400665
666 context_page_num = context_page_num >> PAGE_SHIFT;
667
668 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
669 context_page_num = 19;
670
671 i = 2;
672
673 while (i < context_page_num) {
674 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
675 (u32)((workload->ctx_desc.lrca + i) <<
Zhi Wang9556e112017-10-10 13:51:32 +0800676 I915_GTT_PAGE_SHIFT));
Zhi Wange4734052016-05-01 07:42:16 -0400677 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500678 gvt_vgpu_err("invalid guest context descriptor\n");
Zhi Wange4734052016-05-01 07:42:16 -0400679 return;
680 }
681
Michel Thierry0b29c752017-09-13 09:56:00 +0100682 page = i915_gem_object_get_page(ctx_obj, LRC_HEADER_PAGES + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800683 src = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400684 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
Zhi Wang9556e112017-10-10 13:51:32 +0800685 I915_GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800686 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400687 i++;
688 }
689
690 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
691 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
692
693 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800694 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400695
696#define COPY_REG(name) \
697 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
698 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
699
700 COPY_REG(ctx_ctrl);
701 COPY_REG(ctx_timestamp);
702
703#undef COPY_REG
704
705 intel_gvt_hypervisor_write_gpa(vgpu,
706 workload->ring_context_gpa +
707 sizeof(*shadow_ring_context),
708 (void *)shadow_ring_context +
709 sizeof(*shadow_ring_context),
Zhi Wang9556e112017-10-10 13:51:32 +0800710 I915_GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
Zhi Wange4734052016-05-01 07:42:16 -0400711
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800712 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400713}
714
Zhi Wange2c43c02017-09-13 01:58:35 +0800715static void clean_workloads(struct intel_vgpu *vgpu, unsigned long engine_mask)
716{
717 struct intel_vgpu_submission *s = &vgpu->submission;
718 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
719 struct intel_engine_cs *engine;
720 struct intel_vgpu_workload *pos, *n;
721 unsigned int tmp;
722
723 /* free the unsubmited workloads in the queues. */
724 for_each_engine_masked(engine, dev_priv, engine_mask, tmp) {
725 list_for_each_entry_safe(pos, n,
726 &s->workload_q_head[engine->id], list) {
727 list_del_init(&pos->list);
728 intel_vgpu_destroy_workload(pos);
729 }
730 clear_bit(engine->id, s->shadow_ctx_desc_updated);
731 }
732}
733
Zhi Wange4734052016-05-01 07:42:16 -0400734static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
735{
736 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Zhi Wang1406a142017-09-10 21:15:18 +0800737 struct intel_vgpu_workload *workload =
738 scheduler->current_workload[ring_id];
739 struct intel_vgpu *vgpu = workload->vgpu;
740 struct intel_vgpu_submission *s = &vgpu->submission;
Zhi Wangbe1da702016-05-03 18:26:57 -0400741 int event;
Zhi Wange4734052016-05-01 07:42:16 -0400742
743 mutex_lock(&gvt->lock);
744
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800745 /* For the workload w/ request, needs to wait for the context
746 * switch to make sure request is completed.
747 * For the workload w/o request, directly complete the workload.
748 */
749 if (workload->req) {
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800750 struct drm_i915_private *dev_priv =
751 workload->vgpu->gvt->dev_priv;
752 struct intel_engine_cs *engine =
753 dev_priv->engine[workload->ring_id];
Zhi Wange4734052016-05-01 07:42:16 -0400754 wait_event(workload->shadow_ctx_status_wq,
755 !atomic_read(&workload->shadow_ctx_active));
756
Chuanxiao Dong0cf5ec42017-06-23 13:01:11 +0800757 /* If this request caused GPU hang, req->fence.error will
758 * be set to -EIO. Use -EIO to set workload status so
759 * that when this request caused GPU hang, didn't trigger
760 * context switch interrupt to guest.
761 */
762 if (likely(workload->status == -EINPROGRESS)) {
763 if (workload->req->fence.error == -EIO)
764 workload->status = -EIO;
765 else
766 workload->status = 0;
767 }
768
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800769 i915_gem_request_put(fetch_and_zero(&workload->req));
Zhi Wangbe1da702016-05-03 18:26:57 -0400770
Chuanxiao Dong6184cc82017-08-01 17:47:25 +0800771 if (!workload->status && !(vgpu->resetting_eng &
772 ENGINE_MASK(ring_id))) {
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800773 update_guest_context(workload);
774
775 for_each_set_bit(event, workload->pending_events,
776 INTEL_GVT_EVENT_MAX)
777 intel_vgpu_trigger_virtual_event(vgpu, event);
778 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800779 mutex_lock(&dev_priv->drm.struct_mutex);
780 /* unpin shadow ctx as the shadow_ctx update is done */
Zhi Wang1406a142017-09-10 21:15:18 +0800781 engine->context_unpin(engine, s->shadow_ctx);
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800782 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400783 }
784
785 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
786 ring_id, workload, workload->status);
787
788 scheduler->current_workload[ring_id] = NULL;
789
Zhi Wange4734052016-05-01 07:42:16 -0400790 list_del_init(&workload->list);
Zhi Wangd8235b52017-09-12 22:06:39 +0800791
792 if (!workload->status) {
793 release_shadow_batch_buffer(workload);
794 release_shadow_wa_ctx(&workload->wa_ctx);
795 }
796
Zhi Wange2c43c02017-09-13 01:58:35 +0800797 if (workload->status || (vgpu->resetting_eng & ENGINE_MASK(ring_id))) {
798 /* if workload->status is not successful means HW GPU
799 * has occurred GPU hang or something wrong with i915/GVT,
800 * and GVT won't inject context switch interrupt to guest.
801 * So this error is a vGPU hang actually to the guest.
802 * According to this we should emunlate a vGPU hang. If
803 * there are pending workloads which are already submitted
804 * from guest, we should clean them up like HW GPU does.
805 *
806 * if it is in middle of engine resetting, the pending
807 * workloads won't be submitted to HW GPU and will be
808 * cleaned up during the resetting process later, so doing
809 * the workload clean up here doesn't have any impact.
810 **/
811 clean_workloads(vgpu, ENGINE_MASK(ring_id));
812 }
813
Zhi Wange4734052016-05-01 07:42:16 -0400814 workload->complete(workload);
815
Zhi Wang1406a142017-09-10 21:15:18 +0800816 atomic_dec(&s->running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400817 wake_up(&scheduler->workload_complete_wq);
Ping Gaof100dae2017-05-24 09:14:11 +0800818
819 if (gvt->scheduler.need_reschedule)
820 intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EVENT_SCHED);
821
Zhi Wange4734052016-05-01 07:42:16 -0400822 mutex_unlock(&gvt->lock);
823}
824
825struct workload_thread_param {
826 struct intel_gvt *gvt;
827 int ring_id;
828};
829
830static int workload_thread(void *priv)
831{
832 struct workload_thread_param *p = (struct workload_thread_param *)priv;
833 struct intel_gvt *gvt = p->gvt;
834 int ring_id = p->ring_id;
835 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
836 struct intel_vgpu_workload *workload = NULL;
Tina Zhang695fbc02017-03-10 04:26:53 -0500837 struct intel_vgpu *vgpu = NULL;
Zhi Wange4734052016-05-01 07:42:16 -0400838 int ret;
Xu Hane3476c02017-03-29 10:13:59 +0800839 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv)
840 || IS_KABYLAKE(gvt->dev_priv);
Du, Changbine45d7b72016-10-27 11:10:31 +0800841 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Zhi Wange4734052016-05-01 07:42:16 -0400842
843 kfree(p);
844
845 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
846
847 while (!kthread_should_stop()) {
Du, Changbine45d7b72016-10-27 11:10:31 +0800848 add_wait_queue(&scheduler->waitq[ring_id], &wait);
849 do {
850 workload = pick_next_workload(gvt, ring_id);
851 if (workload)
852 break;
853 wait_woken(&wait, TASK_INTERRUPTIBLE,
854 MAX_SCHEDULE_TIMEOUT);
855 } while (!kthread_should_stop());
856 remove_wait_queue(&scheduler->waitq[ring_id], &wait);
Zhi Wange4734052016-05-01 07:42:16 -0400857
Du, Changbine45d7b72016-10-27 11:10:31 +0800858 if (!workload)
Zhi Wange4734052016-05-01 07:42:16 -0400859 break;
860
861 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
862 workload->ring_id, workload,
863 workload->vgpu->id);
864
865 intel_runtime_pm_get(gvt->dev_priv);
866
Zhi Wange4734052016-05-01 07:42:16 -0400867 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
868 workload->ring_id, workload);
869
870 if (need_force_wake)
871 intel_uncore_forcewake_get(gvt->dev_priv,
872 FORCEWAKE_ALL);
873
Pei Zhang90d27a12016-11-14 18:02:57 +0800874 mutex_lock(&gvt->lock);
Zhi Wange4734052016-05-01 07:42:16 -0400875 ret = dispatch_workload(workload);
Pei Zhang90d27a12016-11-14 18:02:57 +0800876 mutex_unlock(&gvt->lock);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100877
Zhi Wange4734052016-05-01 07:42:16 -0400878 if (ret) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500879 vgpu = workload->vgpu;
880 gvt_vgpu_err("fail to dispatch workload, skip\n");
Zhi Wange4734052016-05-01 07:42:16 -0400881 goto complete;
882 }
883
884 gvt_dbg_sched("ring id %d wait workload %p\n",
885 workload->ring_id, workload);
Chris Wilson3dce2ac2017-03-08 22:08:08 +0000886 i915_wait_request(workload->req, 0, MAX_SCHEDULE_TIMEOUT);
Zhi Wange4734052016-05-01 07:42:16 -0400887
888complete:
Changbin Du3ce32742017-02-09 10:13:16 +0800889 gvt_dbg_sched("will complete workload %p, status: %d\n",
Zhi Wange4734052016-05-01 07:42:16 -0400890 workload, workload->status);
891
Changbin Du2e51ef32017-01-05 13:28:05 +0800892 complete_current_workload(gvt, ring_id);
893
Zhi Wange4734052016-05-01 07:42:16 -0400894 if (need_force_wake)
895 intel_uncore_forcewake_put(gvt->dev_priv,
896 FORCEWAKE_ALL);
897
Zhi Wange4734052016-05-01 07:42:16 -0400898 intel_runtime_pm_put(gvt->dev_priv);
Zhi Wang6d763032017-09-12 22:33:12 +0800899 if (ret && (vgpu_is_vm_unhealthy(ret)))
fred gaoe011c6c2017-09-19 15:11:28 +0800900 enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR);
Zhi Wange4734052016-05-01 07:42:16 -0400901 }
902 return 0;
903}
904
905void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
906{
Zhi Wang1406a142017-09-10 21:15:18 +0800907 struct intel_vgpu_submission *s = &vgpu->submission;
Zhi Wange4734052016-05-01 07:42:16 -0400908 struct intel_gvt *gvt = vgpu->gvt;
909 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
910
Zhi Wang1406a142017-09-10 21:15:18 +0800911 if (atomic_read(&s->running_workload_num)) {
Zhi Wange4734052016-05-01 07:42:16 -0400912 gvt_dbg_sched("wait vgpu idle\n");
913
914 wait_event(scheduler->workload_complete_wq,
Zhi Wang1406a142017-09-10 21:15:18 +0800915 !atomic_read(&s->running_workload_num));
Zhi Wange4734052016-05-01 07:42:16 -0400916 }
917}
918
919void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
920{
921 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Changbin Du3fc03062017-03-13 10:47:11 +0800922 struct intel_engine_cs *engine;
923 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400924
925 gvt_dbg_core("clean workload scheduler\n");
926
Changbin Du3fc03062017-03-13 10:47:11 +0800927 for_each_engine(engine, gvt->dev_priv, i) {
928 atomic_notifier_chain_unregister(
929 &engine->context_status_notifier,
930 &gvt->shadow_ctx_notifier_block[i]);
931 kthread_stop(scheduler->thread[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400932 }
933}
934
935int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
936{
937 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
938 struct workload_thread_param *param = NULL;
Changbin Du3fc03062017-03-13 10:47:11 +0800939 struct intel_engine_cs *engine;
940 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400941 int ret;
Zhi Wange4734052016-05-01 07:42:16 -0400942
943 gvt_dbg_core("init workload scheduler\n");
944
945 init_waitqueue_head(&scheduler->workload_complete_wq);
946
Changbin Du3fc03062017-03-13 10:47:11 +0800947 for_each_engine(engine, gvt->dev_priv, i) {
Zhi Wange4734052016-05-01 07:42:16 -0400948 init_waitqueue_head(&scheduler->waitq[i]);
949
950 param = kzalloc(sizeof(*param), GFP_KERNEL);
951 if (!param) {
952 ret = -ENOMEM;
953 goto err;
954 }
955
956 param->gvt = gvt;
957 param->ring_id = i;
958
959 scheduler->thread[i] = kthread_run(workload_thread, param,
960 "gvt workload %d", i);
961 if (IS_ERR(scheduler->thread[i])) {
962 gvt_err("fail to create workload thread\n");
963 ret = PTR_ERR(scheduler->thread[i]);
964 goto err;
965 }
Changbin Du3fc03062017-03-13 10:47:11 +0800966
967 gvt->shadow_ctx_notifier_block[i].notifier_call =
968 shadow_context_status_change;
969 atomic_notifier_chain_register(&engine->context_status_notifier,
970 &gvt->shadow_ctx_notifier_block[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400971 }
972 return 0;
973err:
974 intel_gvt_clean_workload_scheduler(gvt);
975 kfree(param);
976 param = NULL;
977 return ret;
978}
979
Zhi Wang874b6a92017-09-10 20:08:18 +0800980/**
981 * intel_vgpu_clean_submission - free submission-related resource for vGPU
982 * @vgpu: a vGPU
983 *
984 * This function is called when a vGPU is being destroyed.
985 *
986 */
987void intel_vgpu_clean_submission(struct intel_vgpu *vgpu)
Zhi Wange4734052016-05-01 07:42:16 -0400988{
Zhi Wang1406a142017-09-10 21:15:18 +0800989 struct intel_vgpu_submission *s = &vgpu->submission;
990
Zhi Wangad1d3632017-09-13 00:31:29 +0800991 intel_vgpu_select_submission_ops(vgpu, 0);
Zhi Wang1406a142017-09-10 21:15:18 +0800992 i915_gem_context_put(s->shadow_ctx);
993 kmem_cache_destroy(s->workloads);
Zhi Wange4734052016-05-01 07:42:16 -0400994}
995
Zhi Wang06bb3722017-09-13 01:41:35 +0800996
997/**
998 * intel_vgpu_reset_submission - reset submission-related resource for vGPU
999 * @vgpu: a vGPU
1000 * @engine_mask: engines expected to be reset
1001 *
1002 * This function is called when a vGPU is being destroyed.
1003 *
1004 */
1005void intel_vgpu_reset_submission(struct intel_vgpu *vgpu,
1006 unsigned long engine_mask)
1007{
1008 struct intel_vgpu_submission *s = &vgpu->submission;
1009
1010 if (!s->active)
1011 return;
1012
Zhi Wange2c43c02017-09-13 01:58:35 +08001013 clean_workloads(vgpu, engine_mask);
Zhi Wang06bb3722017-09-13 01:41:35 +08001014 s->ops->reset(vgpu, engine_mask);
1015}
1016
Zhi Wang874b6a92017-09-10 20:08:18 +08001017/**
1018 * intel_vgpu_setup_submission - setup submission-related resource for vGPU
1019 * @vgpu: a vGPU
1020 *
1021 * This function is called when a vGPU is being created.
1022 *
1023 * Returns:
1024 * Zero on success, negative error code if failed.
1025 *
1026 */
1027int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)
Zhi Wange4734052016-05-01 07:42:16 -04001028{
Zhi Wang1406a142017-09-10 21:15:18 +08001029 struct intel_vgpu_submission *s = &vgpu->submission;
Zhi Wang9a9829e2017-09-10 20:28:09 +08001030 enum intel_engine_id i;
1031 struct intel_engine_cs *engine;
1032 int ret;
Zhi Wange4734052016-05-01 07:42:16 -04001033
Zhi Wang1406a142017-09-10 21:15:18 +08001034 s->shadow_ctx = i915_gem_context_create_gvt(
Zhi Wange4734052016-05-01 07:42:16 -04001035 &vgpu->gvt->dev_priv->drm);
Zhi Wang1406a142017-09-10 21:15:18 +08001036 if (IS_ERR(s->shadow_ctx))
1037 return PTR_ERR(s->shadow_ctx);
Zhi Wange4734052016-05-01 07:42:16 -04001038
Zhi Wang1406a142017-09-10 21:15:18 +08001039 bitmap_zero(s->shadow_ctx_desc_updated, I915_NUM_ENGINES);
Kechen Lu9dfb8e52017-08-10 07:41:36 +08001040
Zhi Wang1406a142017-09-10 21:15:18 +08001041 s->workloads = kmem_cache_create("gvt-g_vgpu_workload",
Zhi Wang9a9829e2017-09-10 20:28:09 +08001042 sizeof(struct intel_vgpu_workload), 0,
1043 SLAB_HWCACHE_ALIGN,
1044 NULL);
1045
Zhi Wang1406a142017-09-10 21:15:18 +08001046 if (!s->workloads) {
Zhi Wang9a9829e2017-09-10 20:28:09 +08001047 ret = -ENOMEM;
1048 goto out_shadow_ctx;
1049 }
1050
1051 for_each_engine(engine, vgpu->gvt->dev_priv, i)
Zhi Wang1406a142017-09-10 21:15:18 +08001052 INIT_LIST_HEAD(&s->workload_q_head[i]);
Zhi Wang9a9829e2017-09-10 20:28:09 +08001053
Zhi Wang1406a142017-09-10 21:15:18 +08001054 atomic_set(&s->running_workload_num, 0);
Zhi Wang91d5d852017-09-10 21:33:20 +08001055 bitmap_zero(s->tlb_handle_pending, I915_NUM_ENGINES);
Zhi Wang9a9829e2017-09-10 20:28:09 +08001056
Zhi Wange4734052016-05-01 07:42:16 -04001057 return 0;
Zhi Wang9a9829e2017-09-10 20:28:09 +08001058
1059out_shadow_ctx:
Zhi Wang1406a142017-09-10 21:15:18 +08001060 i915_gem_context_put(s->shadow_ctx);
Zhi Wang9a9829e2017-09-10 20:28:09 +08001061 return ret;
Zhi Wange4734052016-05-01 07:42:16 -04001062}
Zhi Wang21527a82017-09-12 21:42:09 +08001063
1064/**
Zhi Wangad1d3632017-09-13 00:31:29 +08001065 * intel_vgpu_select_submission_ops - select virtual submission interface
1066 * @vgpu: a vGPU
1067 * @interface: expected vGPU virtual submission interface
1068 *
1069 * This function is called when guest configures submission interface.
1070 *
1071 * Returns:
1072 * Zero on success, negative error code if failed.
1073 *
1074 */
1075int intel_vgpu_select_submission_ops(struct intel_vgpu *vgpu,
1076 unsigned int interface)
1077{
1078 struct intel_vgpu_submission *s = &vgpu->submission;
1079 const struct intel_vgpu_submission_ops *ops[] = {
1080 [INTEL_VGPU_EXECLIST_SUBMISSION] =
1081 &intel_vgpu_execlist_submission_ops,
1082 };
1083 int ret;
1084
1085 if (WARN_ON(interface >= ARRAY_SIZE(ops)))
1086 return -EINVAL;
1087
1088 if (s->active) {
1089 s->ops->clean(vgpu);
1090 s->active = false;
1091 gvt_dbg_core("vgpu%d: de-select ops [ %s ] \n",
1092 vgpu->id, s->ops->name);
1093 }
1094
1095 if (interface == 0) {
1096 s->ops = NULL;
1097 s->virtual_submission_interface = 0;
1098 gvt_dbg_core("vgpu%d: no submission ops\n", vgpu->id);
1099 return 0;
1100 }
1101
1102 ret = ops[interface]->init(vgpu);
1103 if (ret)
1104 return ret;
1105
1106 s->ops = ops[interface];
1107 s->virtual_submission_interface = interface;
1108 s->active = true;
1109
1110 gvt_dbg_core("vgpu%d: activate ops [ %s ]\n",
1111 vgpu->id, s->ops->name);
1112
1113 return 0;
1114}
1115
1116/**
Zhi Wang21527a82017-09-12 21:42:09 +08001117 * intel_vgpu_destroy_workload - destroy a vGPU workload
1118 * @vgpu: a vGPU
1119 *
1120 * This function is called when destroy a vGPU workload.
1121 *
1122 */
1123void intel_vgpu_destroy_workload(struct intel_vgpu_workload *workload)
1124{
1125 struct intel_vgpu_submission *s = &workload->vgpu->submission;
1126
1127 if (workload->shadow_mm)
1128 intel_gvt_mm_unreference(workload->shadow_mm);
1129
1130 kmem_cache_free(s->workloads, workload);
1131}
1132
Zhi Wang6d763032017-09-12 22:33:12 +08001133static struct intel_vgpu_workload *
1134alloc_workload(struct intel_vgpu *vgpu)
Zhi Wang21527a82017-09-12 21:42:09 +08001135{
1136 struct intel_vgpu_submission *s = &vgpu->submission;
1137 struct intel_vgpu_workload *workload;
1138
1139 workload = kmem_cache_zalloc(s->workloads, GFP_KERNEL);
1140 if (!workload)
1141 return ERR_PTR(-ENOMEM);
1142
1143 INIT_LIST_HEAD(&workload->list);
1144 INIT_LIST_HEAD(&workload->shadow_bb);
1145
1146 init_waitqueue_head(&workload->shadow_ctx_status_wq);
1147 atomic_set(&workload->shadow_ctx_active, 0);
1148
1149 workload->status = -EINPROGRESS;
1150 workload->shadowed = false;
1151 workload->vgpu = vgpu;
1152
1153 return workload;
1154}
Zhi Wang6d763032017-09-12 22:33:12 +08001155
1156#define RING_CTX_OFF(x) \
1157 offsetof(struct execlist_ring_context, x)
1158
1159static void read_guest_pdps(struct intel_vgpu *vgpu,
1160 u64 ring_context_gpa, u32 pdp[8])
1161{
1162 u64 gpa;
1163 int i;
1164
1165 gpa = ring_context_gpa + RING_CTX_OFF(pdp3_UDW.val);
1166
1167 for (i = 0; i < 8; i++)
1168 intel_gvt_hypervisor_read_gpa(vgpu,
1169 gpa + i * 8, &pdp[7 - i], 4);
1170}
1171
1172static int prepare_mm(struct intel_vgpu_workload *workload)
1173{
1174 struct execlist_ctx_descriptor_format *desc = &workload->ctx_desc;
1175 struct intel_vgpu_mm *mm;
1176 struct intel_vgpu *vgpu = workload->vgpu;
1177 int page_table_level;
1178 u32 pdp[8];
1179
1180 if (desc->addressing_mode == 1) { /* legacy 32-bit */
1181 page_table_level = 3;
1182 } else if (desc->addressing_mode == 3) { /* legacy 64 bit */
1183 page_table_level = 4;
1184 } else {
1185 gvt_vgpu_err("Advanced Context mode(SVM) is not supported!\n");
1186 return -EINVAL;
1187 }
1188
1189 read_guest_pdps(workload->vgpu, workload->ring_context_gpa, pdp);
1190
1191 mm = intel_vgpu_find_ppgtt_mm(workload->vgpu, page_table_level, pdp);
1192 if (mm) {
1193 intel_gvt_mm_reference(mm);
1194 } else {
1195
1196 mm = intel_vgpu_create_mm(workload->vgpu, INTEL_GVT_MM_PPGTT,
1197 pdp, page_table_level, 0);
1198 if (IS_ERR(mm)) {
1199 gvt_vgpu_err("fail to create mm object.\n");
1200 return PTR_ERR(mm);
1201 }
1202 }
1203 workload->shadow_mm = mm;
1204 return 0;
1205}
1206
1207#define same_context(a, b) (((a)->context_id == (b)->context_id) && \
1208 ((a)->lrca == (b)->lrca))
1209
1210#define get_last_workload(q) \
1211 (list_empty(q) ? NULL : container_of(q->prev, \
1212 struct intel_vgpu_workload, list))
1213/**
1214 * intel_vgpu_create_workload - create a vGPU workload
1215 * @vgpu: a vGPU
1216 * @desc: a guest context descriptor
1217 *
1218 * This function is called when creating a vGPU workload.
1219 *
1220 * Returns:
1221 * struct intel_vgpu_workload * on success, negative error code in
1222 * pointer if failed.
1223 *
1224 */
1225struct intel_vgpu_workload *
1226intel_vgpu_create_workload(struct intel_vgpu *vgpu, int ring_id,
1227 struct execlist_ctx_descriptor_format *desc)
1228{
1229 struct intel_vgpu_submission *s = &vgpu->submission;
1230 struct list_head *q = workload_q_head(vgpu, ring_id);
1231 struct intel_vgpu_workload *last_workload = get_last_workload(q);
1232 struct intel_vgpu_workload *workload = NULL;
1233 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
1234 u64 ring_context_gpa;
1235 u32 head, tail, start, ctl, ctx_ctl, per_ctx, indirect_ctx;
1236 int ret;
1237
1238 ring_context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
Zhi Wang9556e112017-10-10 13:51:32 +08001239 (u32)((desc->lrca + 1) << I915_GTT_PAGE_SHIFT));
Zhi Wang6d763032017-09-12 22:33:12 +08001240 if (ring_context_gpa == INTEL_GVT_INVALID_ADDR) {
1241 gvt_vgpu_err("invalid guest context LRCA: %x\n", desc->lrca);
1242 return ERR_PTR(-EINVAL);
1243 }
1244
1245 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1246 RING_CTX_OFF(ring_header.val), &head, 4);
1247
1248 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1249 RING_CTX_OFF(ring_tail.val), &tail, 4);
1250
1251 head &= RB_HEAD_OFF_MASK;
1252 tail &= RB_TAIL_OFF_MASK;
1253
1254 if (last_workload && same_context(&last_workload->ctx_desc, desc)) {
1255 gvt_dbg_el("ring id %d cur workload == last\n", ring_id);
1256 gvt_dbg_el("ctx head %x real head %lx\n", head,
1257 last_workload->rb_tail);
1258 /*
1259 * cannot use guest context head pointer here,
1260 * as it might not be updated at this time
1261 */
1262 head = last_workload->rb_tail;
1263 }
1264
1265 gvt_dbg_el("ring id %d begin a new workload\n", ring_id);
1266
1267 /* record some ring buffer register values for scan and shadow */
1268 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1269 RING_CTX_OFF(rb_start.val), &start, 4);
1270 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1271 RING_CTX_OFF(rb_ctrl.val), &ctl, 4);
1272 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1273 RING_CTX_OFF(ctx_ctrl.val), &ctx_ctl, 4);
1274
1275 workload = alloc_workload(vgpu);
1276 if (IS_ERR(workload))
1277 return workload;
1278
1279 workload->ring_id = ring_id;
1280 workload->ctx_desc = *desc;
1281 workload->ring_context_gpa = ring_context_gpa;
1282 workload->rb_head = head;
1283 workload->rb_tail = tail;
1284 workload->rb_start = start;
1285 workload->rb_ctl = ctl;
1286
1287 if (ring_id == RCS) {
1288 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1289 RING_CTX_OFF(bb_per_ctx_ptr.val), &per_ctx, 4);
1290 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1291 RING_CTX_OFF(rcs_indirect_ctx.val), &indirect_ctx, 4);
1292
1293 workload->wa_ctx.indirect_ctx.guest_gma =
1294 indirect_ctx & INDIRECT_CTX_ADDR_MASK;
1295 workload->wa_ctx.indirect_ctx.size =
1296 (indirect_ctx & INDIRECT_CTX_SIZE_MASK) *
1297 CACHELINE_BYTES;
1298 workload->wa_ctx.per_ctx.guest_gma =
1299 per_ctx & PER_CTX_ADDR_MASK;
1300 workload->wa_ctx.per_ctx.valid = per_ctx & 1;
1301 }
1302
1303 gvt_dbg_el("workload %p ring id %d head %x tail %x start %x ctl %x\n",
1304 workload, ring_id, head, tail, start, ctl);
1305
1306 ret = prepare_mm(workload);
1307 if (ret) {
1308 kmem_cache_free(s->workloads, workload);
1309 return ERR_PTR(ret);
1310 }
1311
1312 /* Only scan and shadow the first workload in the queue
1313 * as there is only one pre-allocated buf-obj for shadow.
1314 */
1315 if (list_empty(workload_q_head(vgpu, ring_id))) {
1316 intel_runtime_pm_get(dev_priv);
1317 mutex_lock(&dev_priv->drm.struct_mutex);
1318 ret = intel_gvt_scan_and_shadow_workload(workload);
1319 mutex_unlock(&dev_priv->drm.struct_mutex);
1320 intel_runtime_pm_put(dev_priv);
1321 }
1322
1323 if (ret && (vgpu_is_vm_unhealthy(ret))) {
1324 enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR);
1325 intel_vgpu_destroy_workload(workload);
1326 return ERR_PTR(ret);
1327 }
1328
1329 return workload;
1330}