blob: 3c2d8e9b2d3fae3cb0586d65bd2e8d9efe137483 [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
44void set_context_pdp_root_pointer(struct execlist_ring_context *ring_context,
45 u32 pdp[8])
46{
47 struct execlist_mmio_pair *pdp_pair = &ring_context->pdp3_UDW;
48 int i;
49
50 for (i = 0; i < 8; i++)
51 pdp_pair[i].val = pdp[7 - i];
52}
53
54static int populate_shadow_context(struct intel_vgpu_workload *workload)
55{
56 struct intel_vgpu *vgpu = workload->vgpu;
57 struct intel_gvt *gvt = vgpu->gvt;
58 int ring_id = workload->ring_id;
59 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
60 struct drm_i915_gem_object *ctx_obj =
61 shadow_ctx->engine[ring_id].state->obj;
62 struct execlist_ring_context *shadow_ring_context;
63 struct page *page;
64 void *dst;
65 unsigned long context_gpa, context_page_num;
66 int i;
67
68 gvt_dbg_sched("ring id %d workload lrca %x", ring_id,
69 workload->ctx_desc.lrca);
70
71 context_page_num = intel_lr_context_size(
Zhenyu Wang1140f9e2016-10-18 09:40:07 +080072 gvt->dev_priv->engine[ring_id]);
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) {
86 gvt_err("Invalid guest context descriptor\n");
87 return -EINVAL;
88 }
89
90 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
91 dst = kmap_atomic(page);
92 intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst,
93 GTT_PAGE_SIZE);
94 kunmap_atomic(dst);
95 i++;
96 }
97
98 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
99 shadow_ring_context = kmap_atomic(page);
100
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
125 kunmap_atomic(shadow_ring_context);
126 return 0;
127}
128
129static int shadow_context_status_change(struct notifier_block *nb,
130 unsigned long action, void *data)
131{
132 struct intel_vgpu *vgpu = container_of(nb,
133 struct intel_vgpu, shadow_ctx_notifier_block);
134 struct drm_i915_gem_request *req =
135 (struct drm_i915_gem_request *)data;
136 struct intel_gvt_workload_scheduler *scheduler =
137 &vgpu->gvt->scheduler;
138 struct intel_vgpu_workload *workload =
139 scheduler->current_workload[req->engine->id];
140
141 switch (action) {
142 case INTEL_CONTEXT_SCHEDULE_IN:
Zhi Wang17865712016-05-01 19:02:37 -0400143 intel_gvt_load_render_mmio(workload->vgpu,
144 workload->ring_id);
Zhi Wange4734052016-05-01 07:42:16 -0400145 atomic_set(&workload->shadow_ctx_active, 1);
146 break;
147 case INTEL_CONTEXT_SCHEDULE_OUT:
Zhi Wang17865712016-05-01 19:02:37 -0400148 intel_gvt_restore_render_mmio(workload->vgpu,
149 workload->ring_id);
Zhi Wange4734052016-05-01 07:42:16 -0400150 atomic_set(&workload->shadow_ctx_active, 0);
151 break;
152 default:
153 WARN_ON(1);
154 return NOTIFY_OK;
155 }
156 wake_up(&workload->shadow_ctx_status_wq);
157 return NOTIFY_OK;
158}
159
160static int dispatch_workload(struct intel_vgpu_workload *workload)
161{
162 struct intel_vgpu *vgpu = workload->vgpu;
163 struct intel_gvt *gvt = vgpu->gvt;
164 int ring_id = workload->ring_id;
165 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
166 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
Chris Wilson0eb742d2016-10-20 17:29:36 +0800167 struct drm_i915_gem_request *rq;
Zhi Wange4734052016-05-01 07:42:16 -0400168 int ret;
169
170 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
171 ring_id, workload);
172
173 shadow_ctx->desc_template = workload->ctx_desc.addressing_mode <<
174 GEN8_CTX_ADDRESSING_MODE_SHIFT;
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");
Chris Wilson0eb742d2016-10-20 17:29:36 +0800179 workload->status = PTR_ERR(rq);
Zhi Wange4734052016-05-01 07:42:16 -0400180 return workload->status;
181 }
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
187 mutex_lock(&gvt->lock);
188
Zhi Wangbe1da702016-05-03 18:26:57 -0400189 ret = intel_gvt_scan_and_shadow_workload(workload);
190 if (ret)
191 goto err;
192
193 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
194 if (ret)
195 goto err;
196
Zhi Wange4734052016-05-01 07:42:16 -0400197 ret = populate_shadow_context(workload);
198 if (ret)
199 goto err;
200
201 if (workload->prepare) {
202 ret = workload->prepare(workload);
203 if (ret)
204 goto err;
205 }
206
207 mutex_unlock(&gvt->lock);
208
209 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
210 ring_id, workload->req);
211
Chris Wilson0eb742d2016-10-20 17:29:36 +0800212 i915_add_request_no_flush(rq);
Zhi Wange4734052016-05-01 07:42:16 -0400213 workload->dispatched = true;
214 return 0;
215err:
216 workload->status = ret;
Zhi Wange4734052016-05-01 07:42:16 -0400217
218 mutex_unlock(&gvt->lock);
Chris Wilson0eb742d2016-10-20 17:29:36 +0800219
220 i915_add_request_no_flush(rq);
Zhi Wange4734052016-05-01 07:42:16 -0400221 return ret;
222}
223
224static struct intel_vgpu_workload *pick_next_workload(
225 struct intel_gvt *gvt, int ring_id)
226{
227 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
228 struct intel_vgpu_workload *workload = NULL;
229
230 mutex_lock(&gvt->lock);
231
232 /*
233 * no current vgpu / will be scheduled out / no workload
234 * bail out
235 */
236 if (!scheduler->current_vgpu) {
237 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
238 goto out;
239 }
240
241 if (scheduler->need_reschedule) {
242 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
243 goto out;
244 }
245
246 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) {
247 gvt_dbg_sched("ring id %d stop - no available workload\n",
248 ring_id);
249 goto out;
250 }
251
252 /*
253 * still have current workload, maybe the workload disptacher
254 * fail to submit it for some reason, resubmit it.
255 */
256 if (scheduler->current_workload[ring_id]) {
257 workload = scheduler->current_workload[ring_id];
258 gvt_dbg_sched("ring id %d still have current workload %p\n",
259 ring_id, workload);
260 goto out;
261 }
262
263 /*
264 * pick a workload as current workload
265 * once current workload is set, schedule policy routines
266 * will wait the current workload is finished when trying to
267 * schedule out a vgpu.
268 */
269 scheduler->current_workload[ring_id] = container_of(
270 workload_q_head(scheduler->current_vgpu, ring_id)->next,
271 struct intel_vgpu_workload, list);
272
273 workload = scheduler->current_workload[ring_id];
274
275 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
276
277 atomic_inc(&workload->vgpu->running_workload_num);
278out:
279 mutex_unlock(&gvt->lock);
280 return workload;
281}
282
283static void update_guest_context(struct intel_vgpu_workload *workload)
284{
285 struct intel_vgpu *vgpu = workload->vgpu;
286 struct intel_gvt *gvt = vgpu->gvt;
287 int ring_id = workload->ring_id;
288 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
289 struct drm_i915_gem_object *ctx_obj =
290 shadow_ctx->engine[ring_id].state->obj;
291 struct execlist_ring_context *shadow_ring_context;
292 struct page *page;
293 void *src;
294 unsigned long context_gpa, context_page_num;
295 int i;
296
297 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
298 workload->ctx_desc.lrca);
299
300 context_page_num = intel_lr_context_size(
Zhenyu Wang1140f9e2016-10-18 09:40:07 +0800301 gvt->dev_priv->engine[ring_id]);
Zhi Wange4734052016-05-01 07:42:16 -0400302
303 context_page_num = context_page_num >> PAGE_SHIFT;
304
305 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
306 context_page_num = 19;
307
308 i = 2;
309
310 while (i < context_page_num) {
311 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
312 (u32)((workload->ctx_desc.lrca + i) <<
313 GTT_PAGE_SHIFT));
314 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
315 gvt_err("invalid guest context descriptor\n");
316 return;
317 }
318
319 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
320 src = kmap_atomic(page);
321 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
322 GTT_PAGE_SIZE);
323 kunmap_atomic(src);
324 i++;
325 }
326
327 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
328 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
329
330 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
331 shadow_ring_context = kmap_atomic(page);
332
333#define COPY_REG(name) \
334 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
335 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
336
337 COPY_REG(ctx_ctrl);
338 COPY_REG(ctx_timestamp);
339
340#undef COPY_REG
341
342 intel_gvt_hypervisor_write_gpa(vgpu,
343 workload->ring_context_gpa +
344 sizeof(*shadow_ring_context),
345 (void *)shadow_ring_context +
346 sizeof(*shadow_ring_context),
347 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
348
349 kunmap_atomic(shadow_ring_context);
350}
351
352static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
353{
354 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
355 struct intel_vgpu_workload *workload;
Zhi Wangbe1da702016-05-03 18:26:57 -0400356 int event;
Zhi Wange4734052016-05-01 07:42:16 -0400357
358 mutex_lock(&gvt->lock);
359
360 workload = scheduler->current_workload[ring_id];
361
362 if (!workload->status && !workload->vgpu->resetting) {
363 wait_event(workload->shadow_ctx_status_wq,
364 !atomic_read(&workload->shadow_ctx_active));
365
366 update_guest_context(workload);
Zhi Wangbe1da702016-05-03 18:26:57 -0400367
368 for_each_set_bit(event, workload->pending_events,
369 INTEL_GVT_EVENT_MAX)
370 intel_vgpu_trigger_virtual_event(workload->vgpu,
371 event);
Zhi Wange4734052016-05-01 07:42:16 -0400372 }
373
374 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
375 ring_id, workload, workload->status);
376
377 scheduler->current_workload[ring_id] = NULL;
378
379 atomic_dec(&workload->vgpu->running_workload_num);
380
381 list_del_init(&workload->list);
382 workload->complete(workload);
383
384 wake_up(&scheduler->workload_complete_wq);
385 mutex_unlock(&gvt->lock);
386}
387
388struct workload_thread_param {
389 struct intel_gvt *gvt;
390 int ring_id;
391};
392
393static int workload_thread(void *priv)
394{
395 struct workload_thread_param *p = (struct workload_thread_param *)priv;
396 struct intel_gvt *gvt = p->gvt;
397 int ring_id = p->ring_id;
398 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
399 struct intel_vgpu_workload *workload = NULL;
400 int ret;
401 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv);
402
403 kfree(p);
404
405 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
406
407 while (!kthread_should_stop()) {
408 ret = wait_event_interruptible(scheduler->waitq[ring_id],
409 kthread_should_stop() ||
410 (workload = pick_next_workload(gvt, ring_id)));
411
412 WARN_ON_ONCE(ret);
413
414 if (kthread_should_stop())
415 break;
416
417 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
418 workload->ring_id, workload,
419 workload->vgpu->id);
420
421 intel_runtime_pm_get(gvt->dev_priv);
422
423 /*
424 * Always take i915 big lock first
425 */
426 ret = i915_mutex_lock_interruptible(&gvt->dev_priv->drm);
427 if (ret < 0) {
428 gvt_err("i915 submission is not available, retry\n");
429 schedule_timeout(1);
430 continue;
431 }
432
433 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
434 workload->ring_id, workload);
435
436 if (need_force_wake)
437 intel_uncore_forcewake_get(gvt->dev_priv,
438 FORCEWAKE_ALL);
439
440 ret = dispatch_workload(workload);
441 if (ret) {
442 gvt_err("fail to dispatch workload, skip\n");
443 goto complete;
444 }
445
446 gvt_dbg_sched("ring id %d wait workload %p\n",
447 workload->ring_id, workload);
448
449 workload->status = i915_wait_request(workload->req,
450 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
451 NULL, NULL);
452 if (workload->status != 0)
453 gvt_err("fail to wait workload, skip\n");
454
455complete:
456 gvt_dbg_sched("will complete workload %p\n, status: %d\n",
457 workload, workload->status);
458
459 complete_current_workload(gvt, ring_id);
460
Chris Wilson0eb742d2016-10-20 17:29:36 +0800461 i915_gem_request_put(fetch_and_zero(&workload->req));
462
Zhi Wange4734052016-05-01 07:42:16 -0400463 if (need_force_wake)
464 intel_uncore_forcewake_put(gvt->dev_priv,
465 FORCEWAKE_ALL);
466
467 mutex_unlock(&gvt->dev_priv->drm.struct_mutex);
468
469 intel_runtime_pm_put(gvt->dev_priv);
470 }
471 return 0;
472}
473
474void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
475{
476 struct intel_gvt *gvt = vgpu->gvt;
477 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
478
479 if (atomic_read(&vgpu->running_workload_num)) {
480 gvt_dbg_sched("wait vgpu idle\n");
481
482 wait_event(scheduler->workload_complete_wq,
483 !atomic_read(&vgpu->running_workload_num));
484 }
485}
486
487void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
488{
489 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
490 int i;
491
492 gvt_dbg_core("clean workload scheduler\n");
493
494 for (i = 0; i < I915_NUM_ENGINES; i++) {
495 if (scheduler->thread[i]) {
496 kthread_stop(scheduler->thread[i]);
497 scheduler->thread[i] = NULL;
498 }
499 }
500}
501
502int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
503{
504 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
505 struct workload_thread_param *param = NULL;
506 int ret;
507 int i;
508
509 gvt_dbg_core("init workload scheduler\n");
510
511 init_waitqueue_head(&scheduler->workload_complete_wq);
512
513 for (i = 0; i < I915_NUM_ENGINES; i++) {
514 init_waitqueue_head(&scheduler->waitq[i]);
515
516 param = kzalloc(sizeof(*param), GFP_KERNEL);
517 if (!param) {
518 ret = -ENOMEM;
519 goto err;
520 }
521
522 param->gvt = gvt;
523 param->ring_id = i;
524
525 scheduler->thread[i] = kthread_run(workload_thread, param,
526 "gvt workload %d", i);
527 if (IS_ERR(scheduler->thread[i])) {
528 gvt_err("fail to create workload thread\n");
529 ret = PTR_ERR(scheduler->thread[i]);
530 goto err;
531 }
532 }
533 return 0;
534err:
535 intel_gvt_clean_workload_scheduler(gvt);
536 kfree(param);
537 param = NULL;
538 return ret;
539}
540
541void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
542{
543 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
544
545 atomic_notifier_chain_unregister(&vgpu->shadow_ctx->status_notifier,
546 &vgpu->shadow_ctx_notifier_block);
547
548 mutex_lock(&dev_priv->drm.struct_mutex);
549
550 /* a little hacky to mark as ctx closed */
551 vgpu->shadow_ctx->closed = true;
552 i915_gem_context_put(vgpu->shadow_ctx);
553
554 mutex_unlock(&dev_priv->drm.struct_mutex);
555}
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
568 vgpu->shadow_ctx_notifier_block.notifier_call =
569 shadow_context_status_change;
570
571 atomic_notifier_chain_register(&vgpu->shadow_ctx->status_notifier,
572 &vgpu->shadow_ctx_notifier_block);
573 return 0;
574}