blob: bd59c6d093191ba9140a510342dedf9da47be8d9 [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;
60 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
61 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) <<
84 GTT_PAGE_SHIFT));
85 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -050086 gvt_vgpu_err("Invalid guest context descriptor\n");
Zhi Wange4734052016-05-01 07:42:16 -040087 return -EINVAL;
88 }
89
90 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + 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,
93 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),
123 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
124
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800125 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400126 return 0;
127}
128
Changbin Dubc2d4b62017-03-22 12:35:31 +0800129static inline bool is_gvt_request(struct drm_i915_gem_request *req)
130{
131 return i915_gem_context_force_single_submission(req->ctx);
132}
133
Zhi Wange4734052016-05-01 07:42:16 -0400134static int shadow_context_status_change(struct notifier_block *nb,
135 unsigned long action, void *data)
136{
Changbin Du3fc03062017-03-13 10:47:11 +0800137 struct drm_i915_gem_request *req = (struct drm_i915_gem_request *)data;
138 struct intel_gvt *gvt = container_of(nb, struct intel_gvt,
139 shadow_ctx_notifier_block[req->engine->id]);
140 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Changbin Du0e86cc92017-05-04 10:52:38 +0800141 enum intel_engine_id ring_id = req->engine->id;
142 struct intel_vgpu_workload *workload;
Zhi Wange4734052016-05-01 07:42:16 -0400143
Changbin Du0e86cc92017-05-04 10:52:38 +0800144 if (!is_gvt_request(req)) {
145 spin_lock_bh(&scheduler->mmio_context_lock);
146 if (action == INTEL_CONTEXT_SCHEDULE_IN &&
147 scheduler->engine_owner[ring_id]) {
148 /* Switch ring from vGPU to host. */
149 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
150 NULL, ring_id);
151 scheduler->engine_owner[ring_id] = NULL;
152 }
153 spin_unlock_bh(&scheduler->mmio_context_lock);
154
155 return NOTIFY_OK;
156 }
157
158 workload = scheduler->current_workload[ring_id];
159 if (unlikely(!workload))
Chuanxiao Dong9272f732017-02-17 19:29:52 +0800160 return NOTIFY_OK;
161
Zhi Wange4734052016-05-01 07:42:16 -0400162 switch (action) {
163 case INTEL_CONTEXT_SCHEDULE_IN:
Changbin Du0e86cc92017-05-04 10:52:38 +0800164 spin_lock_bh(&scheduler->mmio_context_lock);
165 if (workload->vgpu != scheduler->engine_owner[ring_id]) {
166 /* Switch ring from host to vGPU or vGPU to vGPU. */
167 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id],
168 workload->vgpu, ring_id);
169 scheduler->engine_owner[ring_id] = workload->vgpu;
170 } else
171 gvt_dbg_sched("skip ring %d mmio switch for vgpu%d\n",
172 ring_id, workload->vgpu->id);
173 spin_unlock_bh(&scheduler->mmio_context_lock);
Zhi Wange4734052016-05-01 07:42:16 -0400174 atomic_set(&workload->shadow_ctx_active, 1);
175 break;
176 case INTEL_CONTEXT_SCHEDULE_OUT:
Zhi Wange4734052016-05-01 07:42:16 -0400177 atomic_set(&workload->shadow_ctx_active, 0);
178 break;
179 default:
180 WARN_ON(1);
181 return NOTIFY_OK;
182 }
183 wake_up(&workload->shadow_ctx_status_wq);
184 return NOTIFY_OK;
185}
186
Ping Gao89ea20b2017-06-29 12:22:42 +0800187/**
188 * intel_gvt_scan_and_shadow_workload - audit the workload by scanning and
189 * shadow it as well, include ringbuffer,wa_ctx and ctx.
190 * @workload: an abstract entity for each execlist submission.
191 *
192 * This function is called before the workload submitting to i915, to make
193 * sure the content of the workload is valid.
194 */
195int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload)
Zhi Wange4734052016-05-01 07:42:16 -0400196{
Zhi Wange4734052016-05-01 07:42:16 -0400197 int ring_id = workload->ring_id;
198 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
199 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800200 struct drm_i915_gem_request *rq;
Tina Zhang695fbc02017-03-10 04:26:53 -0500201 struct intel_vgpu *vgpu = workload->vgpu;
Zhi Wange4734052016-05-01 07:42:16 -0400202 int ret;
203
Ping Gaod0302e72017-06-29 12:22:43 +0800204 if (workload->shadowed)
205 return 0;
206
Zhenyu Wang03806ed2017-02-13 17:07:19 +0800207 shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT);
208 shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode <<
Zhi Wange4734052016-05-01 07:42:16 -0400209 GEN8_CTX_ADDRESSING_MODE_SHIFT;
210
Chris Wilson0eb742d2016-10-20 17:29:36 +0800211 rq = i915_gem_request_alloc(dev_priv->engine[ring_id], shadow_ctx);
212 if (IS_ERR(rq)) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500213 gvt_vgpu_err("fail to allocate gem request\n");
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800214 ret = PTR_ERR(rq);
215 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400216 }
217
Chris Wilson0eb742d2016-10-20 17:29:36 +0800218 gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq);
219
220 workload->req = i915_gem_request_get(rq);
Zhi Wange4734052016-05-01 07:42:16 -0400221
Ping Gao89ea20b2017-06-29 12:22:42 +0800222 ret = intel_gvt_scan_and_shadow_ringbuffer(workload);
Zhi Wangbe1da702016-05-03 18:26:57 -0400223 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800224 goto out;
Zhi Wangbe1da702016-05-03 18:26:57 -0400225
Tina Zhang17f1b1a2017-03-15 23:16:01 -0400226 if ((workload->ring_id == RCS) &&
227 (workload->wa_ctx.indirect_ctx.size != 0)) {
228 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
229 if (ret)
230 goto out;
231 }
Zhi Wangbe1da702016-05-03 18:26:57 -0400232
Zhi Wange4734052016-05-01 07:42:16 -0400233 ret = populate_shadow_context(workload);
Ping Gaod0302e72017-06-29 12:22:43 +0800234 if (ret)
235 goto out;
236
237 workload->shadowed = true;
Ping Gao89ea20b2017-06-29 12:22:42 +0800238
239out:
240 return ret;
241}
242
243static int dispatch_workload(struct intel_vgpu_workload *workload)
244{
245 int ring_id = workload->ring_id;
246 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
247 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
248 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
249 struct intel_vgpu *vgpu = workload->vgpu;
250 struct intel_ring *ring;
251 int ret = 0;
252
253 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
254 ring_id, workload);
255
256 mutex_lock(&dev_priv->drm.struct_mutex);
257
258 ret = intel_gvt_scan_and_shadow_workload(workload);
Zhi Wange4734052016-05-01 07:42:16 -0400259 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800260 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400261
262 if (workload->prepare) {
263 ret = workload->prepare(workload);
264 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800265 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400266 }
267
Ping Gao89ea20b2017-06-29 12:22:42 +0800268 /* pin shadow context by gvt even the shadow context will be pinned
269 * when i915 alloc request. That is because gvt will update the guest
270 * context from shadow context when workload is completed, and at that
271 * moment, i915 may already unpined the shadow context to make the
272 * shadow_ctx pages invalid. So gvt need to pin itself. After update
273 * the guest context, gvt can unpin the shadow_ctx safely.
274 */
275 ring = engine->context_pin(engine, shadow_ctx);
276 if (IS_ERR(ring)) {
277 ret = PTR_ERR(ring);
278 gvt_vgpu_err("fail to pin shadow context\n");
279 goto out;
280 }
Zhi Wange4734052016-05-01 07:42:16 -0400281
Pei Zhang90d27a12016-11-14 18:02:57 +0800282out:
283 if (ret)
284 workload->status = ret;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800285
Ping Gao89ea20b2017-06-29 12:22:42 +0800286 if (!IS_ERR_OR_NULL(workload->req)) {
287 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
288 ring_id, workload->req);
289 i915_add_request(workload->req);
290 workload->dispatched = true;
291 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800292
Pei Zhang90d27a12016-11-14 18:02:57 +0800293 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400294 return ret;
295}
296
297static struct intel_vgpu_workload *pick_next_workload(
298 struct intel_gvt *gvt, int ring_id)
299{
300 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
301 struct intel_vgpu_workload *workload = NULL;
302
303 mutex_lock(&gvt->lock);
304
305 /*
306 * no current vgpu / will be scheduled out / no workload
307 * bail out
308 */
309 if (!scheduler->current_vgpu) {
310 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
311 goto out;
312 }
313
314 if (scheduler->need_reschedule) {
315 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
316 goto out;
317 }
318
Zhenyu Wang954180a2017-04-12 14:22:50 +0800319 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id)))
Zhi Wange4734052016-05-01 07:42:16 -0400320 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400321
322 /*
323 * still have current workload, maybe the workload disptacher
324 * fail to submit it for some reason, resubmit it.
325 */
326 if (scheduler->current_workload[ring_id]) {
327 workload = scheduler->current_workload[ring_id];
328 gvt_dbg_sched("ring id %d still have current workload %p\n",
329 ring_id, workload);
330 goto out;
331 }
332
333 /*
334 * pick a workload as current workload
335 * once current workload is set, schedule policy routines
336 * will wait the current workload is finished when trying to
337 * schedule out a vgpu.
338 */
339 scheduler->current_workload[ring_id] = container_of(
340 workload_q_head(scheduler->current_vgpu, ring_id)->next,
341 struct intel_vgpu_workload, list);
342
343 workload = scheduler->current_workload[ring_id];
344
345 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
346
347 atomic_inc(&workload->vgpu->running_workload_num);
348out:
349 mutex_unlock(&gvt->lock);
350 return workload;
351}
352
353static void update_guest_context(struct intel_vgpu_workload *workload)
354{
355 struct intel_vgpu *vgpu = workload->vgpu;
356 struct intel_gvt *gvt = vgpu->gvt;
357 int ring_id = workload->ring_id;
358 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
359 struct drm_i915_gem_object *ctx_obj =
360 shadow_ctx->engine[ring_id].state->obj;
361 struct execlist_ring_context *shadow_ring_context;
362 struct page *page;
363 void *src;
364 unsigned long context_gpa, context_page_num;
365 int i;
366
367 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
368 workload->ctx_desc.lrca);
369
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300370 context_page_num = gvt->dev_priv->engine[ring_id]->context_size;
Zhi Wange4734052016-05-01 07:42:16 -0400371
372 context_page_num = context_page_num >> PAGE_SHIFT;
373
374 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
375 context_page_num = 19;
376
377 i = 2;
378
379 while (i < context_page_num) {
380 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
381 (u32)((workload->ctx_desc.lrca + i) <<
382 GTT_PAGE_SHIFT));
383 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500384 gvt_vgpu_err("invalid guest context descriptor\n");
Zhi Wange4734052016-05-01 07:42:16 -0400385 return;
386 }
387
388 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800389 src = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400390 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
391 GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800392 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400393 i++;
394 }
395
396 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
397 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
398
399 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800400 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400401
402#define COPY_REG(name) \
403 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
404 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
405
406 COPY_REG(ctx_ctrl);
407 COPY_REG(ctx_timestamp);
408
409#undef COPY_REG
410
411 intel_gvt_hypervisor_write_gpa(vgpu,
412 workload->ring_context_gpa +
413 sizeof(*shadow_ring_context),
414 (void *)shadow_ring_context +
415 sizeof(*shadow_ring_context),
416 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
417
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800418 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400419}
420
421static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
422{
423 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
424 struct intel_vgpu_workload *workload;
Changbin Du440a9b92017-01-05 16:49:03 +0800425 struct intel_vgpu *vgpu;
Zhi Wangbe1da702016-05-03 18:26:57 -0400426 int event;
Zhi Wange4734052016-05-01 07:42:16 -0400427
428 mutex_lock(&gvt->lock);
429
430 workload = scheduler->current_workload[ring_id];
Changbin Du440a9b92017-01-05 16:49:03 +0800431 vgpu = workload->vgpu;
Zhi Wange4734052016-05-01 07:42:16 -0400432
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800433 /* For the workload w/ request, needs to wait for the context
434 * switch to make sure request is completed.
435 * For the workload w/o request, directly complete the workload.
436 */
437 if (workload->req) {
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800438 struct drm_i915_private *dev_priv =
439 workload->vgpu->gvt->dev_priv;
440 struct intel_engine_cs *engine =
441 dev_priv->engine[workload->ring_id];
Zhi Wange4734052016-05-01 07:42:16 -0400442 wait_event(workload->shadow_ctx_status_wq,
443 !atomic_read(&workload->shadow_ctx_active));
444
Chuanxiao Dong0cf5ec42017-06-23 13:01:11 +0800445 /* If this request caused GPU hang, req->fence.error will
446 * be set to -EIO. Use -EIO to set workload status so
447 * that when this request caused GPU hang, didn't trigger
448 * context switch interrupt to guest.
449 */
450 if (likely(workload->status == -EINPROGRESS)) {
451 if (workload->req->fence.error == -EIO)
452 workload->status = -EIO;
453 else
454 workload->status = 0;
455 }
456
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800457 i915_gem_request_put(fetch_and_zero(&workload->req));
Zhi Wangbe1da702016-05-03 18:26:57 -0400458
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800459 if (!workload->status && !vgpu->resetting) {
460 update_guest_context(workload);
461
462 for_each_set_bit(event, workload->pending_events,
463 INTEL_GVT_EVENT_MAX)
464 intel_vgpu_trigger_virtual_event(vgpu, event);
465 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800466 mutex_lock(&dev_priv->drm.struct_mutex);
467 /* unpin shadow ctx as the shadow_ctx update is done */
468 engine->context_unpin(engine, workload->vgpu->shadow_ctx);
469 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400470 }
471
472 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
473 ring_id, workload, workload->status);
474
475 scheduler->current_workload[ring_id] = NULL;
476
Zhi Wange4734052016-05-01 07:42:16 -0400477 list_del_init(&workload->list);
478 workload->complete(workload);
479
Changbin Du440a9b92017-01-05 16:49:03 +0800480 atomic_dec(&vgpu->running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400481 wake_up(&scheduler->workload_complete_wq);
Ping Gaof100dae2017-05-24 09:14:11 +0800482
483 if (gvt->scheduler.need_reschedule)
484 intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EVENT_SCHED);
485
Zhi Wange4734052016-05-01 07:42:16 -0400486 mutex_unlock(&gvt->lock);
487}
488
489struct workload_thread_param {
490 struct intel_gvt *gvt;
491 int ring_id;
492};
493
494static int workload_thread(void *priv)
495{
496 struct workload_thread_param *p = (struct workload_thread_param *)priv;
497 struct intel_gvt *gvt = p->gvt;
498 int ring_id = p->ring_id;
499 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
500 struct intel_vgpu_workload *workload = NULL;
Tina Zhang695fbc02017-03-10 04:26:53 -0500501 struct intel_vgpu *vgpu = NULL;
Zhi Wange4734052016-05-01 07:42:16 -0400502 int ret;
Xu Hane3476c02017-03-29 10:13:59 +0800503 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv)
504 || IS_KABYLAKE(gvt->dev_priv);
Du, Changbine45d7b72016-10-27 11:10:31 +0800505 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Zhi Wange4734052016-05-01 07:42:16 -0400506
507 kfree(p);
508
509 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
510
511 while (!kthread_should_stop()) {
Du, Changbine45d7b72016-10-27 11:10:31 +0800512 add_wait_queue(&scheduler->waitq[ring_id], &wait);
513 do {
514 workload = pick_next_workload(gvt, ring_id);
515 if (workload)
516 break;
517 wait_woken(&wait, TASK_INTERRUPTIBLE,
518 MAX_SCHEDULE_TIMEOUT);
519 } while (!kthread_should_stop());
520 remove_wait_queue(&scheduler->waitq[ring_id], &wait);
Zhi Wange4734052016-05-01 07:42:16 -0400521
Du, Changbine45d7b72016-10-27 11:10:31 +0800522 if (!workload)
Zhi Wange4734052016-05-01 07:42:16 -0400523 break;
524
525 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
526 workload->ring_id, workload,
527 workload->vgpu->id);
528
529 intel_runtime_pm_get(gvt->dev_priv);
530
Zhi Wange4734052016-05-01 07:42:16 -0400531 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
532 workload->ring_id, workload);
533
534 if (need_force_wake)
535 intel_uncore_forcewake_get(gvt->dev_priv,
536 FORCEWAKE_ALL);
537
Pei Zhang90d27a12016-11-14 18:02:57 +0800538 mutex_lock(&gvt->lock);
Zhi Wange4734052016-05-01 07:42:16 -0400539 ret = dispatch_workload(workload);
Pei Zhang90d27a12016-11-14 18:02:57 +0800540 mutex_unlock(&gvt->lock);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100541
Zhi Wange4734052016-05-01 07:42:16 -0400542 if (ret) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500543 vgpu = workload->vgpu;
544 gvt_vgpu_err("fail to dispatch workload, skip\n");
Zhi Wange4734052016-05-01 07:42:16 -0400545 goto complete;
546 }
547
548 gvt_dbg_sched("ring id %d wait workload %p\n",
549 workload->ring_id, workload);
Chris Wilson3dce2ac2017-03-08 22:08:08 +0000550 i915_wait_request(workload->req, 0, MAX_SCHEDULE_TIMEOUT);
Zhi Wange4734052016-05-01 07:42:16 -0400551
552complete:
Changbin Du3ce32742017-02-09 10:13:16 +0800553 gvt_dbg_sched("will complete workload %p, status: %d\n",
Zhi Wange4734052016-05-01 07:42:16 -0400554 workload, workload->status);
555
Changbin Du2e51ef32017-01-05 13:28:05 +0800556 complete_current_workload(gvt, ring_id);
557
Zhi Wange4734052016-05-01 07:42:16 -0400558 if (need_force_wake)
559 intel_uncore_forcewake_put(gvt->dev_priv,
560 FORCEWAKE_ALL);
561
Zhi Wange4734052016-05-01 07:42:16 -0400562 intel_runtime_pm_put(gvt->dev_priv);
Zhi Wange4734052016-05-01 07:42:16 -0400563 }
564 return 0;
565}
566
567void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
568{
569 struct intel_gvt *gvt = vgpu->gvt;
570 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
571
572 if (atomic_read(&vgpu->running_workload_num)) {
573 gvt_dbg_sched("wait vgpu idle\n");
574
575 wait_event(scheduler->workload_complete_wq,
576 !atomic_read(&vgpu->running_workload_num));
577 }
578}
579
580void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
581{
582 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Changbin Du3fc03062017-03-13 10:47:11 +0800583 struct intel_engine_cs *engine;
584 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400585
586 gvt_dbg_core("clean workload scheduler\n");
587
Changbin Du3fc03062017-03-13 10:47:11 +0800588 for_each_engine(engine, gvt->dev_priv, i) {
589 atomic_notifier_chain_unregister(
590 &engine->context_status_notifier,
591 &gvt->shadow_ctx_notifier_block[i]);
592 kthread_stop(scheduler->thread[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400593 }
594}
595
596int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
597{
598 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
599 struct workload_thread_param *param = NULL;
Changbin Du3fc03062017-03-13 10:47:11 +0800600 struct intel_engine_cs *engine;
601 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400602 int ret;
Zhi Wange4734052016-05-01 07:42:16 -0400603
604 gvt_dbg_core("init workload scheduler\n");
605
606 init_waitqueue_head(&scheduler->workload_complete_wq);
607
Changbin Du3fc03062017-03-13 10:47:11 +0800608 for_each_engine(engine, gvt->dev_priv, i) {
Zhi Wange4734052016-05-01 07:42:16 -0400609 init_waitqueue_head(&scheduler->waitq[i]);
610
611 param = kzalloc(sizeof(*param), GFP_KERNEL);
612 if (!param) {
613 ret = -ENOMEM;
614 goto err;
615 }
616
617 param->gvt = gvt;
618 param->ring_id = i;
619
620 scheduler->thread[i] = kthread_run(workload_thread, param,
621 "gvt workload %d", i);
622 if (IS_ERR(scheduler->thread[i])) {
623 gvt_err("fail to create workload thread\n");
624 ret = PTR_ERR(scheduler->thread[i]);
625 goto err;
626 }
Changbin Du3fc03062017-03-13 10:47:11 +0800627
628 gvt->shadow_ctx_notifier_block[i].notifier_call =
629 shadow_context_status_change;
630 atomic_notifier_chain_register(&engine->context_status_notifier,
631 &gvt->shadow_ctx_notifier_block[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400632 }
633 return 0;
634err:
635 intel_gvt_clean_workload_scheduler(gvt);
636 kfree(param);
637 param = NULL;
638 return ret;
639}
640
641void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
642{
Chris Wilson5f09a9c2017-06-20 12:05:46 +0100643 i915_gem_context_put(vgpu->shadow_ctx);
Zhi Wange4734052016-05-01 07:42:16 -0400644}
645
646int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu)
647{
648 atomic_set(&vgpu->running_workload_num, 0);
649
650 vgpu->shadow_ctx = i915_gem_context_create_gvt(
651 &vgpu->gvt->dev_priv->drm);
652 if (IS_ERR(vgpu->shadow_ctx))
653 return PTR_ERR(vgpu->shadow_ctx);
654
655 vgpu->shadow_ctx->engine[RCS].initialised = true;
656
Zhi Wange4734052016-05-01 07:42:16 -0400657 return 0;
658}