blob: a22a686f14c2c4035ef630ac1da6322439722159 [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
Chris Wilsone61e0f52018-02-21 09:56:36 +0000129static inline bool is_gvt_request(struct i915_request *req)
Changbin Dubc2d4b62017-03-22 12:35:31 +0800130{
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{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000151 struct i915_request *req = data;
Changbin Du3fc03062017-03-13 10:47:11 +0800152 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;
Changbin Du679fd3e2017-11-13 14:58:31 +0800157 unsigned long flags;
Zhi Wange4734052016-05-01 07:42:16 -0400158
Changbin Du0e86cc92017-05-04 10:52:38 +0800159 if (!is_gvt_request(req)) {
Changbin Du679fd3e2017-11-13 14:58:31 +0800160 spin_lock_irqsave(&scheduler->mmio_context_lock, flags);
Changbin Du0e86cc92017-05-04 10:52:38 +0800161 if (action == INTEL_CONTEXT_SCHEDULE_IN &&
162 scheduler->engine_owner[ring_id]) {
163 /* Switch ring from vGPU to host. */
164 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
165 NULL, ring_id);
166 scheduler->engine_owner[ring_id] = NULL;
167 }
Changbin Du679fd3e2017-11-13 14:58:31 +0800168 spin_unlock_irqrestore(&scheduler->mmio_context_lock, flags);
Changbin Du0e86cc92017-05-04 10:52:38 +0800169
170 return NOTIFY_OK;
171 }
172
173 workload = scheduler->current_workload[ring_id];
174 if (unlikely(!workload))
Chuanxiao Dong9272f732017-02-17 19:29:52 +0800175 return NOTIFY_OK;
176
Zhi Wange4734052016-05-01 07:42:16 -0400177 switch (action) {
178 case INTEL_CONTEXT_SCHEDULE_IN:
Changbin Du679fd3e2017-11-13 14:58:31 +0800179 spin_lock_irqsave(&scheduler->mmio_context_lock, flags);
Changbin Du0e86cc92017-05-04 10:52:38 +0800180 if (workload->vgpu != scheduler->engine_owner[ring_id]) {
181 /* Switch ring from host to vGPU or vGPU to vGPU. */
182 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
183 workload->vgpu, ring_id);
184 scheduler->engine_owner[ring_id] = workload->vgpu;
185 } else
186 gvt_dbg_sched("skip ring %d mmio switch for vgpu%d\n",
187 ring_id, workload->vgpu->id);
Changbin Du679fd3e2017-11-13 14:58:31 +0800188 spin_unlock_irqrestore(&scheduler->mmio_context_lock, flags);
Zhi Wange4734052016-05-01 07:42:16 -0400189 atomic_set(&workload->shadow_ctx_active, 1);
190 break;
191 case INTEL_CONTEXT_SCHEDULE_OUT:
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;
Zhenyu Wangda5f99e2017-12-01 14:59:53 +0800195 case INTEL_CONTEXT_SCHEDULE_PREEMPTED:
196 save_ring_hw_state(workload->vgpu, ring_id);
197 break;
Zhi Wange4734052016-05-01 07:42:16 -0400198 default:
199 WARN_ON(1);
200 return NOTIFY_OK;
201 }
202 wake_up(&workload->shadow_ctx_status_wq);
203 return NOTIFY_OK;
204}
205
Kechen Lu9dfb8e52017-08-10 07:41:36 +0800206static void shadow_context_descriptor_update(struct i915_gem_context *ctx,
207 struct intel_engine_cs *engine)
208{
209 struct intel_context *ce = &ctx->engine[engine->id];
210 u64 desc = 0;
211
212 desc = ce->lrc_desc;
213
214 /* Update bits 0-11 of the context descriptor which includes flags
215 * like GEN8_CTX_* cached in desc_template
216 */
217 desc &= U64_MAX << 12;
218 desc |= ctx->desc_template & ((1ULL << 12) - 1);
219
220 ce->lrc_desc = desc;
221}
222
fred gao0a53bc02017-08-18 15:41:06 +0800223static int copy_workload_to_ring_buffer(struct intel_vgpu_workload *workload)
224{
225 struct intel_vgpu *vgpu = workload->vgpu;
226 void *shadow_ring_buffer_va;
227 u32 *cs;
228
229 /* allocate shadow ring buffer */
230 cs = intel_ring_begin(workload->req, workload->rb_len / sizeof(u32));
231 if (IS_ERR(cs)) {
232 gvt_vgpu_err("fail to alloc size =%ld shadow ring buffer\n",
233 workload->rb_len);
234 return PTR_ERR(cs);
235 }
236
237 shadow_ring_buffer_va = workload->shadow_ring_buffer_va;
238
239 /* get shadow ring buffer va */
240 workload->shadow_ring_buffer_va = cs;
241
242 memcpy(cs, shadow_ring_buffer_va,
243 workload->rb_len);
244
245 cs += workload->rb_len / sizeof(u32);
246 intel_ring_advance(workload->req, cs);
247
248 return 0;
249}
250
Chris Wilson7b302552017-11-20 13:29:58 +0000251static void release_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
fred gaoa3cfdca2017-08-18 15:41:07 +0800252{
253 if (!wa_ctx->indirect_ctx.obj)
254 return;
255
256 i915_gem_object_unpin_map(wa_ctx->indirect_ctx.obj);
257 i915_gem_object_put(wa_ctx->indirect_ctx.obj);
258}
259
Ping Gao89ea20b2017-06-29 12:22:42 +0800260/**
261 * intel_gvt_scan_and_shadow_workload - audit the workload by scanning and
262 * shadow it as well, include ringbuffer,wa_ctx and ctx.
263 * @workload: an abstract entity for each execlist submission.
264 *
265 * This function is called before the workload submitting to i915, to make
266 * sure the content of the workload is valid.
267 */
268int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload)
Zhi Wange4734052016-05-01 07:42:16 -0400269{
Zhi Wang1406a142017-09-10 21:15:18 +0800270 struct intel_vgpu *vgpu = workload->vgpu;
271 struct intel_vgpu_submission *s = &vgpu->submission;
272 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
273 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
Zhi Wange4734052016-05-01 07:42:16 -0400274 int ring_id = workload->ring_id;
fred gao0a53bc02017-08-18 15:41:06 +0800275 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
fred gao0a53bc02017-08-18 15:41:06 +0800276 struct intel_ring *ring;
Zhi Wange4734052016-05-01 07:42:16 -0400277 int ret;
278
Ping Gao87e919d2017-07-04 14:53:03 +0800279 lockdep_assert_held(&dev_priv->drm.struct_mutex);
280
Ping Gaod0302e72017-06-29 12:22:43 +0800281 if (workload->shadowed)
282 return 0;
Zhi Wange4734052016-05-01 07:42:16 -0400283
Zhenyu Wang03806ed2017-02-13 17:07:19 +0800284 shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT);
285 shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode <<
Zhi Wange4734052016-05-01 07:42:16 -0400286 GEN8_CTX_ADDRESSING_MODE_SHIFT;
287
Zhi Wang1406a142017-09-10 21:15:18 +0800288 if (!test_and_set_bit(ring_id, s->shadow_ctx_desc_updated))
Kechen Lu9dfb8e52017-08-10 07:41:36 +0800289 shadow_context_descriptor_update(shadow_ctx,
290 dev_priv->engine[ring_id]);
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800291
Ping Gao89ea20b2017-06-29 12:22:42 +0800292 ret = intel_gvt_scan_and_shadow_ringbuffer(workload);
Zhi Wangbe1da702016-05-03 18:26:57 -0400293 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800294 goto err_scan;
Zhi Wangbe1da702016-05-03 18:26:57 -0400295
Tina Zhang17f1b1a2017-03-15 23:16:01 -0400296 if ((workload->ring_id == RCS) &&
297 (workload->wa_ctx.indirect_ctx.size != 0)) {
298 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
299 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800300 goto err_scan;
Tina Zhang17f1b1a2017-03-15 23:16:01 -0400301 }
Zhi Wangbe1da702016-05-03 18:26:57 -0400302
Ping Gao89ea20b2017-06-29 12:22:42 +0800303 /* pin shadow context by gvt even the shadow context will be pinned
304 * when i915 alloc request. That is because gvt will update the guest
305 * context from shadow context when workload is completed, and at that
306 * moment, i915 may already unpined the shadow context to make the
307 * shadow_ctx pages invalid. So gvt need to pin itself. After update
308 * the guest context, gvt can unpin the shadow_ctx safely.
309 */
310 ring = engine->context_pin(engine, shadow_ctx);
311 if (IS_ERR(ring)) {
312 ret = PTR_ERR(ring);
313 gvt_vgpu_err("fail to pin shadow context\n");
fred gaoa3cfdca2017-08-18 15:41:07 +0800314 goto err_shadow;
Ping Gao89ea20b2017-06-29 12:22:42 +0800315 }
Zhi Wange4734052016-05-01 07:42:16 -0400316
fred gao0a53bc02017-08-18 15:41:06 +0800317 ret = populate_shadow_context(workload);
318 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800319 goto err_unpin;
fred gaof2880e02017-11-14 17:09:35 +0800320 workload->shadowed = true;
321 return 0;
322
323err_unpin:
324 engine->context_unpin(engine, shadow_ctx);
325err_shadow:
326 release_shadow_wa_ctx(&workload->wa_ctx);
327err_scan:
328 return ret;
329}
330
331static int intel_gvt_generate_request(struct intel_vgpu_workload *workload)
332{
333 int ring_id = workload->ring_id;
334 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
335 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
Chris Wilsone61e0f52018-02-21 09:56:36 +0000336 struct i915_request *rq;
fred gaof2880e02017-11-14 17:09:35 +0800337 struct intel_vgpu *vgpu = workload->vgpu;
338 struct intel_vgpu_submission *s = &vgpu->submission;
339 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
340 int ret;
fred gao0a53bc02017-08-18 15:41:06 +0800341
Chris Wilsone61e0f52018-02-21 09:56:36 +0000342 rq = i915_request_alloc(dev_priv->engine[ring_id], shadow_ctx);
fred gao0a53bc02017-08-18 15:41:06 +0800343 if (IS_ERR(rq)) {
344 gvt_vgpu_err("fail to allocate gem request\n");
345 ret = PTR_ERR(rq);
fred gaoa3cfdca2017-08-18 15:41:07 +0800346 goto err_unpin;
fred gao0a53bc02017-08-18 15:41:06 +0800347 }
348
349 gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq);
350
Chris Wilsone61e0f52018-02-21 09:56:36 +0000351 workload->req = i915_request_get(rq);
fred gao0a53bc02017-08-18 15:41:06 +0800352 ret = copy_workload_to_ring_buffer(workload);
353 if (ret)
fred gaoa3cfdca2017-08-18 15:41:07 +0800354 goto err_unpin;
fred gaoa3cfdca2017-08-18 15:41:07 +0800355 return 0;
fred gao0a53bc02017-08-18 15:41:06 +0800356
fred gaoa3cfdca2017-08-18 15:41:07 +0800357err_unpin:
358 engine->context_unpin(engine, shadow_ctx);
fred gaoa3cfdca2017-08-18 15:41:07 +0800359 release_shadow_wa_ctx(&workload->wa_ctx);
fred gao0a53bc02017-08-18 15:41:06 +0800360 return ret;
361}
362
Zhi Wangf52c3802017-09-24 21:53:03 +0800363static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload);
364
Zhi Wangd8235b52017-09-12 22:06:39 +0800365static int prepare_shadow_batch_buffer(struct intel_vgpu_workload *workload)
366{
367 struct intel_gvt *gvt = workload->vgpu->gvt;
368 const int gmadr_bytes = gvt->device_info.gmadr_bytes_in_cmd;
Zhi Wangf52c3802017-09-24 21:53:03 +0800369 struct intel_vgpu_shadow_bb *bb;
370 int ret;
Zhi Wangd8235b52017-09-12 22:06:39 +0800371
Zhi Wangf52c3802017-09-24 21:53:03 +0800372 list_for_each_entry(bb, &workload->shadow_bb, list) {
373 bb->vma = i915_gem_object_ggtt_pin(bb->obj, NULL, 0, 0, 0);
374 if (IS_ERR(bb->vma)) {
375 ret = PTR_ERR(bb->vma);
376 goto err;
377 }
Zhi Wangd8235b52017-09-12 22:06:39 +0800378
Zhi Wangf52c3802017-09-24 21:53:03 +0800379 /* relocate shadow batch buffer */
380 bb->bb_start_cmd_va[1] = i915_ggtt_offset(bb->vma);
Zhi Wangd8235b52017-09-12 22:06:39 +0800381 if (gmadr_bytes == 8)
Zhi Wangf52c3802017-09-24 21:53:03 +0800382 bb->bb_start_cmd_va[2] = 0;
383
384 /* No one is going to touch shadow bb from now on. */
385 if (bb->clflush & CLFLUSH_AFTER) {
386 drm_clflush_virt_range(bb->va, bb->obj->base.size);
387 bb->clflush &= ~CLFLUSH_AFTER;
388 }
389
390 ret = i915_gem_object_set_to_gtt_domain(bb->obj, false);
391 if (ret)
392 goto err;
393
394 i915_gem_obj_finish_shmem_access(bb->obj);
395 bb->accessing = false;
396
397 i915_vma_move_to_active(bb->vma, workload->req, 0);
Zhi Wangd8235b52017-09-12 22:06:39 +0800398 }
399 return 0;
Zhi Wangf52c3802017-09-24 21:53:03 +0800400err:
401 release_shadow_batch_buffer(workload);
402 return ret;
Zhi Wangd8235b52017-09-12 22:06:39 +0800403}
404
405static int update_wa_ctx_2_shadow_ctx(struct intel_shadow_wa_ctx *wa_ctx)
406{
407 struct intel_vgpu_workload *workload = container_of(wa_ctx,
408 struct intel_vgpu_workload,
409 wa_ctx);
410 int ring_id = workload->ring_id;
411 struct intel_vgpu_submission *s = &workload->vgpu->submission;
412 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
413 struct drm_i915_gem_object *ctx_obj =
414 shadow_ctx->engine[ring_id].state->obj;
415 struct execlist_ring_context *shadow_ring_context;
416 struct page *page;
417
418 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
419 shadow_ring_context = kmap_atomic(page);
420
421 shadow_ring_context->bb_per_ctx_ptr.val =
422 (shadow_ring_context->bb_per_ctx_ptr.val &
423 (~PER_CTX_ADDR_MASK)) | wa_ctx->per_ctx.shadow_gma;
424 shadow_ring_context->rcs_indirect_ctx.val =
425 (shadow_ring_context->rcs_indirect_ctx.val &
426 (~INDIRECT_CTX_ADDR_MASK)) | wa_ctx->indirect_ctx.shadow_gma;
427
428 kunmap_atomic(shadow_ring_context);
429 return 0;
430}
431
432static int prepare_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
433{
434 struct i915_vma *vma;
435 unsigned char *per_ctx_va =
436 (unsigned char *)wa_ctx->indirect_ctx.shadow_va +
437 wa_ctx->indirect_ctx.size;
438
439 if (wa_ctx->indirect_ctx.size == 0)
440 return 0;
441
442 vma = i915_gem_object_ggtt_pin(wa_ctx->indirect_ctx.obj, NULL,
443 0, CACHELINE_BYTES, 0);
444 if (IS_ERR(vma))
445 return PTR_ERR(vma);
446
447 /* FIXME: we are not tracking our pinned VMA leaving it
448 * up to the core to fix up the stray pin_count upon
449 * free.
450 */
451
452 wa_ctx->indirect_ctx.shadow_gma = i915_ggtt_offset(vma);
453
454 wa_ctx->per_ctx.shadow_gma = *((unsigned int *)per_ctx_va + 1);
455 memset(per_ctx_va, 0, CACHELINE_BYTES);
456
457 update_wa_ctx_2_shadow_ctx(wa_ctx);
458 return 0;
459}
460
461static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload)
462{
Zhi Wangf52c3802017-09-24 21:53:03 +0800463 struct intel_vgpu *vgpu = workload->vgpu;
464 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
465 struct intel_vgpu_shadow_bb *bb, *pos;
Zhi Wangd8235b52017-09-12 22:06:39 +0800466
Zhi Wangf52c3802017-09-24 21:53:03 +0800467 if (list_empty(&workload->shadow_bb))
468 return;
469
470 bb = list_first_entry(&workload->shadow_bb,
471 struct intel_vgpu_shadow_bb, list);
472
473 mutex_lock(&dev_priv->drm.struct_mutex);
474
475 list_for_each_entry_safe(bb, pos, &workload->shadow_bb, list) {
476 if (bb->obj) {
477 if (bb->accessing)
478 i915_gem_obj_finish_shmem_access(bb->obj);
479
480 if (bb->va && !IS_ERR(bb->va))
481 i915_gem_object_unpin_map(bb->obj);
482
483 if (bb->vma && !IS_ERR(bb->vma)) {
484 i915_vma_unpin(bb->vma);
485 i915_vma_close(bb->vma);
486 }
487 __i915_gem_object_release_unless_active(bb->obj);
Zhi Wangd8235b52017-09-12 22:06:39 +0800488 }
Zhi Wangf52c3802017-09-24 21:53:03 +0800489 list_del(&bb->list);
490 kfree(bb);
Zhi Wangd8235b52017-09-12 22:06:39 +0800491 }
Zhi Wangf52c3802017-09-24 21:53:03 +0800492
493 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wangd8235b52017-09-12 22:06:39 +0800494}
495
Zhi Wang497aa3f2017-09-12 21:51:10 +0800496static int prepare_workload(struct intel_vgpu_workload *workload)
497{
Zhi Wangd8235b52017-09-12 22:06:39 +0800498 struct intel_vgpu *vgpu = workload->vgpu;
Zhi Wang497aa3f2017-09-12 21:51:10 +0800499 int ret = 0;
500
Zhi Wangd8235b52017-09-12 22:06:39 +0800501 ret = intel_vgpu_pin_mm(workload->shadow_mm);
502 if (ret) {
503 gvt_vgpu_err("fail to vgpu pin mm\n");
504 return ret;
505 }
Zhi Wang497aa3f2017-09-12 21:51:10 +0800506
Zhi Wangd8235b52017-09-12 22:06:39 +0800507 ret = intel_vgpu_sync_oos_pages(workload->vgpu);
508 if (ret) {
509 gvt_vgpu_err("fail to vgpu sync oos pages\n");
510 goto err_unpin_mm;
511 }
512
513 ret = intel_vgpu_flush_post_shadow(workload->vgpu);
514 if (ret) {
515 gvt_vgpu_err("fail to flush post shadow\n");
516 goto err_unpin_mm;
517 }
518
fred gaof2880e02017-11-14 17:09:35 +0800519 ret = intel_gvt_generate_request(workload);
520 if (ret) {
521 gvt_vgpu_err("fail to generate request\n");
522 goto err_unpin_mm;
523 }
524
Zhi Wangd8235b52017-09-12 22:06:39 +0800525 ret = prepare_shadow_batch_buffer(workload);
526 if (ret) {
527 gvt_vgpu_err("fail to prepare_shadow_batch_buffer\n");
528 goto err_unpin_mm;
529 }
530
531 ret = prepare_shadow_wa_ctx(&workload->wa_ctx);
532 if (ret) {
533 gvt_vgpu_err("fail to prepare_shadow_wa_ctx\n");
534 goto err_shadow_batch;
535 }
536
537 if (workload->prepare) {
538 ret = workload->prepare(workload);
539 if (ret)
540 goto err_shadow_wa_ctx;
541 }
542
543 return 0;
544err_shadow_wa_ctx:
545 release_shadow_wa_ctx(&workload->wa_ctx);
546err_shadow_batch:
547 release_shadow_batch_buffer(workload);
548err_unpin_mm:
549 intel_vgpu_unpin_mm(workload->shadow_mm);
Zhi Wang497aa3f2017-09-12 21:51:10 +0800550 return ret;
551}
552
fred gao0a53bc02017-08-18 15:41:06 +0800553static int dispatch_workload(struct intel_vgpu_workload *workload)
554{
Zhi Wang1406a142017-09-10 21:15:18 +0800555 struct intel_vgpu *vgpu = workload->vgpu;
556 struct intel_vgpu_submission *s = &vgpu->submission;
557 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
558 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
fred gao0a53bc02017-08-18 15:41:06 +0800559 int ring_id = workload->ring_id;
fred gao0a53bc02017-08-18 15:41:06 +0800560 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
561 int ret = 0;
562
563 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
564 ring_id, workload);
565
566 mutex_lock(&dev_priv->drm.struct_mutex);
567
568 ret = intel_gvt_scan_and_shadow_workload(workload);
569 if (ret)
570 goto out;
571
Zhi Wang497aa3f2017-09-12 21:51:10 +0800572 ret = prepare_workload(workload);
573 if (ret) {
574 engine->context_unpin(engine, shadow_ctx);
575 goto out;
fred gao0a53bc02017-08-18 15:41:06 +0800576 }
577
Pei Zhang90d27a12016-11-14 18:02:57 +0800578out:
579 if (ret)
580 workload->status = ret;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800581
Ping Gao89ea20b2017-06-29 12:22:42 +0800582 if (!IS_ERR_OR_NULL(workload->req)) {
583 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
584 ring_id, workload->req);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000585 i915_request_add(workload->req);
Ping Gao89ea20b2017-06-29 12:22:42 +0800586 workload->dispatched = true;
587 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800588
Pei Zhang90d27a12016-11-14 18:02:57 +0800589 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400590 return ret;
591}
592
593static struct intel_vgpu_workload *pick_next_workload(
594 struct intel_gvt *gvt, int ring_id)
595{
596 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
597 struct intel_vgpu_workload *workload = NULL;
598
599 mutex_lock(&gvt->lock);
600
601 /*
602 * no current vgpu / will be scheduled out / no workload
603 * bail out
604 */
605 if (!scheduler->current_vgpu) {
606 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
607 goto out;
608 }
609
610 if (scheduler->need_reschedule) {
611 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
612 goto out;
613 }
614
Zhenyu Wang954180a2017-04-12 14:22:50 +0800615 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id)))
Zhi Wange4734052016-05-01 07:42:16 -0400616 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400617
618 /*
619 * still have current workload, maybe the workload disptacher
620 * fail to submit it for some reason, resubmit it.
621 */
622 if (scheduler->current_workload[ring_id]) {
623 workload = scheduler->current_workload[ring_id];
624 gvt_dbg_sched("ring id %d still have current workload %p\n",
625 ring_id, workload);
626 goto out;
627 }
628
629 /*
630 * pick a workload as current workload
631 * once current workload is set, schedule policy routines
632 * will wait the current workload is finished when trying to
633 * schedule out a vgpu.
634 */
635 scheduler->current_workload[ring_id] = container_of(
636 workload_q_head(scheduler->current_vgpu, ring_id)->next,
637 struct intel_vgpu_workload, list);
638
639 workload = scheduler->current_workload[ring_id];
640
641 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
642
Zhi Wang1406a142017-09-10 21:15:18 +0800643 atomic_inc(&workload->vgpu->submission.running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400644out:
645 mutex_unlock(&gvt->lock);
646 return workload;
647}
648
649static void update_guest_context(struct intel_vgpu_workload *workload)
650{
651 struct intel_vgpu *vgpu = workload->vgpu;
652 struct intel_gvt *gvt = vgpu->gvt;
Zhi Wang1406a142017-09-10 21:15:18 +0800653 struct intel_vgpu_submission *s = &vgpu->submission;
654 struct i915_gem_context *shadow_ctx = s->shadow_ctx;
Zhi Wange4734052016-05-01 07:42:16 -0400655 int ring_id = workload->ring_id;
Zhi Wange4734052016-05-01 07:42:16 -0400656 struct drm_i915_gem_object *ctx_obj =
657 shadow_ctx->engine[ring_id].state->obj;
658 struct execlist_ring_context *shadow_ring_context;
659 struct page *page;
660 void *src;
661 unsigned long context_gpa, context_page_num;
662 int i;
663
664 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
665 workload->ctx_desc.lrca);
666
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300667 context_page_num = gvt->dev_priv->engine[ring_id]->context_size;
Zhi Wange4734052016-05-01 07:42:16 -0400668
669 context_page_num = context_page_num >> PAGE_SHIFT;
670
671 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
672 context_page_num = 19;
673
674 i = 2;
675
676 while (i < context_page_num) {
677 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
678 (u32)((workload->ctx_desc.lrca + i) <<
Zhi Wang9556e112017-10-10 13:51:32 +0800679 I915_GTT_PAGE_SHIFT));
Zhi Wange4734052016-05-01 07:42:16 -0400680 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500681 gvt_vgpu_err("invalid guest context descriptor\n");
Zhi Wange4734052016-05-01 07:42:16 -0400682 return;
683 }
684
Michel Thierry0b29c752017-09-13 09:56:00 +0100685 page = i915_gem_object_get_page(ctx_obj, LRC_HEADER_PAGES + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800686 src = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400687 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
Zhi Wang9556e112017-10-10 13:51:32 +0800688 I915_GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800689 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400690 i++;
691 }
692
693 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
694 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
695
696 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800697 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400698
699#define COPY_REG(name) \
700 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
701 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
702
703 COPY_REG(ctx_ctrl);
704 COPY_REG(ctx_timestamp);
705
706#undef COPY_REG
707
708 intel_gvt_hypervisor_write_gpa(vgpu,
709 workload->ring_context_gpa +
710 sizeof(*shadow_ring_context),
711 (void *)shadow_ring_context +
712 sizeof(*shadow_ring_context),
Zhi Wang9556e112017-10-10 13:51:32 +0800713 I915_GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
Zhi Wange4734052016-05-01 07:42:16 -0400714
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800715 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400716}
717
Zhi Wange2c43c02017-09-13 01:58:35 +0800718static void clean_workloads(struct intel_vgpu *vgpu, unsigned long engine_mask)
719{
720 struct intel_vgpu_submission *s = &vgpu->submission;
721 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
722 struct intel_engine_cs *engine;
723 struct intel_vgpu_workload *pos, *n;
724 unsigned int tmp;
725
726 /* free the unsubmited workloads in the queues. */
727 for_each_engine_masked(engine, dev_priv, engine_mask, tmp) {
728 list_for_each_entry_safe(pos, n,
729 &s->workload_q_head[engine->id], list) {
730 list_del_init(&pos->list);
731 intel_vgpu_destroy_workload(pos);
732 }
733 clear_bit(engine->id, s->shadow_ctx_desc_updated);
734 }
735}
736
Zhi Wange4734052016-05-01 07:42:16 -0400737static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
738{
739 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Zhi Wang1406a142017-09-10 21:15:18 +0800740 struct intel_vgpu_workload *workload =
741 scheduler->current_workload[ring_id];
742 struct intel_vgpu *vgpu = workload->vgpu;
743 struct intel_vgpu_submission *s = &vgpu->submission;
Zhi Wangbe1da702016-05-03 18:26:57 -0400744 int event;
Zhi Wange4734052016-05-01 07:42:16 -0400745
746 mutex_lock(&gvt->lock);
747
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800748 /* For the workload w/ request, needs to wait for the context
749 * switch to make sure request is completed.
750 * For the workload w/o request, directly complete the workload.
751 */
752 if (workload->req) {
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800753 struct drm_i915_private *dev_priv =
754 workload->vgpu->gvt->dev_priv;
755 struct intel_engine_cs *engine =
756 dev_priv->engine[workload->ring_id];
Zhi Wange4734052016-05-01 07:42:16 -0400757 wait_event(workload->shadow_ctx_status_wq,
758 !atomic_read(&workload->shadow_ctx_active));
759
Chuanxiao Dong0cf5ec42017-06-23 13:01:11 +0800760 /* If this request caused GPU hang, req->fence.error will
761 * be set to -EIO. Use -EIO to set workload status so
762 * that when this request caused GPU hang, didn't trigger
763 * context switch interrupt to guest.
764 */
765 if (likely(workload->status == -EINPROGRESS)) {
766 if (workload->req->fence.error == -EIO)
767 workload->status = -EIO;
768 else
769 workload->status = 0;
770 }
771
Chris Wilsone61e0f52018-02-21 09:56:36 +0000772 i915_request_put(fetch_and_zero(&workload->req));
Zhi Wangbe1da702016-05-03 18:26:57 -0400773
Chuanxiao Dong6184cc82017-08-01 17:47:25 +0800774 if (!workload->status && !(vgpu->resetting_eng &
775 ENGINE_MASK(ring_id))) {
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800776 update_guest_context(workload);
777
778 for_each_set_bit(event, workload->pending_events,
779 INTEL_GVT_EVENT_MAX)
780 intel_vgpu_trigger_virtual_event(vgpu, event);
781 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800782 mutex_lock(&dev_priv->drm.struct_mutex);
783 /* unpin shadow ctx as the shadow_ctx update is done */
Zhi Wang1406a142017-09-10 21:15:18 +0800784 engine->context_unpin(engine, s->shadow_ctx);
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800785 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400786 }
787
788 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
789 ring_id, workload, workload->status);
790
791 scheduler->current_workload[ring_id] = NULL;
792
Zhi Wange4734052016-05-01 07:42:16 -0400793 list_del_init(&workload->list);
Zhi Wangd8235b52017-09-12 22:06:39 +0800794
795 if (!workload->status) {
796 release_shadow_batch_buffer(workload);
797 release_shadow_wa_ctx(&workload->wa_ctx);
798 }
799
Zhi Wange2c43c02017-09-13 01:58:35 +0800800 if (workload->status || (vgpu->resetting_eng & ENGINE_MASK(ring_id))) {
801 /* if workload->status is not successful means HW GPU
802 * has occurred GPU hang or something wrong with i915/GVT,
803 * and GVT won't inject context switch interrupt to guest.
804 * So this error is a vGPU hang actually to the guest.
805 * According to this we should emunlate a vGPU hang. If
806 * there are pending workloads which are already submitted
807 * from guest, we should clean them up like HW GPU does.
808 *
809 * if it is in middle of engine resetting, the pending
810 * workloads won't be submitted to HW GPU and will be
811 * cleaned up during the resetting process later, so doing
812 * the workload clean up here doesn't have any impact.
813 **/
814 clean_workloads(vgpu, ENGINE_MASK(ring_id));
815 }
816
Zhi Wange4734052016-05-01 07:42:16 -0400817 workload->complete(workload);
818
Zhi Wang1406a142017-09-10 21:15:18 +0800819 atomic_dec(&s->running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400820 wake_up(&scheduler->workload_complete_wq);
Ping Gaof100dae2017-05-24 09:14:11 +0800821
822 if (gvt->scheduler.need_reschedule)
823 intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EVENT_SCHED);
824
Zhi Wange4734052016-05-01 07:42:16 -0400825 mutex_unlock(&gvt->lock);
826}
827
828struct workload_thread_param {
829 struct intel_gvt *gvt;
830 int ring_id;
831};
832
833static int workload_thread(void *priv)
834{
835 struct workload_thread_param *p = (struct workload_thread_param *)priv;
836 struct intel_gvt *gvt = p->gvt;
837 int ring_id = p->ring_id;
838 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
839 struct intel_vgpu_workload *workload = NULL;
Tina Zhang695fbc02017-03-10 04:26:53 -0500840 struct intel_vgpu *vgpu = NULL;
Zhi Wange4734052016-05-01 07:42:16 -0400841 int ret;
Xu Hane3476c02017-03-29 10:13:59 +0800842 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv)
843 || IS_KABYLAKE(gvt->dev_priv);
Du, Changbine45d7b72016-10-27 11:10:31 +0800844 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Zhi Wange4734052016-05-01 07:42:16 -0400845
846 kfree(p);
847
848 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
849
850 while (!kthread_should_stop()) {
Du, Changbine45d7b72016-10-27 11:10:31 +0800851 add_wait_queue(&scheduler->waitq[ring_id], &wait);
852 do {
853 workload = pick_next_workload(gvt, ring_id);
854 if (workload)
855 break;
856 wait_woken(&wait, TASK_INTERRUPTIBLE,
857 MAX_SCHEDULE_TIMEOUT);
858 } while (!kthread_should_stop());
859 remove_wait_queue(&scheduler->waitq[ring_id], &wait);
Zhi Wange4734052016-05-01 07:42:16 -0400860
Du, Changbine45d7b72016-10-27 11:10:31 +0800861 if (!workload)
Zhi Wange4734052016-05-01 07:42:16 -0400862 break;
863
864 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
865 workload->ring_id, workload,
866 workload->vgpu->id);
867
868 intel_runtime_pm_get(gvt->dev_priv);
869
Zhi Wange4734052016-05-01 07:42:16 -0400870 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
871 workload->ring_id, workload);
872
873 if (need_force_wake)
874 intel_uncore_forcewake_get(gvt->dev_priv,
875 FORCEWAKE_ALL);
876
Pei Zhang90d27a12016-11-14 18:02:57 +0800877 mutex_lock(&gvt->lock);
Zhi Wange4734052016-05-01 07:42:16 -0400878 ret = dispatch_workload(workload);
Pei Zhang90d27a12016-11-14 18:02:57 +0800879 mutex_unlock(&gvt->lock);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100880
Zhi Wange4734052016-05-01 07:42:16 -0400881 if (ret) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500882 vgpu = workload->vgpu;
883 gvt_vgpu_err("fail to dispatch workload, skip\n");
Zhi Wange4734052016-05-01 07:42:16 -0400884 goto complete;
885 }
886
887 gvt_dbg_sched("ring id %d wait workload %p\n",
888 workload->ring_id, workload);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000889 i915_request_wait(workload->req, 0, MAX_SCHEDULE_TIMEOUT);
Zhi Wange4734052016-05-01 07:42:16 -0400890
891complete:
Changbin Du3ce32742017-02-09 10:13:16 +0800892 gvt_dbg_sched("will complete workload %p, status: %d\n",
Zhi Wange4734052016-05-01 07:42:16 -0400893 workload, workload->status);
894
Changbin Du2e51ef32017-01-05 13:28:05 +0800895 complete_current_workload(gvt, ring_id);
896
Zhi Wange4734052016-05-01 07:42:16 -0400897 if (need_force_wake)
898 intel_uncore_forcewake_put(gvt->dev_priv,
899 FORCEWAKE_ALL);
900
Zhi Wange4734052016-05-01 07:42:16 -0400901 intel_runtime_pm_put(gvt->dev_priv);
Zhi Wang6d763032017-09-12 22:33:12 +0800902 if (ret && (vgpu_is_vm_unhealthy(ret)))
fred gaoe011c6c2017-09-19 15:11:28 +0800903 enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR);
Zhi Wange4734052016-05-01 07:42:16 -0400904 }
905 return 0;
906}
907
908void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
909{
Zhi Wang1406a142017-09-10 21:15:18 +0800910 struct intel_vgpu_submission *s = &vgpu->submission;
Zhi Wange4734052016-05-01 07:42:16 -0400911 struct intel_gvt *gvt = vgpu->gvt;
912 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
913
Zhi Wang1406a142017-09-10 21:15:18 +0800914 if (atomic_read(&s->running_workload_num)) {
Zhi Wange4734052016-05-01 07:42:16 -0400915 gvt_dbg_sched("wait vgpu idle\n");
916
917 wait_event(scheduler->workload_complete_wq,
Zhi Wang1406a142017-09-10 21:15:18 +0800918 !atomic_read(&s->running_workload_num));
Zhi Wange4734052016-05-01 07:42:16 -0400919 }
920}
921
922void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
923{
924 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Changbin Du3fc03062017-03-13 10:47:11 +0800925 struct intel_engine_cs *engine;
926 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400927
928 gvt_dbg_core("clean workload scheduler\n");
929
Changbin Du3fc03062017-03-13 10:47:11 +0800930 for_each_engine(engine, gvt->dev_priv, i) {
931 atomic_notifier_chain_unregister(
932 &engine->context_status_notifier,
933 &gvt->shadow_ctx_notifier_block[i]);
934 kthread_stop(scheduler->thread[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400935 }
936}
937
938int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
939{
940 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
941 struct workload_thread_param *param = NULL;
Changbin Du3fc03062017-03-13 10:47:11 +0800942 struct intel_engine_cs *engine;
943 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400944 int ret;
Zhi Wange4734052016-05-01 07:42:16 -0400945
946 gvt_dbg_core("init workload scheduler\n");
947
948 init_waitqueue_head(&scheduler->workload_complete_wq);
949
Changbin Du3fc03062017-03-13 10:47:11 +0800950 for_each_engine(engine, gvt->dev_priv, i) {
Zhi Wange4734052016-05-01 07:42:16 -0400951 init_waitqueue_head(&scheduler->waitq[i]);
952
953 param = kzalloc(sizeof(*param), GFP_KERNEL);
954 if (!param) {
955 ret = -ENOMEM;
956 goto err;
957 }
958
959 param->gvt = gvt;
960 param->ring_id = i;
961
962 scheduler->thread[i] = kthread_run(workload_thread, param,
963 "gvt workload %d", i);
964 if (IS_ERR(scheduler->thread[i])) {
965 gvt_err("fail to create workload thread\n");
966 ret = PTR_ERR(scheduler->thread[i]);
967 goto err;
968 }
Changbin Du3fc03062017-03-13 10:47:11 +0800969
970 gvt->shadow_ctx_notifier_block[i].notifier_call =
971 shadow_context_status_change;
972 atomic_notifier_chain_register(&engine->context_status_notifier,
973 &gvt->shadow_ctx_notifier_block[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400974 }
975 return 0;
976err:
977 intel_gvt_clean_workload_scheduler(gvt);
978 kfree(param);
979 param = NULL;
980 return ret;
981}
982
Zhi Wang874b6a92017-09-10 20:08:18 +0800983/**
984 * intel_vgpu_clean_submission - free submission-related resource for vGPU
985 * @vgpu: a vGPU
986 *
987 * This function is called when a vGPU is being destroyed.
988 *
989 */
990void intel_vgpu_clean_submission(struct intel_vgpu *vgpu)
Zhi Wange4734052016-05-01 07:42:16 -0400991{
Zhi Wang1406a142017-09-10 21:15:18 +0800992 struct intel_vgpu_submission *s = &vgpu->submission;
993
Zhi Wangad1d3632017-09-13 00:31:29 +0800994 intel_vgpu_select_submission_ops(vgpu, 0);
Zhi Wang1406a142017-09-10 21:15:18 +0800995 i915_gem_context_put(s->shadow_ctx);
996 kmem_cache_destroy(s->workloads);
Zhi Wange4734052016-05-01 07:42:16 -0400997}
998
Zhi Wang06bb3722017-09-13 01:41:35 +0800999
1000/**
1001 * intel_vgpu_reset_submission - reset submission-related resource for vGPU
1002 * @vgpu: a vGPU
1003 * @engine_mask: engines expected to be reset
1004 *
1005 * This function is called when a vGPU is being destroyed.
1006 *
1007 */
1008void intel_vgpu_reset_submission(struct intel_vgpu *vgpu,
1009 unsigned long engine_mask)
1010{
1011 struct intel_vgpu_submission *s = &vgpu->submission;
1012
1013 if (!s->active)
1014 return;
1015
Zhi Wange2c43c02017-09-13 01:58:35 +08001016 clean_workloads(vgpu, engine_mask);
Zhi Wang06bb3722017-09-13 01:41:35 +08001017 s->ops->reset(vgpu, engine_mask);
1018}
1019
Zhi Wang874b6a92017-09-10 20:08:18 +08001020/**
1021 * intel_vgpu_setup_submission - setup submission-related resource for vGPU
1022 * @vgpu: a vGPU
1023 *
1024 * This function is called when a vGPU is being created.
1025 *
1026 * Returns:
1027 * Zero on success, negative error code if failed.
1028 *
1029 */
1030int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)
Zhi Wange4734052016-05-01 07:42:16 -04001031{
Zhi Wang1406a142017-09-10 21:15:18 +08001032 struct intel_vgpu_submission *s = &vgpu->submission;
Zhi Wang9a9829e2017-09-10 20:28:09 +08001033 enum intel_engine_id i;
1034 struct intel_engine_cs *engine;
1035 int ret;
Zhi Wange4734052016-05-01 07:42:16 -04001036
Zhi Wang1406a142017-09-10 21:15:18 +08001037 s->shadow_ctx = i915_gem_context_create_gvt(
Zhi Wange4734052016-05-01 07:42:16 -04001038 &vgpu->gvt->dev_priv->drm);
Zhi Wang1406a142017-09-10 21:15:18 +08001039 if (IS_ERR(s->shadow_ctx))
1040 return PTR_ERR(s->shadow_ctx);
Zhi Wange4734052016-05-01 07:42:16 -04001041
Zhenyu Wang16036602017-12-04 10:42:58 +08001042 if (HAS_LOGICAL_RING_PREEMPTION(vgpu->gvt->dev_priv))
1043 s->shadow_ctx->priority = INT_MAX;
1044
Zhi Wang1406a142017-09-10 21:15:18 +08001045 bitmap_zero(s->shadow_ctx_desc_updated, I915_NUM_ENGINES);
Kechen Lu9dfb8e52017-08-10 07:41:36 +08001046
Zhi Wang1406a142017-09-10 21:15:18 +08001047 s->workloads = kmem_cache_create("gvt-g_vgpu_workload",
Zhi Wang9a9829e2017-09-10 20:28:09 +08001048 sizeof(struct intel_vgpu_workload), 0,
1049 SLAB_HWCACHE_ALIGN,
1050 NULL);
1051
Zhi Wang1406a142017-09-10 21:15:18 +08001052 if (!s->workloads) {
Zhi Wang9a9829e2017-09-10 20:28:09 +08001053 ret = -ENOMEM;
1054 goto out_shadow_ctx;
1055 }
1056
1057 for_each_engine(engine, vgpu->gvt->dev_priv, i)
Zhi Wang1406a142017-09-10 21:15:18 +08001058 INIT_LIST_HEAD(&s->workload_q_head[i]);
Zhi Wang9a9829e2017-09-10 20:28:09 +08001059
Zhi Wang1406a142017-09-10 21:15:18 +08001060 atomic_set(&s->running_workload_num, 0);
Zhi Wang91d5d852017-09-10 21:33:20 +08001061 bitmap_zero(s->tlb_handle_pending, I915_NUM_ENGINES);
Zhi Wang9a9829e2017-09-10 20:28:09 +08001062
Zhi Wange4734052016-05-01 07:42:16 -04001063 return 0;
Zhi Wang9a9829e2017-09-10 20:28:09 +08001064
1065out_shadow_ctx:
Zhi Wang1406a142017-09-10 21:15:18 +08001066 i915_gem_context_put(s->shadow_ctx);
Zhi Wang9a9829e2017-09-10 20:28:09 +08001067 return ret;
Zhi Wange4734052016-05-01 07:42:16 -04001068}
Zhi Wang21527a82017-09-12 21:42:09 +08001069
1070/**
Zhi Wangad1d3632017-09-13 00:31:29 +08001071 * intel_vgpu_select_submission_ops - select virtual submission interface
1072 * @vgpu: a vGPU
1073 * @interface: expected vGPU virtual submission interface
1074 *
1075 * This function is called when guest configures submission interface.
1076 *
1077 * Returns:
1078 * Zero on success, negative error code if failed.
1079 *
1080 */
1081int intel_vgpu_select_submission_ops(struct intel_vgpu *vgpu,
1082 unsigned int interface)
1083{
1084 struct intel_vgpu_submission *s = &vgpu->submission;
1085 const struct intel_vgpu_submission_ops *ops[] = {
1086 [INTEL_VGPU_EXECLIST_SUBMISSION] =
1087 &intel_vgpu_execlist_submission_ops,
1088 };
1089 int ret;
1090
1091 if (WARN_ON(interface >= ARRAY_SIZE(ops)))
1092 return -EINVAL;
1093
1094 if (s->active) {
1095 s->ops->clean(vgpu);
1096 s->active = false;
1097 gvt_dbg_core("vgpu%d: de-select ops [ %s ] \n",
1098 vgpu->id, s->ops->name);
1099 }
1100
1101 if (interface == 0) {
1102 s->ops = NULL;
1103 s->virtual_submission_interface = 0;
1104 gvt_dbg_core("vgpu%d: no submission ops\n", vgpu->id);
1105 return 0;
1106 }
1107
1108 ret = ops[interface]->init(vgpu);
1109 if (ret)
1110 return ret;
1111
1112 s->ops = ops[interface];
1113 s->virtual_submission_interface = interface;
1114 s->active = true;
1115
1116 gvt_dbg_core("vgpu%d: activate ops [ %s ]\n",
1117 vgpu->id, s->ops->name);
1118
1119 return 0;
1120}
1121
1122/**
Zhi Wang21527a82017-09-12 21:42:09 +08001123 * intel_vgpu_destroy_workload - destroy a vGPU workload
1124 * @vgpu: a vGPU
1125 *
1126 * This function is called when destroy a vGPU workload.
1127 *
1128 */
1129void intel_vgpu_destroy_workload(struct intel_vgpu_workload *workload)
1130{
1131 struct intel_vgpu_submission *s = &workload->vgpu->submission;
1132
1133 if (workload->shadow_mm)
1134 intel_gvt_mm_unreference(workload->shadow_mm);
1135
1136 kmem_cache_free(s->workloads, workload);
1137}
1138
Zhi Wang6d763032017-09-12 22:33:12 +08001139static struct intel_vgpu_workload *
1140alloc_workload(struct intel_vgpu *vgpu)
Zhi Wang21527a82017-09-12 21:42:09 +08001141{
1142 struct intel_vgpu_submission *s = &vgpu->submission;
1143 struct intel_vgpu_workload *workload;
1144
1145 workload = kmem_cache_zalloc(s->workloads, GFP_KERNEL);
1146 if (!workload)
1147 return ERR_PTR(-ENOMEM);
1148
1149 INIT_LIST_HEAD(&workload->list);
1150 INIT_LIST_HEAD(&workload->shadow_bb);
1151
1152 init_waitqueue_head(&workload->shadow_ctx_status_wq);
1153 atomic_set(&workload->shadow_ctx_active, 0);
1154
1155 workload->status = -EINPROGRESS;
1156 workload->shadowed = false;
1157 workload->vgpu = vgpu;
1158
1159 return workload;
1160}
Zhi Wang6d763032017-09-12 22:33:12 +08001161
1162#define RING_CTX_OFF(x) \
1163 offsetof(struct execlist_ring_context, x)
1164
1165static void read_guest_pdps(struct intel_vgpu *vgpu,
1166 u64 ring_context_gpa, u32 pdp[8])
1167{
1168 u64 gpa;
1169 int i;
1170
1171 gpa = ring_context_gpa + RING_CTX_OFF(pdp3_UDW.val);
1172
1173 for (i = 0; i < 8; i++)
1174 intel_gvt_hypervisor_read_gpa(vgpu,
1175 gpa + i * 8, &pdp[7 - i], 4);
1176}
1177
1178static int prepare_mm(struct intel_vgpu_workload *workload)
1179{
1180 struct execlist_ctx_descriptor_format *desc = &workload->ctx_desc;
1181 struct intel_vgpu_mm *mm;
1182 struct intel_vgpu *vgpu = workload->vgpu;
1183 int page_table_level;
1184 u32 pdp[8];
1185
1186 if (desc->addressing_mode == 1) { /* legacy 32-bit */
1187 page_table_level = 3;
1188 } else if (desc->addressing_mode == 3) { /* legacy 64 bit */
1189 page_table_level = 4;
1190 } else {
1191 gvt_vgpu_err("Advanced Context mode(SVM) is not supported!\n");
1192 return -EINVAL;
1193 }
1194
1195 read_guest_pdps(workload->vgpu, workload->ring_context_gpa, pdp);
1196
1197 mm = intel_vgpu_find_ppgtt_mm(workload->vgpu, page_table_level, pdp);
1198 if (mm) {
1199 intel_gvt_mm_reference(mm);
1200 } else {
1201
1202 mm = intel_vgpu_create_mm(workload->vgpu, INTEL_GVT_MM_PPGTT,
1203 pdp, page_table_level, 0);
1204 if (IS_ERR(mm)) {
1205 gvt_vgpu_err("fail to create mm object.\n");
1206 return PTR_ERR(mm);
1207 }
1208 }
1209 workload->shadow_mm = mm;
1210 return 0;
1211}
1212
1213#define same_context(a, b) (((a)->context_id == (b)->context_id) && \
1214 ((a)->lrca == (b)->lrca))
1215
1216#define get_last_workload(q) \
1217 (list_empty(q) ? NULL : container_of(q->prev, \
1218 struct intel_vgpu_workload, list))
1219/**
1220 * intel_vgpu_create_workload - create a vGPU workload
1221 * @vgpu: a vGPU
1222 * @desc: a guest context descriptor
1223 *
1224 * This function is called when creating a vGPU workload.
1225 *
1226 * Returns:
1227 * struct intel_vgpu_workload * on success, negative error code in
1228 * pointer if failed.
1229 *
1230 */
1231struct intel_vgpu_workload *
1232intel_vgpu_create_workload(struct intel_vgpu *vgpu, int ring_id,
1233 struct execlist_ctx_descriptor_format *desc)
1234{
1235 struct intel_vgpu_submission *s = &vgpu->submission;
1236 struct list_head *q = workload_q_head(vgpu, ring_id);
1237 struct intel_vgpu_workload *last_workload = get_last_workload(q);
1238 struct intel_vgpu_workload *workload = NULL;
1239 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
1240 u64 ring_context_gpa;
1241 u32 head, tail, start, ctl, ctx_ctl, per_ctx, indirect_ctx;
1242 int ret;
1243
1244 ring_context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
Zhi Wang9556e112017-10-10 13:51:32 +08001245 (u32)((desc->lrca + 1) << I915_GTT_PAGE_SHIFT));
Zhi Wang6d763032017-09-12 22:33:12 +08001246 if (ring_context_gpa == INTEL_GVT_INVALID_ADDR) {
1247 gvt_vgpu_err("invalid guest context LRCA: %x\n", desc->lrca);
1248 return ERR_PTR(-EINVAL);
1249 }
1250
1251 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1252 RING_CTX_OFF(ring_header.val), &head, 4);
1253
1254 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1255 RING_CTX_OFF(ring_tail.val), &tail, 4);
1256
1257 head &= RB_HEAD_OFF_MASK;
1258 tail &= RB_TAIL_OFF_MASK;
1259
1260 if (last_workload && same_context(&last_workload->ctx_desc, desc)) {
1261 gvt_dbg_el("ring id %d cur workload == last\n", ring_id);
1262 gvt_dbg_el("ctx head %x real head %lx\n", head,
1263 last_workload->rb_tail);
1264 /*
1265 * cannot use guest context head pointer here,
1266 * as it might not be updated at this time
1267 */
1268 head = last_workload->rb_tail;
1269 }
1270
1271 gvt_dbg_el("ring id %d begin a new workload\n", ring_id);
1272
1273 /* record some ring buffer register values for scan and shadow */
1274 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1275 RING_CTX_OFF(rb_start.val), &start, 4);
1276 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1277 RING_CTX_OFF(rb_ctrl.val), &ctl, 4);
1278 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1279 RING_CTX_OFF(ctx_ctrl.val), &ctx_ctl, 4);
1280
1281 workload = alloc_workload(vgpu);
1282 if (IS_ERR(workload))
1283 return workload;
1284
1285 workload->ring_id = ring_id;
1286 workload->ctx_desc = *desc;
1287 workload->ring_context_gpa = ring_context_gpa;
1288 workload->rb_head = head;
1289 workload->rb_tail = tail;
1290 workload->rb_start = start;
1291 workload->rb_ctl = ctl;
1292
1293 if (ring_id == RCS) {
1294 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1295 RING_CTX_OFF(bb_per_ctx_ptr.val), &per_ctx, 4);
1296 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
1297 RING_CTX_OFF(rcs_indirect_ctx.val), &indirect_ctx, 4);
1298
1299 workload->wa_ctx.indirect_ctx.guest_gma =
1300 indirect_ctx & INDIRECT_CTX_ADDR_MASK;
1301 workload->wa_ctx.indirect_ctx.size =
1302 (indirect_ctx & INDIRECT_CTX_SIZE_MASK) *
1303 CACHELINE_BYTES;
1304 workload->wa_ctx.per_ctx.guest_gma =
1305 per_ctx & PER_CTX_ADDR_MASK;
1306 workload->wa_ctx.per_ctx.valid = per_ctx & 1;
1307 }
1308
1309 gvt_dbg_el("workload %p ring id %d head %x tail %x start %x ctl %x\n",
1310 workload, ring_id, head, tail, start, ctl);
1311
1312 ret = prepare_mm(workload);
1313 if (ret) {
1314 kmem_cache_free(s->workloads, workload);
1315 return ERR_PTR(ret);
1316 }
1317
1318 /* Only scan and shadow the first workload in the queue
1319 * as there is only one pre-allocated buf-obj for shadow.
1320 */
1321 if (list_empty(workload_q_head(vgpu, ring_id))) {
1322 intel_runtime_pm_get(dev_priv);
1323 mutex_lock(&dev_priv->drm.struct_mutex);
1324 ret = intel_gvt_scan_and_shadow_workload(workload);
1325 mutex_unlock(&dev_priv->drm.struct_mutex);
1326 intel_runtime_pm_put(dev_priv);
1327 }
1328
1329 if (ret && (vgpu_is_vm_unhealthy(ret))) {
1330 enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR);
1331 intel_vgpu_destroy_workload(workload);
1332 return ERR_PTR(ret);
1333 }
1334
1335 return workload;
1336}
Changbin Du59a716c2017-11-29 15:40:06 +08001337
1338/**
1339 * intel_vgpu_queue_workload - Qeue a vGPU workload
1340 * @workload: the workload to queue in
1341 */
1342void intel_vgpu_queue_workload(struct intel_vgpu_workload *workload)
1343{
1344 list_add_tail(&workload->list,
1345 workload_q_head(workload->vgpu, workload->ring_id));
Changbin Duc1304562017-11-29 15:40:07 +08001346 intel_gvt_kick_schedule(workload->vgpu->gvt);
Changbin Du59a716c2017-11-29 15:40:06 +08001347 wake_up(&workload->vgpu->gvt->scheduler.waitq[workload->ring_id]);
1348}