blob: 488fdea348a979e411258d6317748fe2bdd5e242 [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:
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800177 /* If the status is -EINPROGRESS means this workload
178 * doesn't meet any issue during dispatching so when
179 * get the SCHEDULE_OUT set the status to be zero for
180 * good. If the status is NOT -EINPROGRESS means there
181 * is something wrong happened during dispatching and
182 * the status should not be set to zero
183 */
184 if (workload->status == -EINPROGRESS)
185 workload->status = 0;
Zhi Wange4734052016-05-01 07:42:16 -0400186 atomic_set(&workload->shadow_ctx_active, 0);
187 break;
188 default:
189 WARN_ON(1);
190 return NOTIFY_OK;
191 }
192 wake_up(&workload->shadow_ctx_status_wq);
193 return NOTIFY_OK;
194}
195
196static int dispatch_workload(struct intel_vgpu_workload *workload)
197{
Zhi Wange4734052016-05-01 07:42:16 -0400198 int ring_id = workload->ring_id;
199 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
200 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800201 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
Chris Wilson0eb742d2016-10-20 17:29:36 +0800202 struct drm_i915_gem_request *rq;
Tina Zhang695fbc02017-03-10 04:26:53 -0500203 struct intel_vgpu *vgpu = workload->vgpu;
Chris Wilson266a2402017-05-04 10:33:08 +0100204 struct intel_ring *ring;
Zhi Wange4734052016-05-01 07:42:16 -0400205 int ret;
206
207 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
208 ring_id, workload);
209
Zhenyu Wang03806ed2017-02-13 17:07:19 +0800210 shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT);
211 shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode <<
Zhi Wange4734052016-05-01 07:42:16 -0400212 GEN8_CTX_ADDRESSING_MODE_SHIFT;
213
Pei Zhang90d27a12016-11-14 18:02:57 +0800214 mutex_lock(&dev_priv->drm.struct_mutex);
215
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800216 /* pin shadow context by gvt even the shadow context will be pinned
217 * when i915 alloc request. That is because gvt will update the guest
218 * context from shadow context when workload is completed, and at that
219 * moment, i915 may already unpined the shadow context to make the
220 * shadow_ctx pages invalid. So gvt need to pin itself. After update
221 * the guest context, gvt can unpin the shadow_ctx safely.
222 */
Chris Wilson266a2402017-05-04 10:33:08 +0100223 ring = engine->context_pin(engine, shadow_ctx);
224 if (IS_ERR(ring)) {
225 ret = PTR_ERR(ring);
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800226 gvt_vgpu_err("fail to pin shadow context\n");
227 workload->status = ret;
228 mutex_unlock(&dev_priv->drm.struct_mutex);
229 return ret;
230 }
231
Chris Wilson0eb742d2016-10-20 17:29:36 +0800232 rq = i915_gem_request_alloc(dev_priv->engine[ring_id], shadow_ctx);
233 if (IS_ERR(rq)) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500234 gvt_vgpu_err("fail to allocate gem request\n");
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800235 ret = PTR_ERR(rq);
236 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400237 }
238
Chris Wilson0eb742d2016-10-20 17:29:36 +0800239 gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq);
240
241 workload->req = i915_gem_request_get(rq);
Zhi Wange4734052016-05-01 07:42:16 -0400242
Zhi Wangbe1da702016-05-03 18:26:57 -0400243 ret = intel_gvt_scan_and_shadow_workload(workload);
244 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800245 goto out;
Zhi Wangbe1da702016-05-03 18:26:57 -0400246
Tina Zhang17f1b1a2017-03-15 23:16:01 -0400247 if ((workload->ring_id == RCS) &&
248 (workload->wa_ctx.indirect_ctx.size != 0)) {
249 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
250 if (ret)
251 goto out;
252 }
Zhi Wangbe1da702016-05-03 18:26:57 -0400253
Zhi Wange4734052016-05-01 07:42:16 -0400254 ret = populate_shadow_context(workload);
255 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800256 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400257
258 if (workload->prepare) {
259 ret = workload->prepare(workload);
260 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800261 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400262 }
263
Zhi Wange4734052016-05-01 07:42:16 -0400264 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
265 ring_id, workload->req);
266
Pei Zhang90d27a12016-11-14 18:02:57 +0800267 ret = 0;
Zhi Wange4734052016-05-01 07:42:16 -0400268 workload->dispatched = true;
Pei Zhang90d27a12016-11-14 18:02:57 +0800269out:
270 if (ret)
271 workload->status = ret;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800272
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800273 if (!IS_ERR_OR_NULL(rq))
Chris Wilsone642c852017-03-17 11:47:09 +0000274 i915_add_request(rq);
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800275 else
276 engine->context_unpin(engine, shadow_ctx);
277
Pei Zhang90d27a12016-11-14 18:02:57 +0800278 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400279 return ret;
280}
281
282static struct intel_vgpu_workload *pick_next_workload(
283 struct intel_gvt *gvt, int ring_id)
284{
285 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
286 struct intel_vgpu_workload *workload = NULL;
287
288 mutex_lock(&gvt->lock);
289
290 /*
291 * no current vgpu / will be scheduled out / no workload
292 * bail out
293 */
294 if (!scheduler->current_vgpu) {
295 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
296 goto out;
297 }
298
299 if (scheduler->need_reschedule) {
300 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
301 goto out;
302 }
303
Zhenyu Wang954180a2017-04-12 14:22:50 +0800304 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id)))
Zhi Wange4734052016-05-01 07:42:16 -0400305 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400306
307 /*
308 * still have current workload, maybe the workload disptacher
309 * fail to submit it for some reason, resubmit it.
310 */
311 if (scheduler->current_workload[ring_id]) {
312 workload = scheduler->current_workload[ring_id];
313 gvt_dbg_sched("ring id %d still have current workload %p\n",
314 ring_id, workload);
315 goto out;
316 }
317
318 /*
319 * pick a workload as current workload
320 * once current workload is set, schedule policy routines
321 * will wait the current workload is finished when trying to
322 * schedule out a vgpu.
323 */
324 scheduler->current_workload[ring_id] = container_of(
325 workload_q_head(scheduler->current_vgpu, ring_id)->next,
326 struct intel_vgpu_workload, list);
327
328 workload = scheduler->current_workload[ring_id];
329
330 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
331
332 atomic_inc(&workload->vgpu->running_workload_num);
333out:
334 mutex_unlock(&gvt->lock);
335 return workload;
336}
337
338static void update_guest_context(struct intel_vgpu_workload *workload)
339{
340 struct intel_vgpu *vgpu = workload->vgpu;
341 struct intel_gvt *gvt = vgpu->gvt;
342 int ring_id = workload->ring_id;
343 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
344 struct drm_i915_gem_object *ctx_obj =
345 shadow_ctx->engine[ring_id].state->obj;
346 struct execlist_ring_context *shadow_ring_context;
347 struct page *page;
348 void *src;
349 unsigned long context_gpa, context_page_num;
350 int i;
351
352 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
353 workload->ctx_desc.lrca);
354
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300355 context_page_num = gvt->dev_priv->engine[ring_id]->context_size;
Zhi Wange4734052016-05-01 07:42:16 -0400356
357 context_page_num = context_page_num >> PAGE_SHIFT;
358
359 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
360 context_page_num = 19;
361
362 i = 2;
363
364 while (i < context_page_num) {
365 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
366 (u32)((workload->ctx_desc.lrca + i) <<
367 GTT_PAGE_SHIFT));
368 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500369 gvt_vgpu_err("invalid guest context descriptor\n");
Zhi Wange4734052016-05-01 07:42:16 -0400370 return;
371 }
372
373 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800374 src = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400375 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
376 GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800377 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400378 i++;
379 }
380
381 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
382 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
383
384 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800385 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400386
387#define COPY_REG(name) \
388 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
389 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
390
391 COPY_REG(ctx_ctrl);
392 COPY_REG(ctx_timestamp);
393
394#undef COPY_REG
395
396 intel_gvt_hypervisor_write_gpa(vgpu,
397 workload->ring_context_gpa +
398 sizeof(*shadow_ring_context),
399 (void *)shadow_ring_context +
400 sizeof(*shadow_ring_context),
401 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
402
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800403 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400404}
405
406static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
407{
408 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
409 struct intel_vgpu_workload *workload;
Changbin Du440a9b92017-01-05 16:49:03 +0800410 struct intel_vgpu *vgpu;
Zhi Wangbe1da702016-05-03 18:26:57 -0400411 int event;
Zhi Wange4734052016-05-01 07:42:16 -0400412
413 mutex_lock(&gvt->lock);
414
415 workload = scheduler->current_workload[ring_id];
Changbin Du440a9b92017-01-05 16:49:03 +0800416 vgpu = workload->vgpu;
Zhi Wange4734052016-05-01 07:42:16 -0400417
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800418 /* For the workload w/ request, needs to wait for the context
419 * switch to make sure request is completed.
420 * For the workload w/o request, directly complete the workload.
421 */
422 if (workload->req) {
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800423 struct drm_i915_private *dev_priv =
424 workload->vgpu->gvt->dev_priv;
425 struct intel_engine_cs *engine =
426 dev_priv->engine[workload->ring_id];
Zhi Wange4734052016-05-01 07:42:16 -0400427 wait_event(workload->shadow_ctx_status_wq,
428 !atomic_read(&workload->shadow_ctx_active));
429
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800430 i915_gem_request_put(fetch_and_zero(&workload->req));
Zhi Wangbe1da702016-05-03 18:26:57 -0400431
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800432 if (!workload->status && !vgpu->resetting) {
433 update_guest_context(workload);
434
435 for_each_set_bit(event, workload->pending_events,
436 INTEL_GVT_EVENT_MAX)
437 intel_vgpu_trigger_virtual_event(vgpu, event);
438 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800439 mutex_lock(&dev_priv->drm.struct_mutex);
440 /* unpin shadow ctx as the shadow_ctx update is done */
441 engine->context_unpin(engine, workload->vgpu->shadow_ctx);
442 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400443 }
444
445 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
446 ring_id, workload, workload->status);
447
448 scheduler->current_workload[ring_id] = NULL;
449
Zhi Wange4734052016-05-01 07:42:16 -0400450 list_del_init(&workload->list);
451 workload->complete(workload);
452
Changbin Du440a9b92017-01-05 16:49:03 +0800453 atomic_dec(&vgpu->running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400454 wake_up(&scheduler->workload_complete_wq);
Ping Gaof100dae2017-05-24 09:14:11 +0800455
456 if (gvt->scheduler.need_reschedule)
457 intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EVENT_SCHED);
458
Zhi Wange4734052016-05-01 07:42:16 -0400459 mutex_unlock(&gvt->lock);
460}
461
462struct workload_thread_param {
463 struct intel_gvt *gvt;
464 int ring_id;
465};
466
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100467static DEFINE_MUTEX(scheduler_mutex);
468
Zhi Wange4734052016-05-01 07:42:16 -0400469static int workload_thread(void *priv)
470{
471 struct workload_thread_param *p = (struct workload_thread_param *)priv;
472 struct intel_gvt *gvt = p->gvt;
473 int ring_id = p->ring_id;
474 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
475 struct intel_vgpu_workload *workload = NULL;
Tina Zhang695fbc02017-03-10 04:26:53 -0500476 struct intel_vgpu *vgpu = NULL;
Zhi Wange4734052016-05-01 07:42:16 -0400477 int ret;
Xu Hane3476c02017-03-29 10:13:59 +0800478 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv)
479 || IS_KABYLAKE(gvt->dev_priv);
Du, Changbine45d7b72016-10-27 11:10:31 +0800480 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Zhi Wange4734052016-05-01 07:42:16 -0400481
482 kfree(p);
483
484 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
485
486 while (!kthread_should_stop()) {
Du, Changbine45d7b72016-10-27 11:10:31 +0800487 add_wait_queue(&scheduler->waitq[ring_id], &wait);
488 do {
489 workload = pick_next_workload(gvt, ring_id);
490 if (workload)
491 break;
492 wait_woken(&wait, TASK_INTERRUPTIBLE,
493 MAX_SCHEDULE_TIMEOUT);
494 } while (!kthread_should_stop());
495 remove_wait_queue(&scheduler->waitq[ring_id], &wait);
Zhi Wange4734052016-05-01 07:42:16 -0400496
Du, Changbine45d7b72016-10-27 11:10:31 +0800497 if (!workload)
Zhi Wange4734052016-05-01 07:42:16 -0400498 break;
499
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100500 mutex_lock(&scheduler_mutex);
501
Zhi Wange4734052016-05-01 07:42:16 -0400502 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
503 workload->ring_id, workload,
504 workload->vgpu->id);
505
506 intel_runtime_pm_get(gvt->dev_priv);
507
Zhi Wange4734052016-05-01 07:42:16 -0400508 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
509 workload->ring_id, workload);
510
511 if (need_force_wake)
512 intel_uncore_forcewake_get(gvt->dev_priv,
513 FORCEWAKE_ALL);
514
Pei Zhang90d27a12016-11-14 18:02:57 +0800515 mutex_lock(&gvt->lock);
Zhi Wange4734052016-05-01 07:42:16 -0400516 ret = dispatch_workload(workload);
Pei Zhang90d27a12016-11-14 18:02:57 +0800517 mutex_unlock(&gvt->lock);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100518
Zhi Wange4734052016-05-01 07:42:16 -0400519 if (ret) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500520 vgpu = workload->vgpu;
521 gvt_vgpu_err("fail to dispatch workload, skip\n");
Zhi Wange4734052016-05-01 07:42:16 -0400522 goto complete;
523 }
524
525 gvt_dbg_sched("ring id %d wait workload %p\n",
526 workload->ring_id, workload);
Chris Wilson3dce2ac2017-03-08 22:08:08 +0000527 i915_wait_request(workload->req, 0, MAX_SCHEDULE_TIMEOUT);
Zhi Wange4734052016-05-01 07:42:16 -0400528
529complete:
Changbin Du3ce32742017-02-09 10:13:16 +0800530 gvt_dbg_sched("will complete workload %p, status: %d\n",
Zhi Wange4734052016-05-01 07:42:16 -0400531 workload, workload->status);
532
Changbin Du2e51ef32017-01-05 13:28:05 +0800533 complete_current_workload(gvt, ring_id);
534
Zhi Wange4734052016-05-01 07:42:16 -0400535 if (need_force_wake)
536 intel_uncore_forcewake_put(gvt->dev_priv,
537 FORCEWAKE_ALL);
538
Zhi Wange4734052016-05-01 07:42:16 -0400539 intel_runtime_pm_put(gvt->dev_priv);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100540
541 mutex_unlock(&scheduler_mutex);
542
Zhi Wange4734052016-05-01 07:42:16 -0400543 }
544 return 0;
545}
546
547void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
548{
549 struct intel_gvt *gvt = vgpu->gvt;
550 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
551
552 if (atomic_read(&vgpu->running_workload_num)) {
553 gvt_dbg_sched("wait vgpu idle\n");
554
555 wait_event(scheduler->workload_complete_wq,
556 !atomic_read(&vgpu->running_workload_num));
557 }
558}
559
560void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
561{
562 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Changbin Du3fc03062017-03-13 10:47:11 +0800563 struct intel_engine_cs *engine;
564 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400565
566 gvt_dbg_core("clean workload scheduler\n");
567
Changbin Du3fc03062017-03-13 10:47:11 +0800568 for_each_engine(engine, gvt->dev_priv, i) {
569 atomic_notifier_chain_unregister(
570 &engine->context_status_notifier,
571 &gvt->shadow_ctx_notifier_block[i]);
572 kthread_stop(scheduler->thread[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400573 }
574}
575
576int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
577{
578 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
579 struct workload_thread_param *param = NULL;
Changbin Du3fc03062017-03-13 10:47:11 +0800580 struct intel_engine_cs *engine;
581 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400582 int ret;
Zhi Wange4734052016-05-01 07:42:16 -0400583
584 gvt_dbg_core("init workload scheduler\n");
585
586 init_waitqueue_head(&scheduler->workload_complete_wq);
587
Changbin Du3fc03062017-03-13 10:47:11 +0800588 for_each_engine(engine, gvt->dev_priv, i) {
Zhi Wange4734052016-05-01 07:42:16 -0400589 init_waitqueue_head(&scheduler->waitq[i]);
590
591 param = kzalloc(sizeof(*param), GFP_KERNEL);
592 if (!param) {
593 ret = -ENOMEM;
594 goto err;
595 }
596
597 param->gvt = gvt;
598 param->ring_id = i;
599
600 scheduler->thread[i] = kthread_run(workload_thread, param,
601 "gvt workload %d", i);
602 if (IS_ERR(scheduler->thread[i])) {
603 gvt_err("fail to create workload thread\n");
604 ret = PTR_ERR(scheduler->thread[i]);
605 goto err;
606 }
Changbin Du3fc03062017-03-13 10:47:11 +0800607
608 gvt->shadow_ctx_notifier_block[i].notifier_call =
609 shadow_context_status_change;
610 atomic_notifier_chain_register(&engine->context_status_notifier,
611 &gvt->shadow_ctx_notifier_block[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400612 }
613 return 0;
614err:
615 intel_gvt_clean_workload_scheduler(gvt);
616 kfree(param);
617 param = NULL;
618 return ret;
619}
620
621void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
622{
Chris Wilson70ffe992016-12-18 15:37:22 +0000623 i915_gem_context_put_unlocked(vgpu->shadow_ctx);
Zhi Wange4734052016-05-01 07:42:16 -0400624}
625
626int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu)
627{
628 atomic_set(&vgpu->running_workload_num, 0);
629
630 vgpu->shadow_ctx = i915_gem_context_create_gvt(
631 &vgpu->gvt->dev_priv->drm);
632 if (IS_ERR(vgpu->shadow_ctx))
633 return PTR_ERR(vgpu->shadow_ctx);
634
635 vgpu->shadow_ctx->engine[RCS].initialised = true;
636
Zhi Wange4734052016-05-01 07:42:16 -0400637 return 0;
638}