blob: 31d2240fdb1fac1124ee4b598ab51fc54f641b4d [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) {
87 gvt_err("Invalid guest context descriptor\n");
88 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
130static int shadow_context_status_change(struct notifier_block *nb,
131 unsigned long action, void *data)
132{
Changbin Du3fc03062017-03-13 10:47:11 +0800133 struct drm_i915_gem_request *req = (struct drm_i915_gem_request *)data;
134 struct intel_gvt *gvt = container_of(nb, struct intel_gvt,
135 shadow_ctx_notifier_block[req->engine->id]);
136 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Zhi Wange4734052016-05-01 07:42:16 -0400137 struct intel_vgpu_workload *workload =
138 scheduler->current_workload[req->engine->id];
139
140 switch (action) {
141 case INTEL_CONTEXT_SCHEDULE_IN:
Zhi Wang17865712016-05-01 19:02:37 -0400142 intel_gvt_load_render_mmio(workload->vgpu,
143 workload->ring_id);
Zhi Wange4734052016-05-01 07:42:16 -0400144 atomic_set(&workload->shadow_ctx_active, 1);
145 break;
146 case INTEL_CONTEXT_SCHEDULE_OUT:
Zhi Wang17865712016-05-01 19:02:37 -0400147 intel_gvt_restore_render_mmio(workload->vgpu,
148 workload->ring_id);
Zhi Wange4734052016-05-01 07:42:16 -0400149 atomic_set(&workload->shadow_ctx_active, 0);
150 break;
151 default:
152 WARN_ON(1);
153 return NOTIFY_OK;
154 }
155 wake_up(&workload->shadow_ctx_status_wq);
156 return NOTIFY_OK;
157}
158
159static int dispatch_workload(struct intel_vgpu_workload *workload)
160{
Zhi Wange4734052016-05-01 07:42:16 -0400161 int ring_id = workload->ring_id;
162 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
163 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800164 struct drm_i915_gem_request *rq;
Zhi Wange4734052016-05-01 07:42:16 -0400165 int ret;
166
167 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
168 ring_id, workload);
169
Zhenyu Wang03806ed2017-02-13 17:07:19 +0800170 shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT);
171 shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode <<
Zhi Wange4734052016-05-01 07:42:16 -0400172 GEN8_CTX_ADDRESSING_MODE_SHIFT;
173
Pei Zhang90d27a12016-11-14 18:02:57 +0800174 mutex_lock(&dev_priv->drm.struct_mutex);
175
Chris Wilson0eb742d2016-10-20 17:29:36 +0800176 rq = i915_gem_request_alloc(dev_priv->engine[ring_id], shadow_ctx);
177 if (IS_ERR(rq)) {
Zhi Wange4734052016-05-01 07:42:16 -0400178 gvt_err("fail to allocate gem request\n");
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800179 ret = PTR_ERR(rq);
180 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400181 }
182
Chris Wilson0eb742d2016-10-20 17:29:36 +0800183 gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq);
184
185 workload->req = i915_gem_request_get(rq);
Zhi Wange4734052016-05-01 07:42:16 -0400186
Zhi Wangbe1da702016-05-03 18:26:57 -0400187 ret = intel_gvt_scan_and_shadow_workload(workload);
188 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800189 goto out;
Zhi Wangbe1da702016-05-03 18:26:57 -0400190
191 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
192 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800193 goto out;
Zhi Wangbe1da702016-05-03 18:26:57 -0400194
Zhi Wange4734052016-05-01 07:42:16 -0400195 ret = populate_shadow_context(workload);
196 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800197 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400198
199 if (workload->prepare) {
200 ret = workload->prepare(workload);
201 if (ret)
Pei Zhang90d27a12016-11-14 18:02:57 +0800202 goto out;
Zhi Wange4734052016-05-01 07:42:16 -0400203 }
204
Zhi Wange4734052016-05-01 07:42:16 -0400205 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
206 ring_id, workload->req);
207
Pei Zhang90d27a12016-11-14 18:02:57 +0800208 ret = 0;
Zhi Wange4734052016-05-01 07:42:16 -0400209 workload->dispatched = true;
Pei Zhang90d27a12016-11-14 18:02:57 +0800210out:
211 if (ret)
212 workload->status = ret;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800213
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800214 if (!IS_ERR_OR_NULL(rq))
Chris Wilsone642c852017-03-17 11:47:09 +0000215 i915_add_request(rq);
Pei Zhang90d27a12016-11-14 18:02:57 +0800216 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhi Wange4734052016-05-01 07:42:16 -0400217 return ret;
218}
219
220static struct intel_vgpu_workload *pick_next_workload(
221 struct intel_gvt *gvt, int ring_id)
222{
223 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
224 struct intel_vgpu_workload *workload = NULL;
225
226 mutex_lock(&gvt->lock);
227
228 /*
229 * no current vgpu / will be scheduled out / no workload
230 * bail out
231 */
232 if (!scheduler->current_vgpu) {
233 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
234 goto out;
235 }
236
237 if (scheduler->need_reschedule) {
238 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
239 goto out;
240 }
241
242 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) {
243 gvt_dbg_sched("ring id %d stop - no available workload\n",
244 ring_id);
245 goto out;
246 }
247
248 /*
249 * still have current workload, maybe the workload disptacher
250 * fail to submit it for some reason, resubmit it.
251 */
252 if (scheduler->current_workload[ring_id]) {
253 workload = scheduler->current_workload[ring_id];
254 gvt_dbg_sched("ring id %d still have current workload %p\n",
255 ring_id, workload);
256 goto out;
257 }
258
259 /*
260 * pick a workload as current workload
261 * once current workload is set, schedule policy routines
262 * will wait the current workload is finished when trying to
263 * schedule out a vgpu.
264 */
265 scheduler->current_workload[ring_id] = container_of(
266 workload_q_head(scheduler->current_vgpu, ring_id)->next,
267 struct intel_vgpu_workload, list);
268
269 workload = scheduler->current_workload[ring_id];
270
271 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
272
273 atomic_inc(&workload->vgpu->running_workload_num);
274out:
275 mutex_unlock(&gvt->lock);
276 return workload;
277}
278
279static void update_guest_context(struct intel_vgpu_workload *workload)
280{
281 struct intel_vgpu *vgpu = workload->vgpu;
282 struct intel_gvt *gvt = vgpu->gvt;
283 int ring_id = workload->ring_id;
284 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
285 struct drm_i915_gem_object *ctx_obj =
286 shadow_ctx->engine[ring_id].state->obj;
287 struct execlist_ring_context *shadow_ring_context;
288 struct page *page;
289 void *src;
290 unsigned long context_gpa, context_page_num;
291 int i;
292
293 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
294 workload->ctx_desc.lrca);
295
296 context_page_num = intel_lr_context_size(
Zhenyu Wang1140f9e2016-10-18 09:40:07 +0800297 gvt->dev_priv->engine[ring_id]);
Zhi Wange4734052016-05-01 07:42:16 -0400298
299 context_page_num = context_page_num >> PAGE_SHIFT;
300
301 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
302 context_page_num = 19;
303
304 i = 2;
305
306 while (i < context_page_num) {
307 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
308 (u32)((workload->ctx_desc.lrca + i) <<
309 GTT_PAGE_SHIFT));
310 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
311 gvt_err("invalid guest context descriptor\n");
312 return;
313 }
314
315 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800316 src = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400317 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
318 GTT_PAGE_SIZE);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800319 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400320 i++;
321 }
322
323 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
324 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
325
326 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800327 shadow_ring_context = kmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400328
329#define COPY_REG(name) \
330 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
331 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
332
333 COPY_REG(ctx_ctrl);
334 COPY_REG(ctx_timestamp);
335
336#undef COPY_REG
337
338 intel_gvt_hypervisor_write_gpa(vgpu,
339 workload->ring_context_gpa +
340 sizeof(*shadow_ring_context),
341 (void *)shadow_ring_context +
342 sizeof(*shadow_ring_context),
343 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
344
Xiaoguang Chenc7549362016-11-03 18:38:30 +0800345 kunmap(page);
Zhi Wange4734052016-05-01 07:42:16 -0400346}
347
348static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
349{
350 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
351 struct intel_vgpu_workload *workload;
Changbin Du440a9b92017-01-05 16:49:03 +0800352 struct intel_vgpu *vgpu;
Zhi Wangbe1da702016-05-03 18:26:57 -0400353 int event;
Zhi Wange4734052016-05-01 07:42:16 -0400354
355 mutex_lock(&gvt->lock);
356
357 workload = scheduler->current_workload[ring_id];
Changbin Du440a9b92017-01-05 16:49:03 +0800358 vgpu = workload->vgpu;
Zhi Wange4734052016-05-01 07:42:16 -0400359
Changbin Du440a9b92017-01-05 16:49:03 +0800360 if (!workload->status && !vgpu->resetting) {
Zhi Wange4734052016-05-01 07:42:16 -0400361 wait_event(workload->shadow_ctx_status_wq,
362 !atomic_read(&workload->shadow_ctx_active));
363
364 update_guest_context(workload);
Zhi Wangbe1da702016-05-03 18:26:57 -0400365
366 for_each_set_bit(event, workload->pending_events,
367 INTEL_GVT_EVENT_MAX)
Changbin Du440a9b92017-01-05 16:49:03 +0800368 intel_vgpu_trigger_virtual_event(vgpu, event);
Zhi Wange4734052016-05-01 07:42:16 -0400369 }
370
371 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
372 ring_id, workload, workload->status);
373
374 scheduler->current_workload[ring_id] = NULL;
375
Zhi Wange4734052016-05-01 07:42:16 -0400376 list_del_init(&workload->list);
377 workload->complete(workload);
378
Changbin Du440a9b92017-01-05 16:49:03 +0800379 atomic_dec(&vgpu->running_workload_num);
Zhi Wange4734052016-05-01 07:42:16 -0400380 wake_up(&scheduler->workload_complete_wq);
381 mutex_unlock(&gvt->lock);
382}
383
384struct workload_thread_param {
385 struct intel_gvt *gvt;
386 int ring_id;
387};
388
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100389static DEFINE_MUTEX(scheduler_mutex);
390
Zhi Wange4734052016-05-01 07:42:16 -0400391static int workload_thread(void *priv)
392{
393 struct workload_thread_param *p = (struct workload_thread_param *)priv;
394 struct intel_gvt *gvt = p->gvt;
395 int ring_id = p->ring_id;
396 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
397 struct intel_vgpu_workload *workload = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +0100398 long lret;
Zhi Wange4734052016-05-01 07:42:16 -0400399 int ret;
400 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv);
Du, Changbine45d7b72016-10-27 11:10:31 +0800401 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Zhi Wange4734052016-05-01 07:42:16 -0400402
403 kfree(p);
404
405 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
406
407 while (!kthread_should_stop()) {
Du, Changbine45d7b72016-10-27 11:10:31 +0800408 add_wait_queue(&scheduler->waitq[ring_id], &wait);
409 do {
410 workload = pick_next_workload(gvt, ring_id);
411 if (workload)
412 break;
413 wait_woken(&wait, TASK_INTERRUPTIBLE,
414 MAX_SCHEDULE_TIMEOUT);
415 } while (!kthread_should_stop());
416 remove_wait_queue(&scheduler->waitq[ring_id], &wait);
Zhi Wange4734052016-05-01 07:42:16 -0400417
Du, Changbine45d7b72016-10-27 11:10:31 +0800418 if (!workload)
Zhi Wange4734052016-05-01 07:42:16 -0400419 break;
420
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100421 mutex_lock(&scheduler_mutex);
422
Zhi Wange4734052016-05-01 07:42:16 -0400423 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
424 workload->ring_id, workload,
425 workload->vgpu->id);
426
427 intel_runtime_pm_get(gvt->dev_priv);
428
Zhi Wange4734052016-05-01 07:42:16 -0400429 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
430 workload->ring_id, workload);
431
432 if (need_force_wake)
433 intel_uncore_forcewake_get(gvt->dev_priv,
434 FORCEWAKE_ALL);
435
Pei Zhang90d27a12016-11-14 18:02:57 +0800436 mutex_lock(&gvt->lock);
Zhi Wange4734052016-05-01 07:42:16 -0400437 ret = dispatch_workload(workload);
Pei Zhang90d27a12016-11-14 18:02:57 +0800438 mutex_unlock(&gvt->lock);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100439
Zhi Wange4734052016-05-01 07:42:16 -0400440 if (ret) {
441 gvt_err("fail to dispatch workload, skip\n");
442 goto complete;
443 }
444
445 gvt_dbg_sched("ring id %d wait workload %p\n",
446 workload->ring_id, workload);
447
Chris Wilsone95433c2016-10-28 13:58:27 +0100448 lret = i915_wait_request(workload->req,
449 0, MAX_SCHEDULE_TIMEOUT);
450 if (lret < 0) {
451 workload->status = lret;
Zhi Wange4734052016-05-01 07:42:16 -0400452 gvt_err("fail to wait workload, skip\n");
Zhenyu Wang9b172342016-11-02 15:00:15 +0800453 } else {
454 workload->status = 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100455 }
Zhi Wange4734052016-05-01 07:42:16 -0400456
457complete:
Changbin Du3ce32742017-02-09 10:13:16 +0800458 gvt_dbg_sched("will complete workload %p, status: %d\n",
Zhi Wange4734052016-05-01 07:42:16 -0400459 workload, workload->status);
460
Zhenyu Wang53d6f8122016-11-24 15:55:49 +0800461 if (workload->req)
462 i915_gem_request_put(fetch_and_zero(&workload->req));
Chris Wilson0eb742d2016-10-20 17:29:36 +0800463
Changbin Du2e51ef32017-01-05 13:28:05 +0800464 complete_current_workload(gvt, ring_id);
465
Zhi Wange4734052016-05-01 07:42:16 -0400466 if (need_force_wake)
467 intel_uncore_forcewake_put(gvt->dev_priv,
468 FORCEWAKE_ALL);
469
Zhi Wange4734052016-05-01 07:42:16 -0400470 intel_runtime_pm_put(gvt->dev_priv);
Chris Wilson66bbc3b2016-10-19 11:11:44 +0100471
472 mutex_unlock(&scheduler_mutex);
473
Zhi Wange4734052016-05-01 07:42:16 -0400474 }
475 return 0;
476}
477
478void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
479{
480 struct intel_gvt *gvt = vgpu->gvt;
481 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
482
483 if (atomic_read(&vgpu->running_workload_num)) {
484 gvt_dbg_sched("wait vgpu idle\n");
485
486 wait_event(scheduler->workload_complete_wq,
487 !atomic_read(&vgpu->running_workload_num));
488 }
489}
490
491void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
492{
493 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
Changbin Du3fc03062017-03-13 10:47:11 +0800494 struct intel_engine_cs *engine;
495 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400496
497 gvt_dbg_core("clean workload scheduler\n");
498
Changbin Du3fc03062017-03-13 10:47:11 +0800499 for_each_engine(engine, gvt->dev_priv, i) {
500 atomic_notifier_chain_unregister(
501 &engine->context_status_notifier,
502 &gvt->shadow_ctx_notifier_block[i]);
503 kthread_stop(scheduler->thread[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400504 }
505}
506
507int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
508{
509 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
510 struct workload_thread_param *param = NULL;
Changbin Du3fc03062017-03-13 10:47:11 +0800511 struct intel_engine_cs *engine;
512 enum intel_engine_id i;
Zhi Wange4734052016-05-01 07:42:16 -0400513 int ret;
Zhi Wange4734052016-05-01 07:42:16 -0400514
515 gvt_dbg_core("init workload scheduler\n");
516
517 init_waitqueue_head(&scheduler->workload_complete_wq);
518
Changbin Du3fc03062017-03-13 10:47:11 +0800519 for_each_engine(engine, gvt->dev_priv, i) {
Zhi Wange4734052016-05-01 07:42:16 -0400520 init_waitqueue_head(&scheduler->waitq[i]);
521
522 param = kzalloc(sizeof(*param), GFP_KERNEL);
523 if (!param) {
524 ret = -ENOMEM;
525 goto err;
526 }
527
528 param->gvt = gvt;
529 param->ring_id = i;
530
531 scheduler->thread[i] = kthread_run(workload_thread, param,
532 "gvt workload %d", i);
533 if (IS_ERR(scheduler->thread[i])) {
534 gvt_err("fail to create workload thread\n");
535 ret = PTR_ERR(scheduler->thread[i]);
536 goto err;
537 }
Changbin Du3fc03062017-03-13 10:47:11 +0800538
539 gvt->shadow_ctx_notifier_block[i].notifier_call =
540 shadow_context_status_change;
541 atomic_notifier_chain_register(&engine->context_status_notifier,
542 &gvt->shadow_ctx_notifier_block[i]);
Zhi Wange4734052016-05-01 07:42:16 -0400543 }
544 return 0;
545err:
546 intel_gvt_clean_workload_scheduler(gvt);
547 kfree(param);
548 param = NULL;
549 return ret;
550}
551
552void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
553{
Chris Wilson70ffe992016-12-18 15:37:22 +0000554 i915_gem_context_put_unlocked(vgpu->shadow_ctx);
Zhi Wange4734052016-05-01 07:42:16 -0400555}
556
557int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu)
558{
559 atomic_set(&vgpu->running_workload_num, 0);
560
561 vgpu->shadow_ctx = i915_gem_context_create_gvt(
562 &vgpu->gvt->dev_priv->drm);
563 if (IS_ERR(vgpu->shadow_ctx))
564 return PTR_ERR(vgpu->shadow_ctx);
565
566 vgpu->shadow_ctx->engine[RCS].initialised = true;
567
Zhi Wange4734052016-05-01 07:42:16 -0400568 return 0;
569}