blob: d0c04155f7d5b469525405815deb20e57534f6c9 [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
72 context_page_num = intel_lr_context_size(
Zhenyu Wang1140f9e2016-10-18 09:40:07 +080073 gvt->dev_priv->engine[ring_id]);
Zhi Wange4734052016-05-01 07:42:16 -040074
75 context_page_num = context_page_num >> PAGE_SHIFT;
76
77 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
78 context_page_num = 19;
79
80 i = 2;
81
82 while (i < context_page_num) {
83 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
84 (u32)((workload->ctx_desc.lrca + i) <<
85 GTT_PAGE_SHIFT));
86 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -050087 gvt_vgpu_err("Invalid guest context descriptor\n");
Zhi Wange4734052016-05-01 07:42:16 -040088 return -EINVAL;
89 }
90
91 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +080092 dst = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -040093 intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst,
94 GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +080095 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -040096 i++;
97 }
98
99 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800100 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400101
102#define COPY_REG(name) \
103 intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \
104 + RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
105
106 COPY_REG(ctx_ctrl);
107 COPY_REG(ctx_timestamp);
108
109 if (ring_id == RCS) {
110 COPY_REG(bb_per_ctx_ptr);
111 COPY_REG(rcs_indirect_ctx);
112 COPY_REG(rcs_indirect_ctx_offset);
113 }
114#undef COPY_REG
115
116 set_context_pdp_root_pointer(shadow_ring_context,
117 workload->shadow_mm->shadow_page_table);
118
119 intel_gvt_hypervisor_read_gpa(vgpu,
120 workload->ring_context_gpa +
121 sizeof(*shadow_ring_context),
122 (void *)shadow_ring_context +
123 sizeof(*shadow_ring_context),
124 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
125
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800126 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400127 return 0;
128}
129
Changbin Dubc2d4b62017-03-22 12:35:31 +0800130static inline bool is_gvt_request(struct drm_i915_gem_request *req)
131{
132 return i915_gem_context_force_single_submission(req->ctx);
133}
134
Zhi Wange4734052016-05-01 07:42:16 -0400135static int shadow_context_status_change(struct notifier_block *nb,
136 unsigned long action, void *data)
137{
138 struct intel_vgpu *vgpu = container_of(nb,
139 struct intel_vgpu, shadow_ctx_notifier_block);
140 struct drm_i915_gem_request *req =
141 (struct drm_i915_gem_request *)data;
142 struct intel_gvt_workload_scheduler *scheduler =
143 &vgpu->gvt->scheduler;
144 struct intel_vgpu_workload *workload =
145 scheduler->current_workload[req->engine->id];
146
Changbin Dubc2d4b62017-03-22 12:35:31 +0800147 if (!is_gvt_request(req) || unlikely(!workload))
Chuanxiao Dong9272f732017-02-17 19:29:52 +0800148 return NOTIFY_OK;
149
Zhi Wange4734052016-05-01 07:42:16 -0400150 switch (action) {
151 case INTEL_CONTEXT_SCHEDULE_IN:
Zhi Wang17865712016-05-01 19:02:37 -0400152 intel_gvt_load_render_mmio(workload->vgpu,
153 workload->ring_id);
Zhi Wange4734052016-05-01 07:42:16 -0400154 atomic_set(&workload->shadow_ctx_active, 1);
155 break;
156 case INTEL_CONTEXT_SCHEDULE_OUT:
Zhi Wang17865712016-05-01 19:02:37 -0400157 intel_gvt_restore_render_mmio(workload->vgpu,
158 workload->ring_id);
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800159 /* If the status is -EINPROGRESS means this workload
160 * doesn't meet any issue during dispatching so when
161 * get the SCHEDULE_OUT set the status to be zero for
162 * good. If the status is NOT -EINPROGRESS means there
163 * is something wrong happened during dispatching and
164 * the status should not be set to zero
165 */
166 if (workload->status == -EINPROGRESS)
167 workload->status = 0;
Zhi Wange4734052016-05-01 07:42:16 -0400168 atomic_set(&workload->shadow_ctx_active, 0);
169 break;
170 default:
171 WARN_ON(1);
172 return NOTIFY_OK;
173 }
174 wake_up(&workload->shadow_ctx_status_wq);
175 return NOTIFY_OK;
176}
177
178static int dispatch_workload(struct intel_vgpu_workload *workload)
179{
Zhi Wange4734052016-05-01 07:42:16 -0400180 int ring_id = workload->ring_id;
181 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
182 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800183 struct intel_engine_cs *engine = dev_priv->engine[ring_id];
Chris Wilson0eb742d2016-10-20 17:29:36 +0800184 struct drm_i915_gem_request *rq;
Tina Zhang695fbc02017-03-10 04:26:53 -0500185 struct intel_vgpu *vgpu = workload->vgpu;
Zhi Wange4734052016-05-01 07:42:16 -0400186 int ret;
187
188 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
189 ring_id, workload);
190
Zhenyu Wang03806ed2017-02-13 17:07:19 +0800191 shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT);
192 shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode <<
Zhi Wange4734052016-05-01 07:42:16 -0400193 GEN8_CTX_ADDRESSING_MODE_SHIFT;
194
Pei Zhang90d27a12016-11-14 18:02:57 +0800195 mutex_lock(&dev_priv->drm.struct_mutex);
196
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800197 /* pin shadow context by gvt even the shadow context will be pinned
198 * when i915 alloc request. That is because gvt will update the guest
199 * context from shadow context when workload is completed, and at that
200 * moment, i915 may already unpined the shadow context to make the
201 * shadow_ctx pages invalid. So gvt need to pin itself. After update
202 * the guest context, gvt can unpin the shadow_ctx safely.
203 */
204 ret = engine->context_pin(engine, shadow_ctx);
205 if (ret) {
206 gvt_vgpu_err("fail to pin shadow context\n");
207 workload->status = ret;
208 mutex_unlock(&dev_priv->drm.struct_mutex);
209 return ret;
210 }
211
Chris Wilson0eb742d2016-10-20 17:29:36 +0800212 rq = i915_gem_request_alloc(dev_priv->engine[ring_id], shadow_ctx);
213 if (IS_ERR(rq)) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500214 gvt_vgpu_err("fail to allocate gem request\n");
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800215 ret = PTR_ERR(rq);
216 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400217 }
218
Chris Wilson0eb742d2016-10-20 17:29:36 +0800219 gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq);
220
221 workload->req = i915_gem_request_get(rq);
Zhi Wange4734052016-05-01 07:42:16 -0400222
Zhi Wangbe1da702016-05-03 18:26:57 -0400223 ret = intel_gvt_scan_and_shadow_workload(workload);
224 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800225 goto out;
Zhi Wangbe1da702016-05-03 18:26:57 -0400226
Tina Zhang17f1b1a2017-03-15 23:16:01 -0400227 if ((workload->ring_id == RCS) &&
228 (workload->wa_ctx.indirect_ctx.size != 0)) {
229 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
230 if (ret)
231 goto out;
232 }
Zhi Wangbe1da702016-05-03 18:26:57 -0400233
Zhi Wange4734052016-05-01 07:42:16 -0400234 ret = populate_shadow_context(workload);
235 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800236 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400237
238 if (workload->prepare) {
239 ret = workload->prepare(workload);
240 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800241 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400242 }
243
Zhi Wange4734052016-05-01 07:42:16 -0400244 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
245 ring_id, workload->req);
246
Pei Zhang90d27a12016-11-14 18:02:57 +0800247 ret = 0;
Zhi Wange4734052016-05-01 07:42:16 -0400248 workload->dispatched = true;
Pei Zhang90d27a12016-11-14 18:02:57 +0800249out:
250 if (ret)
251 workload->status = ret;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800252
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800253 if (!IS_ERR_OR_NULL(rq))
254 i915_add_request_no_flush(rq);
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800255 else
256 engine->context_unpin(engine, shadow_ctx);
257
Pei Zhang90d27a12016-11-14 18:02:57 +0800258 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400259 return ret;
260}
261
262static struct intel_vgpu_workload *pick_next_workload(
263 struct intel_gvt *gvt, int ring_id)
264{
265 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
266 struct intel_vgpu_workload *workload = NULL;
267
268 mutex_lock(&gvt->lock);
269
270 /*
271 * no current vgpu / will be scheduled out / no workload
272 * bail out
273 */
274 if (!scheduler->current_vgpu) {
275 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
276 goto out;
277 }
278
279 if (scheduler->need_reschedule) {
280 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
281 goto out;
282 }
283
284 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) {
285 gvt_dbg_sched("ring id %d stop - no available workload\n",
286 ring_id);
287 goto out;
288 }
289
290 /*
291 * still have current workload, maybe the workload disptacher
292 * fail to submit it for some reason, resubmit it.
293 */
294 if (scheduler->current_workload[ring_id]) {
295 workload = scheduler->current_workload[ring_id];
296 gvt_dbg_sched("ring id %d still have current workload %p\n",
297 ring_id, workload);
298 goto out;
299 }
300
301 /*
302 * pick a workload as current workload
303 * once current workload is set, schedule policy routines
304 * will wait the current workload is finished when trying to
305 * schedule out a vgpu.
306 */
307 scheduler->current_workload[ring_id] = container_of(
308 workload_q_head(scheduler->current_vgpu, ring_id)->next,
309 struct intel_vgpu_workload, list);
310
311 workload = scheduler->current_workload[ring_id];
312
313 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
314
315 atomic_inc(&workload->vgpu->running_workload_num);
316out:
317 mutex_unlock(&gvt->lock);
318 return workload;
319}
320
321static void update_guest_context(struct intel_vgpu_workload *workload)
322{
323 struct intel_vgpu *vgpu = workload->vgpu;
324 struct intel_gvt *gvt = vgpu->gvt;
325 int ring_id = workload->ring_id;
326 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
327 struct drm_i915_gem_object *ctx_obj =
328 shadow_ctx->engine[ring_id].state->obj;
329 struct execlist_ring_context *shadow_ring_context;
330 struct page *page;
331 void *src;
332 unsigned long context_gpa, context_page_num;
333 int i;
334
335 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
336 workload->ctx_desc.lrca);
337
338 context_page_num = intel_lr_context_size(
Zhenyu Wang1140f9e2016-10-18 09:40:07 +0800339 gvt->dev_priv->engine[ring_id]);
Zhi Wange4734052016-05-01 07:42:16 -0400340
341 context_page_num = context_page_num >> PAGE_SHIFT;
342
343 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
344 context_page_num = 19;
345
346 i = 2;
347
348 while (i < context_page_num) {
349 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
350 (u32)((workload->ctx_desc.lrca + i) <<
351 GTT_PAGE_SHIFT));
352 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500353 gvt_vgpu_err("invalid guest context descriptor\n");
Zhi Wange4734052016-05-01 07:42:16 -0400354 return;
355 }
356
357 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800358 src = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400359 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
360 GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800361 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400362 i++;
363 }
364
365 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
366 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
367
368 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800369 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400370
371#define COPY_REG(name) \
372 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
373 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
374
375 COPY_REG(ctx_ctrl);
376 COPY_REG(ctx_timestamp);
377
378#undef COPY_REG
379
380 intel_gvt_hypervisor_write_gpa(vgpu,
381 workload->ring_context_gpa +
382 sizeof(*shadow_ring_context),
383 (void *)shadow_ring_context +
384 sizeof(*shadow_ring_context),
385 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
386
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800387 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400388}
389
390static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
391{
392 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
393 struct intel_vgpu_workload *workload;
Changbin Du440a9b92017-01-05 16:49:03 +0800394 struct intel_vgpu *vgpu;
Zhi Wangbe1da702016-05-03 18:26:57 -0400395 int event;
Zhi Wange4734052016-05-01 07:42:16 -0400396
397 mutex_lock(&gvt->lock);
398
399 workload = scheduler->current_workload[ring_id];
Changbin Du440a9b92017-01-05 16:49:03 +0800400 vgpu = workload->vgpu;
Zhi Wange4734052016-05-01 07:42:16 -0400401
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800402 /* For the workload w/ request, needs to wait for the context
403 * switch to make sure request is completed.
404 * For the workload w/o request, directly complete the workload.
405 */
406 if (workload->req) {
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800407 struct drm_i915_private *dev_priv =
408 workload->vgpu->gvt->dev_priv;
409 struct intel_engine_cs *engine =
410 dev_priv->engine[workload->ring_id];
Zhi Wange4734052016-05-01 07:42:16 -0400411 wait_event(workload->shadow_ctx_status_wq,
412 !atomic_read(&workload->shadow_ctx_active));
413
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800414 i915_gem_request_put(fetch_and_zero(&workload->req));
Zhi Wangbe1da702016-05-03 18:26:57 -0400415
Chuanxiao Dong8f1117a2017-03-06 13:05:24 +0800416 if (!workload->status && !vgpu->resetting) {
417 update_guest_context(workload);
418
419 for_each_set_bit(event, workload->pending_events,
420 INTEL_GVT_EVENT_MAX)
421 intel_vgpu_trigger_virtual_event(vgpu, event);
422 }
Chuanxiao Dong3cd23b82017-03-16 09:47:58 +0800423 mutex_lock(&dev_priv->drm.struct_mutex);
424 /* unpin shadow ctx as the shadow_ctx update is done */
425 engine->context_unpin(engine, workload->vgpu->shadow_ctx);
426 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400427 }
428
429 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
430 ring_id, workload, workload->status);
431
432 scheduler->current_workload[ring_id] = NULL;
433
Zhi Wange4734052016-05-01 07:42:16 -0400434 list_del_init(&workload->list);
435 workload->complete(workload);
436
Changbin Du440a9b92017-01-05 16:49:03 +0800437 atomic_dec(&vgpu->running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400438 wake_up(&scheduler->workload_complete_wq);
439 mutex_unlock(&gvt->lock);
440}
441
442struct workload_thread_param {
443 struct intel_gvt *gvt;
444 int ring_id;
445};
446
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100447static DEFINE_MUTEX(scheduler_mutex);
448
Zhi Wange4734052016-05-01 07:42:16 -0400449static int workload_thread(void *priv)
450{
451 struct workload_thread_param *p = (struct workload_thread_param *)priv;
452 struct intel_gvt *gvt = p->gvt;
453 int ring_id = p->ring_id;
454 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
455 struct intel_vgpu_workload *workload = NULL;
Tina Zhang695fbc02017-03-10 04:26:53 -0500456 struct intel_vgpu *vgpu = NULL;
Zhi Wange4734052016-05-01 07:42:16 -0400457 int ret;
458 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv);
Du, Changbine45d7b72016-10-27 11:10:31 +0800459 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Zhi Wange4734052016-05-01 07:42:16 -0400460
461 kfree(p);
462
463 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
464
465 while (!kthread_should_stop()) {
Du, Changbine45d7b72016-10-27 11:10:31 +0800466 add_wait_queue(&scheduler->waitq[ring_id], &wait);
467 do {
468 workload = pick_next_workload(gvt, ring_id);
469 if (workload)
470 break;
471 wait_woken(&wait, TASK_INTERRUPTIBLE,
472 MAX_SCHEDULE_TIMEOUT);
473 } while (!kthread_should_stop());
474 remove_wait_queue(&scheduler->waitq[ring_id], &wait);
Zhi Wange4734052016-05-01 07:42:16 -0400475
Du, Changbine45d7b72016-10-27 11:10:31 +0800476 if (!workload)
Zhi Wange4734052016-05-01 07:42:16 -0400477 break;
478
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100479 mutex_lock(&scheduler_mutex);
480
Zhi Wange4734052016-05-01 07:42:16 -0400481 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
482 workload->ring_id, workload,
483 workload->vgpu->id);
484
485 intel_runtime_pm_get(gvt->dev_priv);
486
Zhi Wange4734052016-05-01 07:42:16 -0400487 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
488 workload->ring_id, workload);
489
490 if (need_force_wake)
491 intel_uncore_forcewake_get(gvt->dev_priv,
492 FORCEWAKE_ALL);
493
Pei Zhang90d27a12016-11-14 18:02:57 +0800494 mutex_lock(&gvt->lock);
Zhi Wange4734052016-05-01 07:42:16 -0400495 ret = dispatch_workload(workload);
Pei Zhang90d27a12016-11-14 18:02:57 +0800496 mutex_unlock(&gvt->lock);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100497
Zhi Wange4734052016-05-01 07:42:16 -0400498 if (ret) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500499 vgpu = workload->vgpu;
500 gvt_vgpu_err("fail to dispatch workload, skip\n");
Zhi Wange4734052016-05-01 07:42:16 -0400501 goto complete;
502 }
503
504 gvt_dbg_sched("ring id %d wait workload %p\n",
505 workload->ring_id, workload);
Chris Wilson3dce2ac2017-03-08 22:08:08 +0000506 i915_wait_request(workload->req, 0, MAX_SCHEDULE_TIMEOUT);
Zhi Wange4734052016-05-01 07:42:16 -0400507
508complete:
Changbin Du3ce32742017-02-09 10:13:16 +0800509 gvt_dbg_sched("will complete workload %p, status: %d\n",
Zhi Wange4734052016-05-01 07:42:16 -0400510 workload, workload->status);
511
Changbin Du2e51ef32017-01-05 13:28:05 +0800512 complete_current_workload(gvt, ring_id);
513
Zhi Wange4734052016-05-01 07:42:16 -0400514 if (need_force_wake)
515 intel_uncore_forcewake_put(gvt->dev_priv,
516 FORCEWAKE_ALL);
517
Zhi Wange4734052016-05-01 07:42:16 -0400518 intel_runtime_pm_put(gvt->dev_priv);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100519
520 mutex_unlock(&scheduler_mutex);
521
Zhi Wange4734052016-05-01 07:42:16 -0400522 }
523 return 0;
524}
525
526void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
527{
528 struct intel_gvt *gvt = vgpu->gvt;
529 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
530
531 if (atomic_read(&vgpu->running_workload_num)) {
532 gvt_dbg_sched("wait vgpu idle\n");
533
534 wait_event(scheduler->workload_complete_wq,
535 !atomic_read(&vgpu->running_workload_num));
536 }
537}
538
539void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
540{
541 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
542 int i;
543
544 gvt_dbg_core("clean workload scheduler\n");
545
546 for (i = 0; i < I915_NUM_ENGINES; i++) {
547 if (scheduler->thread[i]) {
548 kthread_stop(scheduler->thread[i]);
549 scheduler->thread[i] = NULL;
550 }
551 }
552}
553
554int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
555{
556 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
557 struct workload_thread_param *param = NULL;
558 int ret;
559 int i;
560
561 gvt_dbg_core("init workload scheduler\n");
562
563 init_waitqueue_head(&scheduler->workload_complete_wq);
564
565 for (i = 0; i < I915_NUM_ENGINES; i++) {
Zhenyu Wang0fac21e2016-10-20 13:30:33 +0800566 /* check ring mask at init time */
567 if (!HAS_ENGINE(gvt->dev_priv, i))
568 continue;
569
Zhi Wange4734052016-05-01 07:42:16 -0400570 init_waitqueue_head(&scheduler->waitq[i]);
571
572 param = kzalloc(sizeof(*param), GFP_KERNEL);
573 if (!param) {
574 ret = -ENOMEM;
575 goto err;
576 }
577
578 param->gvt = gvt;
579 param->ring_id = i;
580
581 scheduler->thread[i] = kthread_run(workload_thread, param,
582 "gvt workload %d", i);
583 if (IS_ERR(scheduler->thread[i])) {
584 gvt_err("fail to create workload thread\n");
585 ret = PTR_ERR(scheduler->thread[i]);
586 goto err;
587 }
588 }
589 return 0;
590err:
591 intel_gvt_clean_workload_scheduler(gvt);
592 kfree(param);
593 param = NULL;
594 return ret;
595}
596
597void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
598{
Zhi Wange4734052016-05-01 07:42:16 -0400599 atomic_notifier_chain_unregister(&vgpu->shadow_ctx->status_notifier,
600 &vgpu->shadow_ctx_notifier_block);
601
Chris Wilson70ffe992016-12-18 15:37:22 +0000602 i915_gem_context_put_unlocked(vgpu->shadow_ctx);
Zhi Wange4734052016-05-01 07:42:16 -0400603}
604
605int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu)
606{
607 atomic_set(&vgpu->running_workload_num, 0);
608
609 vgpu->shadow_ctx = i915_gem_context_create_gvt(
610 &vgpu->gvt->dev_priv->drm);
611 if (IS_ERR(vgpu->shadow_ctx))
612 return PTR_ERR(vgpu->shadow_ctx);
613
614 vgpu->shadow_ctx->engine[RCS].initialised = true;
615
616 vgpu->shadow_ctx_notifier_block.notifier_call =
617 shadow_context_status_change;
618
619 atomic_notifier_chain_register(&vgpu->shadow_ctx->status_notifier,
620 &vgpu->shadow_ctx_notifier_block);
621 return 0;
622}