2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
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:
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
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
24 * Zhi Wang <zhi.a.wang@intel.com>
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>
38 #include <linux/kthread.h>
40 #define RING_CTX_OFF(x) \
41 offsetof(struct execlist_ring_context, x)
43 void set_context_pdp_root_pointer(struct execlist_ring_context *ring_context,
46 struct execlist_mmio_pair *pdp_pair = &ring_context->pdp3_UDW;
49 for (i = 0; i < 8; i++)
50 pdp_pair[i].val = pdp[7 - i];
53 static int populate_shadow_context(struct intel_vgpu_workload *workload)
55 struct intel_vgpu *vgpu = workload->vgpu;
56 struct intel_gvt *gvt = vgpu->gvt;
57 int ring_id = workload->ring_id;
58 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
59 struct drm_i915_gem_object *ctx_obj =
60 shadow_ctx->engine[ring_id].state->obj;
61 struct execlist_ring_context *shadow_ring_context;
64 unsigned long context_gpa, context_page_num;
67 gvt_dbg_sched("ring id %d workload lrca %x", ring_id,
68 workload->ctx_desc.lrca);
70 context_page_num = intel_lr_context_size(
71 &gvt->dev_priv->engine[ring_id]);
73 context_page_num = context_page_num >> PAGE_SHIFT;
75 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
76 context_page_num = 19;
80 while (i < context_page_num) {
81 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
82 (u32)((workload->ctx_desc.lrca + i) <<
84 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
85 gvt_err("Invalid guest context descriptor\n");
89 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
90 dst = kmap_atomic(page);
91 intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst,
97 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
98 shadow_ring_context = kmap_atomic(page);
100 #define COPY_REG(name) \
101 intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \
102 + RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
105 COPY_REG(ctx_timestamp);
107 if (ring_id == RCS) {
108 COPY_REG(bb_per_ctx_ptr);
109 COPY_REG(rcs_indirect_ctx);
110 COPY_REG(rcs_indirect_ctx_offset);
114 set_context_pdp_root_pointer(shadow_ring_context,
115 workload->shadow_mm->shadow_page_table);
117 intel_gvt_hypervisor_read_gpa(vgpu,
118 workload->ring_context_gpa +
119 sizeof(*shadow_ring_context),
120 (void *)shadow_ring_context +
121 sizeof(*shadow_ring_context),
122 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
124 kunmap_atomic(shadow_ring_context);
128 static int shadow_context_status_change(struct notifier_block *nb,
129 unsigned long action, void *data)
131 struct intel_vgpu *vgpu = container_of(nb,
132 struct intel_vgpu, shadow_ctx_notifier_block);
133 struct drm_i915_gem_request *req =
134 (struct drm_i915_gem_request *)data;
135 struct intel_gvt_workload_scheduler *scheduler =
136 &vgpu->gvt->scheduler;
137 struct intel_vgpu_workload *workload =
138 scheduler->current_workload[req->engine->id];
141 case INTEL_CONTEXT_SCHEDULE_IN:
142 intel_gvt_load_render_mmio(workload->vgpu,
144 atomic_set(&workload->shadow_ctx_active, 1);
146 case INTEL_CONTEXT_SCHEDULE_OUT:
147 intel_gvt_restore_render_mmio(workload->vgpu,
149 atomic_set(&workload->shadow_ctx_active, 0);
155 wake_up(&workload->shadow_ctx_status_wq);
159 static int dispatch_workload(struct intel_vgpu_workload *workload)
161 struct intel_vgpu *vgpu = workload->vgpu;
162 struct intel_gvt *gvt = vgpu->gvt;
163 int ring_id = workload->ring_id;
164 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
165 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
168 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
171 shadow_ctx->desc_template = workload->ctx_desc.addressing_mode <<
172 GEN8_CTX_ADDRESSING_MODE_SHIFT;
174 workload->req = i915_gem_request_alloc(&dev_priv->engine[ring_id],
176 if (IS_ERR_OR_NULL(workload->req)) {
177 gvt_err("fail to allocate gem request\n");
178 workload->status = PTR_ERR(workload->req);
179 workload->req = NULL;
180 return workload->status;
183 gvt_dbg_sched("ring id %d get i915 gem request %p\n",
184 ring_id, workload->req);
186 mutex_lock(&gvt->lock);
188 ret = populate_shadow_context(workload);
192 if (workload->prepare) {
193 ret = workload->prepare(workload);
198 mutex_unlock(&gvt->lock);
200 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
201 ring_id, workload->req);
203 i915_add_request_no_flush(workload->req);
205 workload->dispatched = true;
208 workload->status = ret;
210 workload->req = NULL;
212 mutex_unlock(&gvt->lock);
216 static struct intel_vgpu_workload *pick_next_workload(
217 struct intel_gvt *gvt, int ring_id)
219 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
220 struct intel_vgpu_workload *workload = NULL;
222 mutex_lock(&gvt->lock);
225 * no current vgpu / will be scheduled out / no workload
228 if (!scheduler->current_vgpu) {
229 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
233 if (scheduler->need_reschedule) {
234 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
238 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) {
239 gvt_dbg_sched("ring id %d stop - no available workload\n",
245 * still have current workload, maybe the workload disptacher
246 * fail to submit it for some reason, resubmit it.
248 if (scheduler->current_workload[ring_id]) {
249 workload = scheduler->current_workload[ring_id];
250 gvt_dbg_sched("ring id %d still have current workload %p\n",
256 * pick a workload as current workload
257 * once current workload is set, schedule policy routines
258 * will wait the current workload is finished when trying to
259 * schedule out a vgpu.
261 scheduler->current_workload[ring_id] = container_of(
262 workload_q_head(scheduler->current_vgpu, ring_id)->next,
263 struct intel_vgpu_workload, list);
265 workload = scheduler->current_workload[ring_id];
267 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
269 atomic_inc(&workload->vgpu->running_workload_num);
271 mutex_unlock(&gvt->lock);
275 static void update_guest_context(struct intel_vgpu_workload *workload)
277 struct intel_vgpu *vgpu = workload->vgpu;
278 struct intel_gvt *gvt = vgpu->gvt;
279 int ring_id = workload->ring_id;
280 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
281 struct drm_i915_gem_object *ctx_obj =
282 shadow_ctx->engine[ring_id].state->obj;
283 struct execlist_ring_context *shadow_ring_context;
286 unsigned long context_gpa, context_page_num;
289 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
290 workload->ctx_desc.lrca);
292 context_page_num = intel_lr_context_size(
293 &gvt->dev_priv->engine[ring_id]);
295 context_page_num = context_page_num >> PAGE_SHIFT;
297 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
298 context_page_num = 19;
302 while (i < context_page_num) {
303 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
304 (u32)((workload->ctx_desc.lrca + i) <<
306 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
307 gvt_err("invalid guest context descriptor\n");
311 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
312 src = kmap_atomic(page);
313 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
319 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
320 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
322 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
323 shadow_ring_context = kmap_atomic(page);
325 #define COPY_REG(name) \
326 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
327 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
330 COPY_REG(ctx_timestamp);
334 intel_gvt_hypervisor_write_gpa(vgpu,
335 workload->ring_context_gpa +
336 sizeof(*shadow_ring_context),
337 (void *)shadow_ring_context +
338 sizeof(*shadow_ring_context),
339 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
341 kunmap_atomic(shadow_ring_context);
344 static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
346 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
347 struct intel_vgpu_workload *workload;
349 mutex_lock(&gvt->lock);
351 workload = scheduler->current_workload[ring_id];
353 if (!workload->status && !workload->vgpu->resetting) {
354 wait_event(workload->shadow_ctx_status_wq,
355 !atomic_read(&workload->shadow_ctx_active));
357 update_guest_context(workload);
360 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
361 ring_id, workload, workload->status);
363 scheduler->current_workload[ring_id] = NULL;
365 atomic_dec(&workload->vgpu->running_workload_num);
367 list_del_init(&workload->list);
368 workload->complete(workload);
370 wake_up(&scheduler->workload_complete_wq);
371 mutex_unlock(&gvt->lock);
374 struct workload_thread_param {
375 struct intel_gvt *gvt;
379 static int workload_thread(void *priv)
381 struct workload_thread_param *p = (struct workload_thread_param *)priv;
382 struct intel_gvt *gvt = p->gvt;
383 int ring_id = p->ring_id;
384 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
385 struct intel_vgpu_workload *workload = NULL;
387 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv);
391 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
393 while (!kthread_should_stop()) {
394 ret = wait_event_interruptible(scheduler->waitq[ring_id],
395 kthread_should_stop() ||
396 (workload = pick_next_workload(gvt, ring_id)));
400 if (kthread_should_stop())
403 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
404 workload->ring_id, workload,
407 intel_runtime_pm_get(gvt->dev_priv);
410 * Always take i915 big lock first
412 ret = i915_mutex_lock_interruptible(&gvt->dev_priv->drm);
414 gvt_err("i915 submission is not available, retry\n");
419 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
420 workload->ring_id, workload);
423 intel_uncore_forcewake_get(gvt->dev_priv,
426 ret = dispatch_workload(workload);
428 gvt_err("fail to dispatch workload, skip\n");
432 gvt_dbg_sched("ring id %d wait workload %p\n",
433 workload->ring_id, workload);
435 workload->status = i915_wait_request(workload->req,
436 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
438 if (workload->status != 0)
439 gvt_err("fail to wait workload, skip\n");
442 gvt_dbg_sched("will complete workload %p\n, status: %d\n",
443 workload, workload->status);
445 complete_current_workload(gvt, ring_id);
448 intel_uncore_forcewake_put(gvt->dev_priv,
451 mutex_unlock(&gvt->dev_priv->drm.struct_mutex);
453 intel_runtime_pm_put(gvt->dev_priv);
458 void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
460 struct intel_gvt *gvt = vgpu->gvt;
461 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
463 if (atomic_read(&vgpu->running_workload_num)) {
464 gvt_dbg_sched("wait vgpu idle\n");
466 wait_event(scheduler->workload_complete_wq,
467 !atomic_read(&vgpu->running_workload_num));
471 void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
473 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
476 gvt_dbg_core("clean workload scheduler\n");
478 for (i = 0; i < I915_NUM_ENGINES; i++) {
479 if (scheduler->thread[i]) {
480 kthread_stop(scheduler->thread[i]);
481 scheduler->thread[i] = NULL;
486 int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
488 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
489 struct workload_thread_param *param = NULL;
493 gvt_dbg_core("init workload scheduler\n");
495 init_waitqueue_head(&scheduler->workload_complete_wq);
497 for (i = 0; i < I915_NUM_ENGINES; i++) {
498 init_waitqueue_head(&scheduler->waitq[i]);
500 param = kzalloc(sizeof(*param), GFP_KERNEL);
509 scheduler->thread[i] = kthread_run(workload_thread, param,
510 "gvt workload %d", i);
511 if (IS_ERR(scheduler->thread[i])) {
512 gvt_err("fail to create workload thread\n");
513 ret = PTR_ERR(scheduler->thread[i]);
519 intel_gvt_clean_workload_scheduler(gvt);
525 void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
527 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
529 atomic_notifier_chain_unregister(&vgpu->shadow_ctx->status_notifier,
530 &vgpu->shadow_ctx_notifier_block);
532 mutex_lock(&dev_priv->drm.struct_mutex);
534 /* a little hacky to mark as ctx closed */
535 vgpu->shadow_ctx->closed = true;
536 i915_gem_context_put(vgpu->shadow_ctx);
538 mutex_unlock(&dev_priv->drm.struct_mutex);
541 int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu)
543 atomic_set(&vgpu->running_workload_num, 0);
545 vgpu->shadow_ctx = i915_gem_context_create_gvt(
546 &vgpu->gvt->dev_priv->drm);
547 if (IS_ERR(vgpu->shadow_ctx))
548 return PTR_ERR(vgpu->shadow_ctx);
550 vgpu->shadow_ctx->engine[RCS].initialised = true;
552 vgpu->shadow_ctx_notifier_block.notifier_call =
553 shadow_context_status_change;
555 atomic_notifier_chain_register(&vgpu->shadow_ctx->status_notifier,
556 &vgpu->shadow_ctx_notifier_block);