xref: /openbmc/linux/drivers/gpu/drm/i915/gvt/scheduler.c (revision c8ec3743)
1 /*
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 
36 #include <linux/kthread.h>
37 
38 #include "i915_drv.h"
39 #include "gvt.h"
40 
41 #define RING_CTX_OFF(x) \
42 	offsetof(struct execlist_ring_context, x)
43 
44 static void set_context_pdp_root_pointer(
45 		struct execlist_ring_context *ring_context,
46 		u32 pdp[8])
47 {
48 	int i;
49 
50 	for (i = 0; i < 8; i++)
51 		ring_context->pdps[i].val = pdp[7 - i];
52 }
53 
54 static void update_shadow_pdps(struct intel_vgpu_workload *workload)
55 {
56 	struct drm_i915_gem_object *ctx_obj =
57 		workload->req->hw_context->state->obj;
58 	struct execlist_ring_context *shadow_ring_context;
59 	struct page *page;
60 
61 	if (WARN_ON(!workload->shadow_mm))
62 		return;
63 
64 	if (WARN_ON(!atomic_read(&workload->shadow_mm->pincount)))
65 		return;
66 
67 	page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
68 	shadow_ring_context = kmap(page);
69 	set_context_pdp_root_pointer(shadow_ring_context,
70 			(void *)workload->shadow_mm->ppgtt_mm.shadow_pdps);
71 	kunmap(page);
72 }
73 
74 /*
75  * when populating shadow ctx from guest, we should not overrride oa related
76  * registers, so that they will not be overlapped by guest oa configs. Thus
77  * made it possible to capture oa data from host for both host and guests.
78  */
79 static void sr_oa_regs(struct intel_vgpu_workload *workload,
80 		u32 *reg_state, bool save)
81 {
82 	struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
83 	u32 ctx_oactxctrl = dev_priv->perf.oa.ctx_oactxctrl_offset;
84 	u32 ctx_flexeu0 = dev_priv->perf.oa.ctx_flexeu0_offset;
85 	int i = 0;
86 	u32 flex_mmio[] = {
87 		i915_mmio_reg_offset(EU_PERF_CNTL0),
88 		i915_mmio_reg_offset(EU_PERF_CNTL1),
89 		i915_mmio_reg_offset(EU_PERF_CNTL2),
90 		i915_mmio_reg_offset(EU_PERF_CNTL3),
91 		i915_mmio_reg_offset(EU_PERF_CNTL4),
92 		i915_mmio_reg_offset(EU_PERF_CNTL5),
93 		i915_mmio_reg_offset(EU_PERF_CNTL6),
94 	};
95 
96 	if (workload->ring_id != RCS)
97 		return;
98 
99 	if (save) {
100 		workload->oactxctrl = reg_state[ctx_oactxctrl + 1];
101 
102 		for (i = 0; i < ARRAY_SIZE(workload->flex_mmio); i++) {
103 			u32 state_offset = ctx_flexeu0 + i * 2;
104 
105 			workload->flex_mmio[i] = reg_state[state_offset + 1];
106 		}
107 	} else {
108 		reg_state[ctx_oactxctrl] =
109 			i915_mmio_reg_offset(GEN8_OACTXCONTROL);
110 		reg_state[ctx_oactxctrl + 1] = workload->oactxctrl;
111 
112 		for (i = 0; i < ARRAY_SIZE(workload->flex_mmio); i++) {
113 			u32 state_offset = ctx_flexeu0 + i * 2;
114 			u32 mmio = flex_mmio[i];
115 
116 			reg_state[state_offset] = mmio;
117 			reg_state[state_offset + 1] = workload->flex_mmio[i];
118 		}
119 	}
120 }
121 
122 static int populate_shadow_context(struct intel_vgpu_workload *workload)
123 {
124 	struct intel_vgpu *vgpu = workload->vgpu;
125 	struct intel_gvt *gvt = vgpu->gvt;
126 	int ring_id = workload->ring_id;
127 	struct drm_i915_gem_object *ctx_obj =
128 		workload->req->hw_context->state->obj;
129 	struct execlist_ring_context *shadow_ring_context;
130 	struct page *page;
131 	void *dst;
132 	unsigned long context_gpa, context_page_num;
133 	int i;
134 
135 	page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
136 	shadow_ring_context = kmap(page);
137 
138 	sr_oa_regs(workload, (u32 *)shadow_ring_context, true);
139 #define COPY_REG(name) \
140 	intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \
141 		+ RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
142 #define COPY_REG_MASKED(name) {\
143 		intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \
144 					      + RING_CTX_OFF(name.val),\
145 					      &shadow_ring_context->name.val, 4);\
146 		shadow_ring_context->name.val |= 0xffff << 16;\
147 	}
148 
149 	COPY_REG_MASKED(ctx_ctrl);
150 	COPY_REG(ctx_timestamp);
151 
152 	if (ring_id == RCS) {
153 		COPY_REG(bb_per_ctx_ptr);
154 		COPY_REG(rcs_indirect_ctx);
155 		COPY_REG(rcs_indirect_ctx_offset);
156 	}
157 #undef COPY_REG
158 #undef COPY_REG_MASKED
159 
160 	intel_gvt_hypervisor_read_gpa(vgpu,
161 			workload->ring_context_gpa +
162 			sizeof(*shadow_ring_context),
163 			(void *)shadow_ring_context +
164 			sizeof(*shadow_ring_context),
165 			I915_GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
166 
167 	sr_oa_regs(workload, (u32 *)shadow_ring_context, false);
168 	kunmap(page);
169 
170 	if (IS_RESTORE_INHIBIT(shadow_ring_context->ctx_ctrl.val))
171 		return 0;
172 
173 	gvt_dbg_sched("ring id %d workload lrca %x", ring_id,
174 			workload->ctx_desc.lrca);
175 
176 	context_page_num = gvt->dev_priv->engine[ring_id]->context_size;
177 
178 	context_page_num = context_page_num >> PAGE_SHIFT;
179 
180 	if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
181 		context_page_num = 19;
182 
183 	i = 2;
184 	while (i < context_page_num) {
185 		context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
186 				(u32)((workload->ctx_desc.lrca + i) <<
187 				I915_GTT_PAGE_SHIFT));
188 		if (context_gpa == INTEL_GVT_INVALID_ADDR) {
189 			gvt_vgpu_err("Invalid guest context descriptor\n");
190 			return -EFAULT;
191 		}
192 
193 		page = i915_gem_object_get_page(ctx_obj, LRC_HEADER_PAGES + i);
194 		dst = kmap(page);
195 		intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst,
196 				I915_GTT_PAGE_SIZE);
197 		kunmap(page);
198 		i++;
199 	}
200 	return 0;
201 }
202 
203 static inline bool is_gvt_request(struct i915_request *req)
204 {
205 	return i915_gem_context_force_single_submission(req->gem_context);
206 }
207 
208 static void save_ring_hw_state(struct intel_vgpu *vgpu, int ring_id)
209 {
210 	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
211 	u32 ring_base = dev_priv->engine[ring_id]->mmio_base;
212 	i915_reg_t reg;
213 
214 	reg = RING_INSTDONE(ring_base);
215 	vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg);
216 	reg = RING_ACTHD(ring_base);
217 	vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg);
218 	reg = RING_ACTHD_UDW(ring_base);
219 	vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg);
220 }
221 
222 static int shadow_context_status_change(struct notifier_block *nb,
223 		unsigned long action, void *data)
224 {
225 	struct i915_request *req = data;
226 	struct intel_gvt *gvt = container_of(nb, struct intel_gvt,
227 				shadow_ctx_notifier_block[req->engine->id]);
228 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
229 	enum intel_engine_id ring_id = req->engine->id;
230 	struct intel_vgpu_workload *workload;
231 	unsigned long flags;
232 
233 	if (!is_gvt_request(req)) {
234 		spin_lock_irqsave(&scheduler->mmio_context_lock, flags);
235 		if (action == INTEL_CONTEXT_SCHEDULE_IN &&
236 		    scheduler->engine_owner[ring_id]) {
237 			/* Switch ring from vGPU to host. */
238 			intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
239 					      NULL, ring_id);
240 			scheduler->engine_owner[ring_id] = NULL;
241 		}
242 		spin_unlock_irqrestore(&scheduler->mmio_context_lock, flags);
243 
244 		return NOTIFY_OK;
245 	}
246 
247 	workload = scheduler->current_workload[ring_id];
248 	if (unlikely(!workload))
249 		return NOTIFY_OK;
250 
251 	switch (action) {
252 	case INTEL_CONTEXT_SCHEDULE_IN:
253 		spin_lock_irqsave(&scheduler->mmio_context_lock, flags);
254 		if (workload->vgpu != scheduler->engine_owner[ring_id]) {
255 			/* Switch ring from host to vGPU or vGPU to vGPU. */
256 			intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
257 					      workload->vgpu, ring_id);
258 			scheduler->engine_owner[ring_id] = workload->vgpu;
259 		} else
260 			gvt_dbg_sched("skip ring %d mmio switch for vgpu%d\n",
261 				      ring_id, workload->vgpu->id);
262 		spin_unlock_irqrestore(&scheduler->mmio_context_lock, flags);
263 		atomic_set(&workload->shadow_ctx_active, 1);
264 		break;
265 	case INTEL_CONTEXT_SCHEDULE_OUT:
266 		save_ring_hw_state(workload->vgpu, ring_id);
267 		atomic_set(&workload->shadow_ctx_active, 0);
268 		break;
269 	case INTEL_CONTEXT_SCHEDULE_PREEMPTED:
270 		save_ring_hw_state(workload->vgpu, ring_id);
271 		break;
272 	default:
273 		WARN_ON(1);
274 		return NOTIFY_OK;
275 	}
276 	wake_up(&workload->shadow_ctx_status_wq);
277 	return NOTIFY_OK;
278 }
279 
280 static void shadow_context_descriptor_update(struct intel_context *ce)
281 {
282 	u64 desc = 0;
283 
284 	desc = ce->lrc_desc;
285 
286 	/* Update bits 0-11 of the context descriptor which includes flags
287 	 * like GEN8_CTX_* cached in desc_template
288 	 */
289 	desc &= U64_MAX << 12;
290 	desc |= ce->gem_context->desc_template & ((1ULL << 12) - 1);
291 
292 	ce->lrc_desc = desc;
293 }
294 
295 static int copy_workload_to_ring_buffer(struct intel_vgpu_workload *workload)
296 {
297 	struct intel_vgpu *vgpu = workload->vgpu;
298 	struct i915_request *req = workload->req;
299 	void *shadow_ring_buffer_va;
300 	u32 *cs;
301 
302 	if ((IS_KABYLAKE(req->i915) || IS_BROXTON(req->i915))
303 		&& is_inhibit_context(req->hw_context))
304 		intel_vgpu_restore_inhibit_context(vgpu, req);
305 
306 	/* allocate shadow ring buffer */
307 	cs = intel_ring_begin(workload->req, workload->rb_len / sizeof(u32));
308 	if (IS_ERR(cs)) {
309 		gvt_vgpu_err("fail to alloc size =%ld shadow  ring buffer\n",
310 			workload->rb_len);
311 		return PTR_ERR(cs);
312 	}
313 
314 	shadow_ring_buffer_va = workload->shadow_ring_buffer_va;
315 
316 	/* get shadow ring buffer va */
317 	workload->shadow_ring_buffer_va = cs;
318 
319 	memcpy(cs, shadow_ring_buffer_va,
320 			workload->rb_len);
321 
322 	cs += workload->rb_len / sizeof(u32);
323 	intel_ring_advance(workload->req, cs);
324 
325 	return 0;
326 }
327 
328 static void release_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
329 {
330 	if (!wa_ctx->indirect_ctx.obj)
331 		return;
332 
333 	i915_gem_object_unpin_map(wa_ctx->indirect_ctx.obj);
334 	i915_gem_object_put(wa_ctx->indirect_ctx.obj);
335 
336 	wa_ctx->indirect_ctx.obj = NULL;
337 	wa_ctx->indirect_ctx.shadow_va = NULL;
338 }
339 
340 static int set_context_ppgtt_from_shadow(struct intel_vgpu_workload *workload,
341 					 struct i915_gem_context *ctx)
342 {
343 	struct intel_vgpu_mm *mm = workload->shadow_mm;
344 	struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
345 	int i = 0;
346 
347 	if (mm->type != INTEL_GVT_MM_PPGTT || !mm->ppgtt_mm.shadowed)
348 		return -1;
349 
350 	if (mm->ppgtt_mm.root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY) {
351 		px_dma(&ppgtt->pml4) = mm->ppgtt_mm.shadow_pdps[0];
352 	} else {
353 		for (i = 0; i < GVT_RING_CTX_NR_PDPS; i++) {
354 			px_dma(ppgtt->pdp.page_directory[i]) =
355 				mm->ppgtt_mm.shadow_pdps[i];
356 		}
357 	}
358 
359 	return 0;
360 }
361 
362 static int
363 intel_gvt_workload_req_alloc(struct intel_vgpu_workload *workload)
364 {
365 	struct intel_vgpu *vgpu = workload->vgpu;
366 	struct intel_vgpu_submission *s = &vgpu->submission;
367 	struct i915_gem_context *shadow_ctx = s->shadow_ctx;
368 	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
369 	struct intel_engine_cs *engine = dev_priv->engine[workload->ring_id];
370 	struct i915_request *rq;
371 	int ret = 0;
372 
373 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
374 
375 	if (workload->req)
376 		goto out;
377 
378 	rq = i915_request_alloc(engine, shadow_ctx);
379 	if (IS_ERR(rq)) {
380 		gvt_vgpu_err("fail to allocate gem request\n");
381 		ret = PTR_ERR(rq);
382 		goto out;
383 	}
384 	workload->req = i915_request_get(rq);
385 out:
386 	return ret;
387 }
388 
389 /**
390  * intel_gvt_scan_and_shadow_workload - audit the workload by scanning and
391  * shadow it as well, include ringbuffer,wa_ctx and ctx.
392  * @workload: an abstract entity for each execlist submission.
393  *
394  * This function is called before the workload submitting to i915, to make
395  * sure the content of the workload is valid.
396  */
397 int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload)
398 {
399 	struct intel_vgpu *vgpu = workload->vgpu;
400 	struct intel_vgpu_submission *s = &vgpu->submission;
401 	struct i915_gem_context *shadow_ctx = s->shadow_ctx;
402 	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
403 	struct intel_engine_cs *engine = dev_priv->engine[workload->ring_id];
404 	struct intel_context *ce;
405 	int ret;
406 
407 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
408 
409 	if (workload->shadow)
410 		return 0;
411 
412 	ret = set_context_ppgtt_from_shadow(workload, shadow_ctx);
413 	if (ret < 0) {
414 		gvt_vgpu_err("workload shadow ppgtt isn't ready\n");
415 		return ret;
416 	}
417 
418 	/* pin shadow context by gvt even the shadow context will be pinned
419 	 * when i915 alloc request. That is because gvt will update the guest
420 	 * context from shadow context when workload is completed, and at that
421 	 * moment, i915 may already unpined the shadow context to make the
422 	 * shadow_ctx pages invalid. So gvt need to pin itself. After update
423 	 * the guest context, gvt can unpin the shadow_ctx safely.
424 	 */
425 	ce = intel_context_pin(shadow_ctx, engine);
426 	if (IS_ERR(ce)) {
427 		gvt_vgpu_err("fail to pin shadow context\n");
428 		return PTR_ERR(ce);
429 	}
430 
431 	shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT);
432 	shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode <<
433 				    GEN8_CTX_ADDRESSING_MODE_SHIFT;
434 
435 	if (!test_and_set_bit(workload->ring_id, s->shadow_ctx_desc_updated))
436 		shadow_context_descriptor_update(ce);
437 
438 	ret = intel_gvt_scan_and_shadow_ringbuffer(workload);
439 	if (ret)
440 		goto err_unpin;
441 
442 	if ((workload->ring_id == RCS) &&
443 	    (workload->wa_ctx.indirect_ctx.size != 0)) {
444 		ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
445 		if (ret)
446 			goto err_shadow;
447 	}
448 
449 	workload->shadow = true;
450 	return 0;
451 err_shadow:
452 	release_shadow_wa_ctx(&workload->wa_ctx);
453 err_unpin:
454 	intel_context_unpin(ce);
455 	return ret;
456 }
457 
458 static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload);
459 
460 static int prepare_shadow_batch_buffer(struct intel_vgpu_workload *workload)
461 {
462 	struct intel_gvt *gvt = workload->vgpu->gvt;
463 	const int gmadr_bytes = gvt->device_info.gmadr_bytes_in_cmd;
464 	struct intel_vgpu_shadow_bb *bb;
465 	int ret;
466 
467 	list_for_each_entry(bb, &workload->shadow_bb, list) {
468 		/* For privilge batch buffer and not wa_ctx, the bb_start_cmd_va
469 		 * is only updated into ring_scan_buffer, not real ring address
470 		 * allocated in later copy_workload_to_ring_buffer. pls be noted
471 		 * shadow_ring_buffer_va is now pointed to real ring buffer va
472 		 * in copy_workload_to_ring_buffer.
473 		 */
474 
475 		if (bb->bb_offset)
476 			bb->bb_start_cmd_va = workload->shadow_ring_buffer_va
477 				+ bb->bb_offset;
478 
479 		if (bb->ppgtt) {
480 			/* for non-priv bb, scan&shadow is only for
481 			 * debugging purpose, so the content of shadow bb
482 			 * is the same as original bb. Therefore,
483 			 * here, rather than switch to shadow bb's gma
484 			 * address, we directly use original batch buffer's
485 			 * gma address, and send original bb to hardware
486 			 * directly
487 			 */
488 			if (bb->clflush & CLFLUSH_AFTER) {
489 				drm_clflush_virt_range(bb->va,
490 						bb->obj->base.size);
491 				bb->clflush &= ~CLFLUSH_AFTER;
492 			}
493 			i915_gem_obj_finish_shmem_access(bb->obj);
494 			bb->accessing = false;
495 
496 		} else {
497 			bb->vma = i915_gem_object_ggtt_pin(bb->obj,
498 					NULL, 0, 0, 0);
499 			if (IS_ERR(bb->vma)) {
500 				ret = PTR_ERR(bb->vma);
501 				goto err;
502 			}
503 
504 			/* relocate shadow batch buffer */
505 			bb->bb_start_cmd_va[1] = i915_ggtt_offset(bb->vma);
506 			if (gmadr_bytes == 8)
507 				bb->bb_start_cmd_va[2] = 0;
508 
509 			/* No one is going to touch shadow bb from now on. */
510 			if (bb->clflush & CLFLUSH_AFTER) {
511 				drm_clflush_virt_range(bb->va,
512 						bb->obj->base.size);
513 				bb->clflush &= ~CLFLUSH_AFTER;
514 			}
515 
516 			ret = i915_gem_object_set_to_gtt_domain(bb->obj,
517 					false);
518 			if (ret)
519 				goto err;
520 
521 			i915_gem_obj_finish_shmem_access(bb->obj);
522 			bb->accessing = false;
523 
524 			ret = i915_vma_move_to_active(bb->vma,
525 						      workload->req,
526 						      0);
527 			if (ret)
528 				goto err;
529 		}
530 	}
531 	return 0;
532 err:
533 	release_shadow_batch_buffer(workload);
534 	return ret;
535 }
536 
537 static void update_wa_ctx_2_shadow_ctx(struct intel_shadow_wa_ctx *wa_ctx)
538 {
539 	struct intel_vgpu_workload *workload =
540 		container_of(wa_ctx, struct intel_vgpu_workload, wa_ctx);
541 	struct i915_request *rq = workload->req;
542 	struct execlist_ring_context *shadow_ring_context =
543 		(struct execlist_ring_context *)rq->hw_context->lrc_reg_state;
544 
545 	shadow_ring_context->bb_per_ctx_ptr.val =
546 		(shadow_ring_context->bb_per_ctx_ptr.val &
547 		(~PER_CTX_ADDR_MASK)) | wa_ctx->per_ctx.shadow_gma;
548 	shadow_ring_context->rcs_indirect_ctx.val =
549 		(shadow_ring_context->rcs_indirect_ctx.val &
550 		(~INDIRECT_CTX_ADDR_MASK)) | wa_ctx->indirect_ctx.shadow_gma;
551 }
552 
553 static int prepare_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
554 {
555 	struct i915_vma *vma;
556 	unsigned char *per_ctx_va =
557 		(unsigned char *)wa_ctx->indirect_ctx.shadow_va +
558 		wa_ctx->indirect_ctx.size;
559 
560 	if (wa_ctx->indirect_ctx.size == 0)
561 		return 0;
562 
563 	vma = i915_gem_object_ggtt_pin(wa_ctx->indirect_ctx.obj, NULL,
564 				       0, CACHELINE_BYTES, 0);
565 	if (IS_ERR(vma))
566 		return PTR_ERR(vma);
567 
568 	/* FIXME: we are not tracking our pinned VMA leaving it
569 	 * up to the core to fix up the stray pin_count upon
570 	 * free.
571 	 */
572 
573 	wa_ctx->indirect_ctx.shadow_gma = i915_ggtt_offset(vma);
574 
575 	wa_ctx->per_ctx.shadow_gma = *((unsigned int *)per_ctx_va + 1);
576 	memset(per_ctx_va, 0, CACHELINE_BYTES);
577 
578 	update_wa_ctx_2_shadow_ctx(wa_ctx);
579 	return 0;
580 }
581 
582 static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload)
583 {
584 	struct intel_vgpu *vgpu = workload->vgpu;
585 	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
586 	struct intel_vgpu_shadow_bb *bb, *pos;
587 
588 	if (list_empty(&workload->shadow_bb))
589 		return;
590 
591 	bb = list_first_entry(&workload->shadow_bb,
592 			struct intel_vgpu_shadow_bb, list);
593 
594 	mutex_lock(&dev_priv->drm.struct_mutex);
595 
596 	list_for_each_entry_safe(bb, pos, &workload->shadow_bb, list) {
597 		if (bb->obj) {
598 			if (bb->accessing)
599 				i915_gem_obj_finish_shmem_access(bb->obj);
600 
601 			if (bb->va && !IS_ERR(bb->va))
602 				i915_gem_object_unpin_map(bb->obj);
603 
604 			if (bb->vma && !IS_ERR(bb->vma)) {
605 				i915_vma_unpin(bb->vma);
606 				i915_vma_close(bb->vma);
607 			}
608 			__i915_gem_object_release_unless_active(bb->obj);
609 		}
610 		list_del(&bb->list);
611 		kfree(bb);
612 	}
613 
614 	mutex_unlock(&dev_priv->drm.struct_mutex);
615 }
616 
617 static int prepare_workload(struct intel_vgpu_workload *workload)
618 {
619 	struct intel_vgpu *vgpu = workload->vgpu;
620 	int ret = 0;
621 
622 	ret = intel_vgpu_pin_mm(workload->shadow_mm);
623 	if (ret) {
624 		gvt_vgpu_err("fail to vgpu pin mm\n");
625 		return ret;
626 	}
627 
628 	update_shadow_pdps(workload);
629 
630 	ret = intel_vgpu_sync_oos_pages(workload->vgpu);
631 	if (ret) {
632 		gvt_vgpu_err("fail to vgpu sync oos pages\n");
633 		goto err_unpin_mm;
634 	}
635 
636 	ret = intel_vgpu_flush_post_shadow(workload->vgpu);
637 	if (ret) {
638 		gvt_vgpu_err("fail to flush post shadow\n");
639 		goto err_unpin_mm;
640 	}
641 
642 	ret = copy_workload_to_ring_buffer(workload);
643 	if (ret) {
644 		gvt_vgpu_err("fail to generate request\n");
645 		goto err_unpin_mm;
646 	}
647 
648 	ret = prepare_shadow_batch_buffer(workload);
649 	if (ret) {
650 		gvt_vgpu_err("fail to prepare_shadow_batch_buffer\n");
651 		goto err_unpin_mm;
652 	}
653 
654 	ret = prepare_shadow_wa_ctx(&workload->wa_ctx);
655 	if (ret) {
656 		gvt_vgpu_err("fail to prepare_shadow_wa_ctx\n");
657 		goto err_shadow_batch;
658 	}
659 
660 	if (workload->prepare) {
661 		ret = workload->prepare(workload);
662 		if (ret)
663 			goto err_shadow_wa_ctx;
664 	}
665 
666 	return 0;
667 err_shadow_wa_ctx:
668 	release_shadow_wa_ctx(&workload->wa_ctx);
669 err_shadow_batch:
670 	release_shadow_batch_buffer(workload);
671 err_unpin_mm:
672 	intel_vgpu_unpin_mm(workload->shadow_mm);
673 	return ret;
674 }
675 
676 static int dispatch_workload(struct intel_vgpu_workload *workload)
677 {
678 	struct intel_vgpu *vgpu = workload->vgpu;
679 	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
680 	int ring_id = workload->ring_id;
681 	int ret;
682 
683 	gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
684 		ring_id, workload);
685 
686 	mutex_lock(&vgpu->vgpu_lock);
687 	mutex_lock(&dev_priv->drm.struct_mutex);
688 
689 	ret = intel_gvt_workload_req_alloc(workload);
690 	if (ret)
691 		goto err_req;
692 
693 	ret = intel_gvt_scan_and_shadow_workload(workload);
694 	if (ret)
695 		goto out;
696 
697 	ret = populate_shadow_context(workload);
698 	if (ret) {
699 		release_shadow_wa_ctx(&workload->wa_ctx);
700 		goto out;
701 	}
702 
703 	ret = prepare_workload(workload);
704 out:
705 	if (!IS_ERR_OR_NULL(workload->req)) {
706 		gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
707 				ring_id, workload->req);
708 		i915_request_add(workload->req);
709 		workload->dispatched = true;
710 	}
711 err_req:
712 	if (ret)
713 		workload->status = ret;
714 	mutex_unlock(&dev_priv->drm.struct_mutex);
715 	mutex_unlock(&vgpu->vgpu_lock);
716 	return ret;
717 }
718 
719 static struct intel_vgpu_workload *pick_next_workload(
720 		struct intel_gvt *gvt, int ring_id)
721 {
722 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
723 	struct intel_vgpu_workload *workload = NULL;
724 
725 	mutex_lock(&gvt->sched_lock);
726 
727 	/*
728 	 * no current vgpu / will be scheduled out / no workload
729 	 * bail out
730 	 */
731 	if (!scheduler->current_vgpu) {
732 		gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
733 		goto out;
734 	}
735 
736 	if (scheduler->need_reschedule) {
737 		gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
738 		goto out;
739 	}
740 
741 	if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id)))
742 		goto out;
743 
744 	/*
745 	 * still have current workload, maybe the workload disptacher
746 	 * fail to submit it for some reason, resubmit it.
747 	 */
748 	if (scheduler->current_workload[ring_id]) {
749 		workload = scheduler->current_workload[ring_id];
750 		gvt_dbg_sched("ring id %d still have current workload %p\n",
751 				ring_id, workload);
752 		goto out;
753 	}
754 
755 	/*
756 	 * pick a workload as current workload
757 	 * once current workload is set, schedule policy routines
758 	 * will wait the current workload is finished when trying to
759 	 * schedule out a vgpu.
760 	 */
761 	scheduler->current_workload[ring_id] = container_of(
762 			workload_q_head(scheduler->current_vgpu, ring_id)->next,
763 			struct intel_vgpu_workload, list);
764 
765 	workload = scheduler->current_workload[ring_id];
766 
767 	gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
768 
769 	atomic_inc(&workload->vgpu->submission.running_workload_num);
770 out:
771 	mutex_unlock(&gvt->sched_lock);
772 	return workload;
773 }
774 
775 static void update_guest_context(struct intel_vgpu_workload *workload)
776 {
777 	struct i915_request *rq = workload->req;
778 	struct intel_vgpu *vgpu = workload->vgpu;
779 	struct intel_gvt *gvt = vgpu->gvt;
780 	struct drm_i915_gem_object *ctx_obj = rq->hw_context->state->obj;
781 	struct execlist_ring_context *shadow_ring_context;
782 	struct page *page;
783 	void *src;
784 	unsigned long context_gpa, context_page_num;
785 	int i;
786 
787 	gvt_dbg_sched("ring id %d workload lrca %x\n", rq->engine->id,
788 		      workload->ctx_desc.lrca);
789 
790 	context_page_num = rq->engine->context_size;
791 	context_page_num = context_page_num >> PAGE_SHIFT;
792 
793 	if (IS_BROADWELL(gvt->dev_priv) && rq->engine->id == RCS)
794 		context_page_num = 19;
795 
796 	i = 2;
797 
798 	while (i < context_page_num) {
799 		context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
800 				(u32)((workload->ctx_desc.lrca + i) <<
801 					I915_GTT_PAGE_SHIFT));
802 		if (context_gpa == INTEL_GVT_INVALID_ADDR) {
803 			gvt_vgpu_err("invalid guest context descriptor\n");
804 			return;
805 		}
806 
807 		page = i915_gem_object_get_page(ctx_obj, LRC_HEADER_PAGES + i);
808 		src = kmap(page);
809 		intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
810 				I915_GTT_PAGE_SIZE);
811 		kunmap(page);
812 		i++;
813 	}
814 
815 	intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
816 		RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
817 
818 	page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
819 	shadow_ring_context = kmap(page);
820 
821 #define COPY_REG(name) \
822 	intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
823 		RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
824 
825 	COPY_REG(ctx_ctrl);
826 	COPY_REG(ctx_timestamp);
827 
828 #undef COPY_REG
829 
830 	intel_gvt_hypervisor_write_gpa(vgpu,
831 			workload->ring_context_gpa +
832 			sizeof(*shadow_ring_context),
833 			(void *)shadow_ring_context +
834 			sizeof(*shadow_ring_context),
835 			I915_GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
836 
837 	kunmap(page);
838 }
839 
840 void intel_vgpu_clean_workloads(struct intel_vgpu *vgpu,
841 				unsigned long engine_mask)
842 {
843 	struct intel_vgpu_submission *s = &vgpu->submission;
844 	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
845 	struct intel_engine_cs *engine;
846 	struct intel_vgpu_workload *pos, *n;
847 	unsigned int tmp;
848 
849 	/* free the unsubmited workloads in the queues. */
850 	for_each_engine_masked(engine, dev_priv, engine_mask, tmp) {
851 		list_for_each_entry_safe(pos, n,
852 			&s->workload_q_head[engine->id], list) {
853 			list_del_init(&pos->list);
854 			intel_vgpu_destroy_workload(pos);
855 		}
856 		clear_bit(engine->id, s->shadow_ctx_desc_updated);
857 	}
858 }
859 
860 static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
861 {
862 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
863 	struct intel_vgpu_workload *workload =
864 		scheduler->current_workload[ring_id];
865 	struct intel_vgpu *vgpu = workload->vgpu;
866 	struct intel_vgpu_submission *s = &vgpu->submission;
867 	struct i915_request *rq = workload->req;
868 	int event;
869 
870 	mutex_lock(&vgpu->vgpu_lock);
871 	mutex_lock(&gvt->sched_lock);
872 
873 	/* For the workload w/ request, needs to wait for the context
874 	 * switch to make sure request is completed.
875 	 * For the workload w/o request, directly complete the workload.
876 	 */
877 	if (rq) {
878 		wait_event(workload->shadow_ctx_status_wq,
879 			   !atomic_read(&workload->shadow_ctx_active));
880 
881 		/* If this request caused GPU hang, req->fence.error will
882 		 * be set to -EIO. Use -EIO to set workload status so
883 		 * that when this request caused GPU hang, didn't trigger
884 		 * context switch interrupt to guest.
885 		 */
886 		if (likely(workload->status == -EINPROGRESS)) {
887 			if (workload->req->fence.error == -EIO)
888 				workload->status = -EIO;
889 			else
890 				workload->status = 0;
891 		}
892 
893 		if (!workload->status && !(vgpu->resetting_eng &
894 					   ENGINE_MASK(ring_id))) {
895 			update_guest_context(workload);
896 
897 			for_each_set_bit(event, workload->pending_events,
898 					 INTEL_GVT_EVENT_MAX)
899 				intel_vgpu_trigger_virtual_event(vgpu, event);
900 		}
901 
902 		/* unpin shadow ctx as the shadow_ctx update is done */
903 		mutex_lock(&rq->i915->drm.struct_mutex);
904 		intel_context_unpin(rq->hw_context);
905 		mutex_unlock(&rq->i915->drm.struct_mutex);
906 
907 		i915_request_put(fetch_and_zero(&workload->req));
908 	}
909 
910 	gvt_dbg_sched("ring id %d complete workload %p status %d\n",
911 			ring_id, workload, workload->status);
912 
913 	scheduler->current_workload[ring_id] = NULL;
914 
915 	list_del_init(&workload->list);
916 
917 	if (workload->status || (vgpu->resetting_eng & ENGINE_MASK(ring_id))) {
918 		/* if workload->status is not successful means HW GPU
919 		 * has occurred GPU hang or something wrong with i915/GVT,
920 		 * and GVT won't inject context switch interrupt to guest.
921 		 * So this error is a vGPU hang actually to the guest.
922 		 * According to this we should emunlate a vGPU hang. If
923 		 * there are pending workloads which are already submitted
924 		 * from guest, we should clean them up like HW GPU does.
925 		 *
926 		 * if it is in middle of engine resetting, the pending
927 		 * workloads won't be submitted to HW GPU and will be
928 		 * cleaned up during the resetting process later, so doing
929 		 * the workload clean up here doesn't have any impact.
930 		 **/
931 		intel_vgpu_clean_workloads(vgpu, ENGINE_MASK(ring_id));
932 	}
933 
934 	workload->complete(workload);
935 
936 	atomic_dec(&s->running_workload_num);
937 	wake_up(&scheduler->workload_complete_wq);
938 
939 	if (gvt->scheduler.need_reschedule)
940 		intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EVENT_SCHED);
941 
942 	mutex_unlock(&gvt->sched_lock);
943 	mutex_unlock(&vgpu->vgpu_lock);
944 }
945 
946 struct workload_thread_param {
947 	struct intel_gvt *gvt;
948 	int ring_id;
949 };
950 
951 static int workload_thread(void *priv)
952 {
953 	struct workload_thread_param *p = (struct workload_thread_param *)priv;
954 	struct intel_gvt *gvt = p->gvt;
955 	int ring_id = p->ring_id;
956 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
957 	struct intel_vgpu_workload *workload = NULL;
958 	struct intel_vgpu *vgpu = NULL;
959 	int ret;
960 	bool need_force_wake = IS_SKYLAKE(gvt->dev_priv)
961 			|| IS_KABYLAKE(gvt->dev_priv)
962 			|| IS_BROXTON(gvt->dev_priv);
963 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
964 
965 	kfree(p);
966 
967 	gvt_dbg_core("workload thread for ring %d started\n", ring_id);
968 
969 	while (!kthread_should_stop()) {
970 		add_wait_queue(&scheduler->waitq[ring_id], &wait);
971 		do {
972 			workload = pick_next_workload(gvt, ring_id);
973 			if (workload)
974 				break;
975 			wait_woken(&wait, TASK_INTERRUPTIBLE,
976 				   MAX_SCHEDULE_TIMEOUT);
977 		} while (!kthread_should_stop());
978 		remove_wait_queue(&scheduler->waitq[ring_id], &wait);
979 
980 		if (!workload)
981 			break;
982 
983 		gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
984 				workload->ring_id, workload,
985 				workload->vgpu->id);
986 
987 		intel_runtime_pm_get(gvt->dev_priv);
988 
989 		gvt_dbg_sched("ring id %d will dispatch workload %p\n",
990 				workload->ring_id, workload);
991 
992 		if (need_force_wake)
993 			intel_uncore_forcewake_get(gvt->dev_priv,
994 					FORCEWAKE_ALL);
995 
996 		ret = dispatch_workload(workload);
997 
998 		if (ret) {
999 			vgpu = workload->vgpu;
1000 			gvt_vgpu_err("fail to dispatch workload, skip\n");
1001 			goto complete;
1002 		}
1003 
1004 		gvt_dbg_sched("ring id %d wait workload %p\n",
1005 				workload->ring_id, workload);
1006 		i915_request_wait(workload->req, 0, MAX_SCHEDULE_TIMEOUT);
1007 
1008 complete:
1009 		gvt_dbg_sched("will complete workload %p, status: %d\n",
1010 				workload, workload->status);
1011 
1012 		complete_current_workload(gvt, ring_id);
1013 
1014 		if (need_force_wake)
1015 			intel_uncore_forcewake_put(gvt->dev_priv,
1016 					FORCEWAKE_ALL);
1017 
1018 		intel_runtime_pm_put(gvt->dev_priv);
1019 		if (ret && (vgpu_is_vm_unhealthy(ret)))
1020 			enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR);
1021 	}
1022 	return 0;
1023 }
1024 
1025 void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
1026 {
1027 	struct intel_vgpu_submission *s = &vgpu->submission;
1028 	struct intel_gvt *gvt = vgpu->gvt;
1029 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
1030 
1031 	if (atomic_read(&s->running_workload_num)) {
1032 		gvt_dbg_sched("wait vgpu idle\n");
1033 
1034 		wait_event(scheduler->workload_complete_wq,
1035 				!atomic_read(&s->running_workload_num));
1036 	}
1037 }
1038 
1039 void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
1040 {
1041 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
1042 	struct intel_engine_cs *engine;
1043 	enum intel_engine_id i;
1044 
1045 	gvt_dbg_core("clean workload scheduler\n");
1046 
1047 	for_each_engine(engine, gvt->dev_priv, i) {
1048 		atomic_notifier_chain_unregister(
1049 					&engine->context_status_notifier,
1050 					&gvt->shadow_ctx_notifier_block[i]);
1051 		kthread_stop(scheduler->thread[i]);
1052 	}
1053 }
1054 
1055 int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
1056 {
1057 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
1058 	struct workload_thread_param *param = NULL;
1059 	struct intel_engine_cs *engine;
1060 	enum intel_engine_id i;
1061 	int ret;
1062 
1063 	gvt_dbg_core("init workload scheduler\n");
1064 
1065 	init_waitqueue_head(&scheduler->workload_complete_wq);
1066 
1067 	for_each_engine(engine, gvt->dev_priv, i) {
1068 		init_waitqueue_head(&scheduler->waitq[i]);
1069 
1070 		param = kzalloc(sizeof(*param), GFP_KERNEL);
1071 		if (!param) {
1072 			ret = -ENOMEM;
1073 			goto err;
1074 		}
1075 
1076 		param->gvt = gvt;
1077 		param->ring_id = i;
1078 
1079 		scheduler->thread[i] = kthread_run(workload_thread, param,
1080 			"gvt workload %d", i);
1081 		if (IS_ERR(scheduler->thread[i])) {
1082 			gvt_err("fail to create workload thread\n");
1083 			ret = PTR_ERR(scheduler->thread[i]);
1084 			goto err;
1085 		}
1086 
1087 		gvt->shadow_ctx_notifier_block[i].notifier_call =
1088 					shadow_context_status_change;
1089 		atomic_notifier_chain_register(&engine->context_status_notifier,
1090 					&gvt->shadow_ctx_notifier_block[i]);
1091 	}
1092 	return 0;
1093 err:
1094 	intel_gvt_clean_workload_scheduler(gvt);
1095 	kfree(param);
1096 	param = NULL;
1097 	return ret;
1098 }
1099 
1100 static void
1101 i915_context_ppgtt_root_restore(struct intel_vgpu_submission *s)
1102 {
1103 	struct i915_hw_ppgtt *i915_ppgtt = s->shadow_ctx->ppgtt;
1104 	int i;
1105 
1106 	if (i915_vm_is_48bit(&i915_ppgtt->vm))
1107 		px_dma(&i915_ppgtt->pml4) = s->i915_context_pml4;
1108 	else {
1109 		for (i = 0; i < GEN8_3LVL_PDPES; i++)
1110 			px_dma(i915_ppgtt->pdp.page_directory[i]) =
1111 						s->i915_context_pdps[i];
1112 	}
1113 }
1114 
1115 /**
1116  * intel_vgpu_clean_submission - free submission-related resource for vGPU
1117  * @vgpu: a vGPU
1118  *
1119  * This function is called when a vGPU is being destroyed.
1120  *
1121  */
1122 void intel_vgpu_clean_submission(struct intel_vgpu *vgpu)
1123 {
1124 	struct intel_vgpu_submission *s = &vgpu->submission;
1125 
1126 	intel_vgpu_select_submission_ops(vgpu, ALL_ENGINES, 0);
1127 	i915_context_ppgtt_root_restore(s);
1128 	i915_gem_context_put(s->shadow_ctx);
1129 	kmem_cache_destroy(s->workloads);
1130 }
1131 
1132 
1133 /**
1134  * intel_vgpu_reset_submission - reset submission-related resource for vGPU
1135  * @vgpu: a vGPU
1136  * @engine_mask: engines expected to be reset
1137  *
1138  * This function is called when a vGPU is being destroyed.
1139  *
1140  */
1141 void intel_vgpu_reset_submission(struct intel_vgpu *vgpu,
1142 		unsigned long engine_mask)
1143 {
1144 	struct intel_vgpu_submission *s = &vgpu->submission;
1145 
1146 	if (!s->active)
1147 		return;
1148 
1149 	intel_vgpu_clean_workloads(vgpu, engine_mask);
1150 	s->ops->reset(vgpu, engine_mask);
1151 }
1152 
1153 static void
1154 i915_context_ppgtt_root_save(struct intel_vgpu_submission *s)
1155 {
1156 	struct i915_hw_ppgtt *i915_ppgtt = s->shadow_ctx->ppgtt;
1157 	int i;
1158 
1159 	if (i915_vm_is_48bit(&i915_ppgtt->vm))
1160 		s->i915_context_pml4 = px_dma(&i915_ppgtt->pml4);
1161 	else {
1162 		for (i = 0; i < GEN8_3LVL_PDPES; i++)
1163 			s->i915_context_pdps[i] =
1164 				px_dma(i915_ppgtt->pdp.page_directory[i]);
1165 	}
1166 }
1167 
1168 /**
1169  * intel_vgpu_setup_submission - setup submission-related resource for vGPU
1170  * @vgpu: a vGPU
1171  *
1172  * This function is called when a vGPU is being created.
1173  *
1174  * Returns:
1175  * Zero on success, negative error code if failed.
1176  *
1177  */
1178 int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)
1179 {
1180 	struct intel_vgpu_submission *s = &vgpu->submission;
1181 	enum intel_engine_id i;
1182 	struct intel_engine_cs *engine;
1183 	int ret;
1184 
1185 	s->shadow_ctx = i915_gem_context_create_gvt(
1186 			&vgpu->gvt->dev_priv->drm);
1187 	if (IS_ERR(s->shadow_ctx))
1188 		return PTR_ERR(s->shadow_ctx);
1189 
1190 	i915_context_ppgtt_root_save(s);
1191 
1192 	bitmap_zero(s->shadow_ctx_desc_updated, I915_NUM_ENGINES);
1193 
1194 	s->workloads = kmem_cache_create_usercopy("gvt-g_vgpu_workload",
1195 						  sizeof(struct intel_vgpu_workload), 0,
1196 						  SLAB_HWCACHE_ALIGN,
1197 						  offsetof(struct intel_vgpu_workload, rb_tail),
1198 						  sizeof_field(struct intel_vgpu_workload, rb_tail),
1199 						  NULL);
1200 
1201 	if (!s->workloads) {
1202 		ret = -ENOMEM;
1203 		goto out_shadow_ctx;
1204 	}
1205 
1206 	for_each_engine(engine, vgpu->gvt->dev_priv, i)
1207 		INIT_LIST_HEAD(&s->workload_q_head[i]);
1208 
1209 	atomic_set(&s->running_workload_num, 0);
1210 	bitmap_zero(s->tlb_handle_pending, I915_NUM_ENGINES);
1211 
1212 	return 0;
1213 
1214 out_shadow_ctx:
1215 	i915_gem_context_put(s->shadow_ctx);
1216 	return ret;
1217 }
1218 
1219 /**
1220  * intel_vgpu_select_submission_ops - select virtual submission interface
1221  * @vgpu: a vGPU
1222  * @engine_mask: either ALL_ENGINES or target engine mask
1223  * @interface: expected vGPU virtual submission interface
1224  *
1225  * This function is called when guest configures submission interface.
1226  *
1227  * Returns:
1228  * Zero on success, negative error code if failed.
1229  *
1230  */
1231 int intel_vgpu_select_submission_ops(struct intel_vgpu *vgpu,
1232 				     unsigned long engine_mask,
1233 				     unsigned int interface)
1234 {
1235 	struct intel_vgpu_submission *s = &vgpu->submission;
1236 	const struct intel_vgpu_submission_ops *ops[] = {
1237 		[INTEL_VGPU_EXECLIST_SUBMISSION] =
1238 			&intel_vgpu_execlist_submission_ops,
1239 	};
1240 	int ret;
1241 
1242 	if (WARN_ON(interface >= ARRAY_SIZE(ops)))
1243 		return -EINVAL;
1244 
1245 	if (WARN_ON(interface == 0 && engine_mask != ALL_ENGINES))
1246 		return -EINVAL;
1247 
1248 	if (s->active)
1249 		s->ops->clean(vgpu, engine_mask);
1250 
1251 	if (interface == 0) {
1252 		s->ops = NULL;
1253 		s->virtual_submission_interface = 0;
1254 		s->active = false;
1255 		gvt_dbg_core("vgpu%d: remove submission ops\n", vgpu->id);
1256 		return 0;
1257 	}
1258 
1259 	ret = ops[interface]->init(vgpu, engine_mask);
1260 	if (ret)
1261 		return ret;
1262 
1263 	s->ops = ops[interface];
1264 	s->virtual_submission_interface = interface;
1265 	s->active = true;
1266 
1267 	gvt_dbg_core("vgpu%d: activate ops [ %s ]\n",
1268 			vgpu->id, s->ops->name);
1269 
1270 	return 0;
1271 }
1272 
1273 /**
1274  * intel_vgpu_destroy_workload - destroy a vGPU workload
1275  * @workload: workload to destroy
1276  *
1277  * This function is called when destroy a vGPU workload.
1278  *
1279  */
1280 void intel_vgpu_destroy_workload(struct intel_vgpu_workload *workload)
1281 {
1282 	struct intel_vgpu_submission *s = &workload->vgpu->submission;
1283 
1284 	release_shadow_batch_buffer(workload);
1285 	release_shadow_wa_ctx(&workload->wa_ctx);
1286 
1287 	if (workload->shadow_mm)
1288 		intel_vgpu_mm_put(workload->shadow_mm);
1289 
1290 	kmem_cache_free(s->workloads, workload);
1291 }
1292 
1293 static struct intel_vgpu_workload *
1294 alloc_workload(struct intel_vgpu *vgpu)
1295 {
1296 	struct intel_vgpu_submission *s = &vgpu->submission;
1297 	struct intel_vgpu_workload *workload;
1298 
1299 	workload = kmem_cache_zalloc(s->workloads, GFP_KERNEL);
1300 	if (!workload)
1301 		return ERR_PTR(-ENOMEM);
1302 
1303 	INIT_LIST_HEAD(&workload->list);
1304 	INIT_LIST_HEAD(&workload->shadow_bb);
1305 
1306 	init_waitqueue_head(&workload->shadow_ctx_status_wq);
1307 	atomic_set(&workload->shadow_ctx_active, 0);
1308 
1309 	workload->status = -EINPROGRESS;
1310 	workload->vgpu = vgpu;
1311 
1312 	return workload;
1313 }
1314 
1315 #define RING_CTX_OFF(x) \
1316 	offsetof(struct execlist_ring_context, x)
1317 
1318 static void read_guest_pdps(struct intel_vgpu *vgpu,
1319 		u64 ring_context_gpa, u32 pdp[8])
1320 {
1321 	u64 gpa;
1322 	int i;
1323 
1324 	gpa = ring_context_gpa + RING_CTX_OFF(pdps[0].val);
1325 
1326 	for (i = 0; i < 8; i++)
1327 		intel_gvt_hypervisor_read_gpa(vgpu,
1328 				gpa + i * 8, &pdp[7 - i], 4);
1329 }
1330 
1331 static int prepare_mm(struct intel_vgpu_workload *workload)
1332 {
1333 	struct execlist_ctx_descriptor_format *desc = &workload->ctx_desc;
1334 	struct intel_vgpu_mm *mm;
1335 	struct intel_vgpu *vgpu = workload->vgpu;
1336 	intel_gvt_gtt_type_t root_entry_type;
1337 	u64 pdps[GVT_RING_CTX_NR_PDPS];
1338 
1339 	switch (desc->addressing_mode) {
1340 	case 1: /* legacy 32-bit */
1341 		root_entry_type = GTT_TYPE_PPGTT_ROOT_L3_ENTRY;
1342 		break;
1343 	case 3: /* legacy 64-bit */
1344 		root_entry_type = GTT_TYPE_PPGTT_ROOT_L4_ENTRY;
1345 		break;
1346 	default:
1347 		gvt_vgpu_err("Advanced Context mode(SVM) is not supported!\n");
1348 		return -EINVAL;
1349 	}
1350 
1351 	read_guest_pdps(workload->vgpu, workload->ring_context_gpa, (void *)pdps);
1352 
1353 	mm = intel_vgpu_get_ppgtt_mm(workload->vgpu, root_entry_type, pdps);
1354 	if (IS_ERR(mm))
1355 		return PTR_ERR(mm);
1356 
1357 	workload->shadow_mm = mm;
1358 	return 0;
1359 }
1360 
1361 #define same_context(a, b) (((a)->context_id == (b)->context_id) && \
1362 		((a)->lrca == (b)->lrca))
1363 
1364 #define get_last_workload(q) \
1365 	(list_empty(q) ? NULL : container_of(q->prev, \
1366 	struct intel_vgpu_workload, list))
1367 /**
1368  * intel_vgpu_create_workload - create a vGPU workload
1369  * @vgpu: a vGPU
1370  * @ring_id: ring index
1371  * @desc: a guest context descriptor
1372  *
1373  * This function is called when creating a vGPU workload.
1374  *
1375  * Returns:
1376  * struct intel_vgpu_workload * on success, negative error code in
1377  * pointer if failed.
1378  *
1379  */
1380 struct intel_vgpu_workload *
1381 intel_vgpu_create_workload(struct intel_vgpu *vgpu, int ring_id,
1382 			   struct execlist_ctx_descriptor_format *desc)
1383 {
1384 	struct intel_vgpu_submission *s = &vgpu->submission;
1385 	struct list_head *q = workload_q_head(vgpu, ring_id);
1386 	struct intel_vgpu_workload *last_workload = get_last_workload(q);
1387 	struct intel_vgpu_workload *workload = NULL;
1388 	struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
1389 	u64 ring_context_gpa;
1390 	u32 head, tail, start, ctl, ctx_ctl, per_ctx, indirect_ctx;
1391 	int ret;
1392 
1393 	ring_context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
1394 			(u32)((desc->lrca + 1) << I915_GTT_PAGE_SHIFT));
1395 	if (ring_context_gpa == INTEL_GVT_INVALID_ADDR) {
1396 		gvt_vgpu_err("invalid guest context LRCA: %x\n", desc->lrca);
1397 		return ERR_PTR(-EINVAL);
1398 	}
1399 
1400 	intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1401 			RING_CTX_OFF(ring_header.val), &head, 4);
1402 
1403 	intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1404 			RING_CTX_OFF(ring_tail.val), &tail, 4);
1405 
1406 	head &= RB_HEAD_OFF_MASK;
1407 	tail &= RB_TAIL_OFF_MASK;
1408 
1409 	if (last_workload && same_context(&last_workload->ctx_desc, desc)) {
1410 		gvt_dbg_el("ring id %d cur workload == last\n", ring_id);
1411 		gvt_dbg_el("ctx head %x real head %lx\n", head,
1412 				last_workload->rb_tail);
1413 		/*
1414 		 * cannot use guest context head pointer here,
1415 		 * as it might not be updated at this time
1416 		 */
1417 		head = last_workload->rb_tail;
1418 	}
1419 
1420 	gvt_dbg_el("ring id %d begin a new workload\n", ring_id);
1421 
1422 	/* record some ring buffer register values for scan and shadow */
1423 	intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1424 			RING_CTX_OFF(rb_start.val), &start, 4);
1425 	intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1426 			RING_CTX_OFF(rb_ctrl.val), &ctl, 4);
1427 	intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1428 			RING_CTX_OFF(ctx_ctrl.val), &ctx_ctl, 4);
1429 
1430 	workload = alloc_workload(vgpu);
1431 	if (IS_ERR(workload))
1432 		return workload;
1433 
1434 	workload->ring_id = ring_id;
1435 	workload->ctx_desc = *desc;
1436 	workload->ring_context_gpa = ring_context_gpa;
1437 	workload->rb_head = head;
1438 	workload->rb_tail = tail;
1439 	workload->rb_start = start;
1440 	workload->rb_ctl = ctl;
1441 
1442 	if (ring_id == RCS) {
1443 		intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1444 			RING_CTX_OFF(bb_per_ctx_ptr.val), &per_ctx, 4);
1445 		intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1446 			RING_CTX_OFF(rcs_indirect_ctx.val), &indirect_ctx, 4);
1447 
1448 		workload->wa_ctx.indirect_ctx.guest_gma =
1449 			indirect_ctx & INDIRECT_CTX_ADDR_MASK;
1450 		workload->wa_ctx.indirect_ctx.size =
1451 			(indirect_ctx & INDIRECT_CTX_SIZE_MASK) *
1452 			CACHELINE_BYTES;
1453 		workload->wa_ctx.per_ctx.guest_gma =
1454 			per_ctx & PER_CTX_ADDR_MASK;
1455 		workload->wa_ctx.per_ctx.valid = per_ctx & 1;
1456 	}
1457 
1458 	gvt_dbg_el("workload %p ring id %d head %x tail %x start %x ctl %x\n",
1459 			workload, ring_id, head, tail, start, ctl);
1460 
1461 	ret = prepare_mm(workload);
1462 	if (ret) {
1463 		kmem_cache_free(s->workloads, workload);
1464 		return ERR_PTR(ret);
1465 	}
1466 
1467 	/* Only scan and shadow the first workload in the queue
1468 	 * as there is only one pre-allocated buf-obj for shadow.
1469 	 */
1470 	if (list_empty(workload_q_head(vgpu, ring_id))) {
1471 		intel_runtime_pm_get(dev_priv);
1472 		mutex_lock(&dev_priv->drm.struct_mutex);
1473 		ret = intel_gvt_scan_and_shadow_workload(workload);
1474 		mutex_unlock(&dev_priv->drm.struct_mutex);
1475 		intel_runtime_pm_put(dev_priv);
1476 	}
1477 
1478 	if (ret && (vgpu_is_vm_unhealthy(ret))) {
1479 		enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR);
1480 		intel_vgpu_destroy_workload(workload);
1481 		return ERR_PTR(ret);
1482 	}
1483 
1484 	return workload;
1485 }
1486 
1487 /**
1488  * intel_vgpu_queue_workload - Qeue a vGPU workload
1489  * @workload: the workload to queue in
1490  */
1491 void intel_vgpu_queue_workload(struct intel_vgpu_workload *workload)
1492 {
1493 	list_add_tail(&workload->list,
1494 		workload_q_head(workload->vgpu, workload->ring_id));
1495 	intel_gvt_kick_schedule(workload->vgpu->gvt);
1496 	wake_up(&workload->vgpu->gvt->scheduler.waitq[workload->ring_id]);
1497 }
1498