1 /* 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 36 #include <linux/kthread.h> 37 38 #include "i915_drv.h" 39 #include "gvt.h" 40 41 #define RING_CTX_OFF(x) \ 42 offsetof(struct execlist_ring_context, x) 43 44 static void set_context_pdp_root_pointer( 45 struct execlist_ring_context *ring_context, 46 u32 pdp[8]) 47 { 48 int i; 49 50 for (i = 0; i < 8; i++) 51 ring_context->pdps[i].val = pdp[7 - i]; 52 } 53 54 static void update_shadow_pdps(struct intel_vgpu_workload *workload) 55 { 56 struct drm_i915_gem_object *ctx_obj = 57 workload->req->hw_context->state->obj; 58 struct execlist_ring_context *shadow_ring_context; 59 struct page *page; 60 61 if (WARN_ON(!workload->shadow_mm)) 62 return; 63 64 if (WARN_ON(!atomic_read(&workload->shadow_mm->pincount))) 65 return; 66 67 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN); 68 shadow_ring_context = kmap(page); 69 set_context_pdp_root_pointer(shadow_ring_context, 70 (void *)workload->shadow_mm->ppgtt_mm.shadow_pdps); 71 kunmap(page); 72 } 73 74 /* 75 * when populating shadow ctx from guest, we should not overrride oa related 76 * registers, so that they will not be overlapped by guest oa configs. Thus 77 * made it possible to capture oa data from host for both host and guests. 78 */ 79 static void sr_oa_regs(struct intel_vgpu_workload *workload, 80 u32 *reg_state, bool save) 81 { 82 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv; 83 u32 ctx_oactxctrl = dev_priv->perf.oa.ctx_oactxctrl_offset; 84 u32 ctx_flexeu0 = dev_priv->perf.oa.ctx_flexeu0_offset; 85 int i = 0; 86 u32 flex_mmio[] = { 87 i915_mmio_reg_offset(EU_PERF_CNTL0), 88 i915_mmio_reg_offset(EU_PERF_CNTL1), 89 i915_mmio_reg_offset(EU_PERF_CNTL2), 90 i915_mmio_reg_offset(EU_PERF_CNTL3), 91 i915_mmio_reg_offset(EU_PERF_CNTL4), 92 i915_mmio_reg_offset(EU_PERF_CNTL5), 93 i915_mmio_reg_offset(EU_PERF_CNTL6), 94 }; 95 96 if (workload->ring_id != RCS) 97 return; 98 99 if (save) { 100 workload->oactxctrl = reg_state[ctx_oactxctrl + 1]; 101 102 for (i = 0; i < ARRAY_SIZE(workload->flex_mmio); i++) { 103 u32 state_offset = ctx_flexeu0 + i * 2; 104 105 workload->flex_mmio[i] = reg_state[state_offset + 1]; 106 } 107 } else { 108 reg_state[ctx_oactxctrl] = 109 i915_mmio_reg_offset(GEN8_OACTXCONTROL); 110 reg_state[ctx_oactxctrl + 1] = workload->oactxctrl; 111 112 for (i = 0; i < ARRAY_SIZE(workload->flex_mmio); i++) { 113 u32 state_offset = ctx_flexeu0 + i * 2; 114 u32 mmio = flex_mmio[i]; 115 116 reg_state[state_offset] = mmio; 117 reg_state[state_offset + 1] = workload->flex_mmio[i]; 118 } 119 } 120 } 121 122 static int populate_shadow_context(struct intel_vgpu_workload *workload) 123 { 124 struct intel_vgpu *vgpu = workload->vgpu; 125 struct intel_gvt *gvt = vgpu->gvt; 126 int ring_id = workload->ring_id; 127 struct drm_i915_gem_object *ctx_obj = 128 workload->req->hw_context->state->obj; 129 struct execlist_ring_context *shadow_ring_context; 130 struct page *page; 131 void *dst; 132 unsigned long context_gpa, context_page_num; 133 int i; 134 135 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN); 136 shadow_ring_context = kmap(page); 137 138 sr_oa_regs(workload, (u32 *)shadow_ring_context, true); 139 #define COPY_REG(name) \ 140 intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \ 141 + RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4) 142 #define COPY_REG_MASKED(name) {\ 143 intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \ 144 + RING_CTX_OFF(name.val),\ 145 &shadow_ring_context->name.val, 4);\ 146 shadow_ring_context->name.val |= 0xffff << 16;\ 147 } 148 149 COPY_REG_MASKED(ctx_ctrl); 150 COPY_REG(ctx_timestamp); 151 152 if (ring_id == RCS) { 153 COPY_REG(bb_per_ctx_ptr); 154 COPY_REG(rcs_indirect_ctx); 155 COPY_REG(rcs_indirect_ctx_offset); 156 } 157 #undef COPY_REG 158 #undef COPY_REG_MASKED 159 160 intel_gvt_hypervisor_read_gpa(vgpu, 161 workload->ring_context_gpa + 162 sizeof(*shadow_ring_context), 163 (void *)shadow_ring_context + 164 sizeof(*shadow_ring_context), 165 I915_GTT_PAGE_SIZE - sizeof(*shadow_ring_context)); 166 167 sr_oa_regs(workload, (u32 *)shadow_ring_context, false); 168 kunmap(page); 169 170 if (IS_RESTORE_INHIBIT(shadow_ring_context->ctx_ctrl.val)) 171 return 0; 172 173 gvt_dbg_sched("ring id %d workload lrca %x", ring_id, 174 workload->ctx_desc.lrca); 175 176 context_page_num = gvt->dev_priv->engine[ring_id]->context_size; 177 178 context_page_num = context_page_num >> PAGE_SHIFT; 179 180 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS) 181 context_page_num = 19; 182 183 i = 2; 184 while (i < context_page_num) { 185 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm, 186 (u32)((workload->ctx_desc.lrca + i) << 187 I915_GTT_PAGE_SHIFT)); 188 if (context_gpa == INTEL_GVT_INVALID_ADDR) { 189 gvt_vgpu_err("Invalid guest context descriptor\n"); 190 return -EFAULT; 191 } 192 193 page = i915_gem_object_get_page(ctx_obj, LRC_HEADER_PAGES + i); 194 dst = kmap(page); 195 intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst, 196 I915_GTT_PAGE_SIZE); 197 kunmap(page); 198 i++; 199 } 200 return 0; 201 } 202 203 static inline bool is_gvt_request(struct i915_request *req) 204 { 205 return i915_gem_context_force_single_submission(req->gem_context); 206 } 207 208 static void save_ring_hw_state(struct intel_vgpu *vgpu, int ring_id) 209 { 210 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 211 u32 ring_base = dev_priv->engine[ring_id]->mmio_base; 212 i915_reg_t reg; 213 214 reg = RING_INSTDONE(ring_base); 215 vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg); 216 reg = RING_ACTHD(ring_base); 217 vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg); 218 reg = RING_ACTHD_UDW(ring_base); 219 vgpu_vreg(vgpu, i915_mmio_reg_offset(reg)) = I915_READ_FW(reg); 220 } 221 222 static int shadow_context_status_change(struct notifier_block *nb, 223 unsigned long action, void *data) 224 { 225 struct i915_request *req = data; 226 struct intel_gvt *gvt = container_of(nb, struct intel_gvt, 227 shadow_ctx_notifier_block[req->engine->id]); 228 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 229 enum intel_engine_id ring_id = req->engine->id; 230 struct intel_vgpu_workload *workload; 231 unsigned long flags; 232 233 if (!is_gvt_request(req)) { 234 spin_lock_irqsave(&scheduler->mmio_context_lock, flags); 235 if (action == INTEL_CONTEXT_SCHEDULE_IN && 236 scheduler->engine_owner[ring_id]) { 237 /* Switch ring from vGPU to host. */ 238 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id], 239 NULL, ring_id); 240 scheduler->engine_owner[ring_id] = NULL; 241 } 242 spin_unlock_irqrestore(&scheduler->mmio_context_lock, flags); 243 244 return NOTIFY_OK; 245 } 246 247 workload = scheduler->current_workload[ring_id]; 248 if (unlikely(!workload)) 249 return NOTIFY_OK; 250 251 switch (action) { 252 case INTEL_CONTEXT_SCHEDULE_IN: 253 spin_lock_irqsave(&scheduler->mmio_context_lock, flags); 254 if (workload->vgpu != scheduler->engine_owner[ring_id]) { 255 /* Switch ring from host to vGPU or vGPU to vGPU. */ 256 intel_gvt_switch_mmio(scheduler->engine_owner[ring_id], 257 workload->vgpu, ring_id); 258 scheduler->engine_owner[ring_id] = workload->vgpu; 259 } else 260 gvt_dbg_sched("skip ring %d mmio switch for vgpu%d\n", 261 ring_id, workload->vgpu->id); 262 spin_unlock_irqrestore(&scheduler->mmio_context_lock, flags); 263 atomic_set(&workload->shadow_ctx_active, 1); 264 break; 265 case INTEL_CONTEXT_SCHEDULE_OUT: 266 save_ring_hw_state(workload->vgpu, ring_id); 267 atomic_set(&workload->shadow_ctx_active, 0); 268 break; 269 case INTEL_CONTEXT_SCHEDULE_PREEMPTED: 270 save_ring_hw_state(workload->vgpu, ring_id); 271 break; 272 default: 273 WARN_ON(1); 274 return NOTIFY_OK; 275 } 276 wake_up(&workload->shadow_ctx_status_wq); 277 return NOTIFY_OK; 278 } 279 280 static void shadow_context_descriptor_update(struct intel_context *ce) 281 { 282 u64 desc = 0; 283 284 desc = ce->lrc_desc; 285 286 /* Update bits 0-11 of the context descriptor which includes flags 287 * like GEN8_CTX_* cached in desc_template 288 */ 289 desc &= U64_MAX << 12; 290 desc |= ce->gem_context->desc_template & ((1ULL << 12) - 1); 291 292 ce->lrc_desc = desc; 293 } 294 295 static int copy_workload_to_ring_buffer(struct intel_vgpu_workload *workload) 296 { 297 struct intel_vgpu *vgpu = workload->vgpu; 298 struct i915_request *req = workload->req; 299 void *shadow_ring_buffer_va; 300 u32 *cs; 301 302 if ((IS_KABYLAKE(req->i915) || IS_BROXTON(req->i915)) 303 && is_inhibit_context(req->hw_context)) 304 intel_vgpu_restore_inhibit_context(vgpu, req); 305 306 /* allocate shadow ring buffer */ 307 cs = intel_ring_begin(workload->req, workload->rb_len / sizeof(u32)); 308 if (IS_ERR(cs)) { 309 gvt_vgpu_err("fail to alloc size =%ld shadow ring buffer\n", 310 workload->rb_len); 311 return PTR_ERR(cs); 312 } 313 314 shadow_ring_buffer_va = workload->shadow_ring_buffer_va; 315 316 /* get shadow ring buffer va */ 317 workload->shadow_ring_buffer_va = cs; 318 319 memcpy(cs, shadow_ring_buffer_va, 320 workload->rb_len); 321 322 cs += workload->rb_len / sizeof(u32); 323 intel_ring_advance(workload->req, cs); 324 325 return 0; 326 } 327 328 static void release_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx) 329 { 330 if (!wa_ctx->indirect_ctx.obj) 331 return; 332 333 i915_gem_object_unpin_map(wa_ctx->indirect_ctx.obj); 334 i915_gem_object_put(wa_ctx->indirect_ctx.obj); 335 } 336 337 static int set_context_ppgtt_from_shadow(struct intel_vgpu_workload *workload, 338 struct i915_gem_context *ctx) 339 { 340 struct intel_vgpu_mm *mm = workload->shadow_mm; 341 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt; 342 int i = 0; 343 344 if (mm->type != INTEL_GVT_MM_PPGTT || !mm->ppgtt_mm.shadowed) 345 return -1; 346 347 if (mm->ppgtt_mm.root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY) { 348 px_dma(&ppgtt->pml4) = mm->ppgtt_mm.shadow_pdps[0]; 349 } else { 350 for (i = 0; i < GVT_RING_CTX_NR_PDPS; i++) { 351 px_dma(ppgtt->pdp.page_directory[i]) = 352 mm->ppgtt_mm.shadow_pdps[i]; 353 } 354 } 355 356 return 0; 357 } 358 359 static int 360 intel_gvt_workload_req_alloc(struct intel_vgpu_workload *workload) 361 { 362 struct intel_vgpu *vgpu = workload->vgpu; 363 struct intel_vgpu_submission *s = &vgpu->submission; 364 struct i915_gem_context *shadow_ctx = s->shadow_ctx; 365 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 366 struct intel_engine_cs *engine = dev_priv->engine[workload->ring_id]; 367 struct i915_request *rq; 368 int ret = 0; 369 370 lockdep_assert_held(&dev_priv->drm.struct_mutex); 371 372 if (workload->req) 373 goto out; 374 375 rq = i915_request_alloc(engine, shadow_ctx); 376 if (IS_ERR(rq)) { 377 gvt_vgpu_err("fail to allocate gem request\n"); 378 ret = PTR_ERR(rq); 379 goto out; 380 } 381 workload->req = i915_request_get(rq); 382 out: 383 return ret; 384 } 385 386 /** 387 * intel_gvt_scan_and_shadow_workload - audit the workload by scanning and 388 * shadow it as well, include ringbuffer,wa_ctx and ctx. 389 * @workload: an abstract entity for each execlist submission. 390 * 391 * This function is called before the workload submitting to i915, to make 392 * sure the content of the workload is valid. 393 */ 394 int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload) 395 { 396 struct intel_vgpu *vgpu = workload->vgpu; 397 struct intel_vgpu_submission *s = &vgpu->submission; 398 struct i915_gem_context *shadow_ctx = s->shadow_ctx; 399 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 400 struct intel_engine_cs *engine = dev_priv->engine[workload->ring_id]; 401 struct intel_context *ce; 402 int ret; 403 404 lockdep_assert_held(&dev_priv->drm.struct_mutex); 405 406 if (workload->shadow) 407 return 0; 408 409 ret = set_context_ppgtt_from_shadow(workload, shadow_ctx); 410 if (ret < 0) { 411 gvt_vgpu_err("workload shadow ppgtt isn't ready\n"); 412 return ret; 413 } 414 415 /* pin shadow context by gvt even the shadow context will be pinned 416 * when i915 alloc request. That is because gvt will update the guest 417 * context from shadow context when workload is completed, and at that 418 * moment, i915 may already unpined the shadow context to make the 419 * shadow_ctx pages invalid. So gvt need to pin itself. After update 420 * the guest context, gvt can unpin the shadow_ctx safely. 421 */ 422 ce = intel_context_pin(shadow_ctx, engine); 423 if (IS_ERR(ce)) { 424 gvt_vgpu_err("fail to pin shadow context\n"); 425 return PTR_ERR(ce); 426 } 427 428 shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT); 429 shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode << 430 GEN8_CTX_ADDRESSING_MODE_SHIFT; 431 432 if (!test_and_set_bit(workload->ring_id, s->shadow_ctx_desc_updated)) 433 shadow_context_descriptor_update(ce); 434 435 ret = intel_gvt_scan_and_shadow_ringbuffer(workload); 436 if (ret) 437 goto err_unpin; 438 439 if ((workload->ring_id == RCS) && 440 (workload->wa_ctx.indirect_ctx.size != 0)) { 441 ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx); 442 if (ret) 443 goto err_shadow; 444 } 445 446 workload->shadow = true; 447 return 0; 448 err_shadow: 449 release_shadow_wa_ctx(&workload->wa_ctx); 450 err_unpin: 451 intel_context_unpin(ce); 452 return ret; 453 } 454 455 static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload); 456 457 static int prepare_shadow_batch_buffer(struct intel_vgpu_workload *workload) 458 { 459 struct intel_gvt *gvt = workload->vgpu->gvt; 460 const int gmadr_bytes = gvt->device_info.gmadr_bytes_in_cmd; 461 struct intel_vgpu_shadow_bb *bb; 462 int ret; 463 464 list_for_each_entry(bb, &workload->shadow_bb, list) { 465 /* For privilge batch buffer and not wa_ctx, the bb_start_cmd_va 466 * is only updated into ring_scan_buffer, not real ring address 467 * allocated in later copy_workload_to_ring_buffer. pls be noted 468 * shadow_ring_buffer_va is now pointed to real ring buffer va 469 * in copy_workload_to_ring_buffer. 470 */ 471 472 if (bb->bb_offset) 473 bb->bb_start_cmd_va = workload->shadow_ring_buffer_va 474 + bb->bb_offset; 475 476 if (bb->ppgtt) { 477 /* for non-priv bb, scan&shadow is only for 478 * debugging purpose, so the content of shadow bb 479 * is the same as original bb. Therefore, 480 * here, rather than switch to shadow bb's gma 481 * address, we directly use original batch buffer's 482 * gma address, and send original bb to hardware 483 * directly 484 */ 485 if (bb->clflush & CLFLUSH_AFTER) { 486 drm_clflush_virt_range(bb->va, 487 bb->obj->base.size); 488 bb->clflush &= ~CLFLUSH_AFTER; 489 } 490 i915_gem_obj_finish_shmem_access(bb->obj); 491 bb->accessing = false; 492 493 } else { 494 bb->vma = i915_gem_object_ggtt_pin(bb->obj, 495 NULL, 0, 0, 0); 496 if (IS_ERR(bb->vma)) { 497 ret = PTR_ERR(bb->vma); 498 goto err; 499 } 500 501 /* relocate shadow batch buffer */ 502 bb->bb_start_cmd_va[1] = i915_ggtt_offset(bb->vma); 503 if (gmadr_bytes == 8) 504 bb->bb_start_cmd_va[2] = 0; 505 506 /* No one is going to touch shadow bb from now on. */ 507 if (bb->clflush & CLFLUSH_AFTER) { 508 drm_clflush_virt_range(bb->va, 509 bb->obj->base.size); 510 bb->clflush &= ~CLFLUSH_AFTER; 511 } 512 513 ret = i915_gem_object_set_to_gtt_domain(bb->obj, 514 false); 515 if (ret) 516 goto err; 517 518 i915_gem_obj_finish_shmem_access(bb->obj); 519 bb->accessing = false; 520 521 ret = i915_vma_move_to_active(bb->vma, 522 workload->req, 523 0); 524 if (ret) 525 goto err; 526 } 527 } 528 return 0; 529 err: 530 release_shadow_batch_buffer(workload); 531 return ret; 532 } 533 534 static void update_wa_ctx_2_shadow_ctx(struct intel_shadow_wa_ctx *wa_ctx) 535 { 536 struct intel_vgpu_workload *workload = 537 container_of(wa_ctx, struct intel_vgpu_workload, wa_ctx); 538 struct i915_request *rq = workload->req; 539 struct execlist_ring_context *shadow_ring_context = 540 (struct execlist_ring_context *)rq->hw_context->lrc_reg_state; 541 542 shadow_ring_context->bb_per_ctx_ptr.val = 543 (shadow_ring_context->bb_per_ctx_ptr.val & 544 (~PER_CTX_ADDR_MASK)) | wa_ctx->per_ctx.shadow_gma; 545 shadow_ring_context->rcs_indirect_ctx.val = 546 (shadow_ring_context->rcs_indirect_ctx.val & 547 (~INDIRECT_CTX_ADDR_MASK)) | wa_ctx->indirect_ctx.shadow_gma; 548 } 549 550 static int prepare_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx) 551 { 552 struct i915_vma *vma; 553 unsigned char *per_ctx_va = 554 (unsigned char *)wa_ctx->indirect_ctx.shadow_va + 555 wa_ctx->indirect_ctx.size; 556 557 if (wa_ctx->indirect_ctx.size == 0) 558 return 0; 559 560 vma = i915_gem_object_ggtt_pin(wa_ctx->indirect_ctx.obj, NULL, 561 0, CACHELINE_BYTES, 0); 562 if (IS_ERR(vma)) 563 return PTR_ERR(vma); 564 565 /* FIXME: we are not tracking our pinned VMA leaving it 566 * up to the core to fix up the stray pin_count upon 567 * free. 568 */ 569 570 wa_ctx->indirect_ctx.shadow_gma = i915_ggtt_offset(vma); 571 572 wa_ctx->per_ctx.shadow_gma = *((unsigned int *)per_ctx_va + 1); 573 memset(per_ctx_va, 0, CACHELINE_BYTES); 574 575 update_wa_ctx_2_shadow_ctx(wa_ctx); 576 return 0; 577 } 578 579 static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload) 580 { 581 struct intel_vgpu *vgpu = workload->vgpu; 582 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 583 struct intel_vgpu_shadow_bb *bb, *pos; 584 585 if (list_empty(&workload->shadow_bb)) 586 return; 587 588 bb = list_first_entry(&workload->shadow_bb, 589 struct intel_vgpu_shadow_bb, list); 590 591 mutex_lock(&dev_priv->drm.struct_mutex); 592 593 list_for_each_entry_safe(bb, pos, &workload->shadow_bb, list) { 594 if (bb->obj) { 595 if (bb->accessing) 596 i915_gem_obj_finish_shmem_access(bb->obj); 597 598 if (bb->va && !IS_ERR(bb->va)) 599 i915_gem_object_unpin_map(bb->obj); 600 601 if (bb->vma && !IS_ERR(bb->vma)) { 602 i915_vma_unpin(bb->vma); 603 i915_vma_close(bb->vma); 604 } 605 __i915_gem_object_release_unless_active(bb->obj); 606 } 607 list_del(&bb->list); 608 kfree(bb); 609 } 610 611 mutex_unlock(&dev_priv->drm.struct_mutex); 612 } 613 614 static int prepare_workload(struct intel_vgpu_workload *workload) 615 { 616 struct intel_vgpu *vgpu = workload->vgpu; 617 int ret = 0; 618 619 ret = intel_vgpu_pin_mm(workload->shadow_mm); 620 if (ret) { 621 gvt_vgpu_err("fail to vgpu pin mm\n"); 622 return ret; 623 } 624 625 update_shadow_pdps(workload); 626 627 ret = intel_vgpu_sync_oos_pages(workload->vgpu); 628 if (ret) { 629 gvt_vgpu_err("fail to vgpu sync oos pages\n"); 630 goto err_unpin_mm; 631 } 632 633 ret = intel_vgpu_flush_post_shadow(workload->vgpu); 634 if (ret) { 635 gvt_vgpu_err("fail to flush post shadow\n"); 636 goto err_unpin_mm; 637 } 638 639 ret = copy_workload_to_ring_buffer(workload); 640 if (ret) { 641 gvt_vgpu_err("fail to generate request\n"); 642 goto err_unpin_mm; 643 } 644 645 ret = prepare_shadow_batch_buffer(workload); 646 if (ret) { 647 gvt_vgpu_err("fail to prepare_shadow_batch_buffer\n"); 648 goto err_unpin_mm; 649 } 650 651 ret = prepare_shadow_wa_ctx(&workload->wa_ctx); 652 if (ret) { 653 gvt_vgpu_err("fail to prepare_shadow_wa_ctx\n"); 654 goto err_shadow_batch; 655 } 656 657 if (workload->prepare) { 658 ret = workload->prepare(workload); 659 if (ret) 660 goto err_shadow_wa_ctx; 661 } 662 663 return 0; 664 err_shadow_wa_ctx: 665 release_shadow_wa_ctx(&workload->wa_ctx); 666 err_shadow_batch: 667 release_shadow_batch_buffer(workload); 668 err_unpin_mm: 669 intel_vgpu_unpin_mm(workload->shadow_mm); 670 return ret; 671 } 672 673 static int dispatch_workload(struct intel_vgpu_workload *workload) 674 { 675 struct intel_vgpu *vgpu = workload->vgpu; 676 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 677 int ring_id = workload->ring_id; 678 int ret; 679 680 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n", 681 ring_id, workload); 682 683 mutex_lock(&vgpu->vgpu_lock); 684 mutex_lock(&dev_priv->drm.struct_mutex); 685 686 ret = intel_gvt_workload_req_alloc(workload); 687 if (ret) 688 goto err_req; 689 690 ret = intel_gvt_scan_and_shadow_workload(workload); 691 if (ret) 692 goto out; 693 694 ret = populate_shadow_context(workload); 695 if (ret) { 696 release_shadow_wa_ctx(&workload->wa_ctx); 697 goto out; 698 } 699 700 ret = prepare_workload(workload); 701 out: 702 if (!IS_ERR_OR_NULL(workload->req)) { 703 gvt_dbg_sched("ring id %d submit workload to i915 %p\n", 704 ring_id, workload->req); 705 i915_request_add(workload->req); 706 workload->dispatched = true; 707 } 708 err_req: 709 if (ret) 710 workload->status = ret; 711 mutex_unlock(&dev_priv->drm.struct_mutex); 712 mutex_unlock(&vgpu->vgpu_lock); 713 return ret; 714 } 715 716 static struct intel_vgpu_workload *pick_next_workload( 717 struct intel_gvt *gvt, int ring_id) 718 { 719 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 720 struct intel_vgpu_workload *workload = NULL; 721 722 mutex_lock(&gvt->sched_lock); 723 724 /* 725 * no current vgpu / will be scheduled out / no workload 726 * bail out 727 */ 728 if (!scheduler->current_vgpu) { 729 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id); 730 goto out; 731 } 732 733 if (scheduler->need_reschedule) { 734 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id); 735 goto out; 736 } 737 738 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) 739 goto out; 740 741 /* 742 * still have current workload, maybe the workload disptacher 743 * fail to submit it for some reason, resubmit it. 744 */ 745 if (scheduler->current_workload[ring_id]) { 746 workload = scheduler->current_workload[ring_id]; 747 gvt_dbg_sched("ring id %d still have current workload %p\n", 748 ring_id, workload); 749 goto out; 750 } 751 752 /* 753 * pick a workload as current workload 754 * once current workload is set, schedule policy routines 755 * will wait the current workload is finished when trying to 756 * schedule out a vgpu. 757 */ 758 scheduler->current_workload[ring_id] = container_of( 759 workload_q_head(scheduler->current_vgpu, ring_id)->next, 760 struct intel_vgpu_workload, list); 761 762 workload = scheduler->current_workload[ring_id]; 763 764 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload); 765 766 atomic_inc(&workload->vgpu->submission.running_workload_num); 767 out: 768 mutex_unlock(&gvt->sched_lock); 769 return workload; 770 } 771 772 static void update_guest_context(struct intel_vgpu_workload *workload) 773 { 774 struct i915_request *rq = workload->req; 775 struct intel_vgpu *vgpu = workload->vgpu; 776 struct intel_gvt *gvt = vgpu->gvt; 777 struct drm_i915_gem_object *ctx_obj = rq->hw_context->state->obj; 778 struct execlist_ring_context *shadow_ring_context; 779 struct page *page; 780 void *src; 781 unsigned long context_gpa, context_page_num; 782 int i; 783 784 gvt_dbg_sched("ring id %d workload lrca %x\n", rq->engine->id, 785 workload->ctx_desc.lrca); 786 787 context_page_num = rq->engine->context_size; 788 context_page_num = context_page_num >> PAGE_SHIFT; 789 790 if (IS_BROADWELL(gvt->dev_priv) && rq->engine->id == RCS) 791 context_page_num = 19; 792 793 i = 2; 794 795 while (i < context_page_num) { 796 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm, 797 (u32)((workload->ctx_desc.lrca + i) << 798 I915_GTT_PAGE_SHIFT)); 799 if (context_gpa == INTEL_GVT_INVALID_ADDR) { 800 gvt_vgpu_err("invalid guest context descriptor\n"); 801 return; 802 } 803 804 page = i915_gem_object_get_page(ctx_obj, LRC_HEADER_PAGES + i); 805 src = kmap(page); 806 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src, 807 I915_GTT_PAGE_SIZE); 808 kunmap(page); 809 i++; 810 } 811 812 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + 813 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4); 814 815 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN); 816 shadow_ring_context = kmap(page); 817 818 #define COPY_REG(name) \ 819 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \ 820 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4) 821 822 COPY_REG(ctx_ctrl); 823 COPY_REG(ctx_timestamp); 824 825 #undef COPY_REG 826 827 intel_gvt_hypervisor_write_gpa(vgpu, 828 workload->ring_context_gpa + 829 sizeof(*shadow_ring_context), 830 (void *)shadow_ring_context + 831 sizeof(*shadow_ring_context), 832 I915_GTT_PAGE_SIZE - sizeof(*shadow_ring_context)); 833 834 kunmap(page); 835 } 836 837 void intel_vgpu_clean_workloads(struct intel_vgpu *vgpu, 838 unsigned long engine_mask) 839 { 840 struct intel_vgpu_submission *s = &vgpu->submission; 841 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 842 struct intel_engine_cs *engine; 843 struct intel_vgpu_workload *pos, *n; 844 unsigned int tmp; 845 846 /* free the unsubmited workloads in the queues. */ 847 for_each_engine_masked(engine, dev_priv, engine_mask, tmp) { 848 list_for_each_entry_safe(pos, n, 849 &s->workload_q_head[engine->id], list) { 850 list_del_init(&pos->list); 851 intel_vgpu_destroy_workload(pos); 852 } 853 clear_bit(engine->id, s->shadow_ctx_desc_updated); 854 } 855 } 856 857 static void complete_current_workload(struct intel_gvt *gvt, int ring_id) 858 { 859 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 860 struct intel_vgpu_workload *workload = 861 scheduler->current_workload[ring_id]; 862 struct intel_vgpu *vgpu = workload->vgpu; 863 struct intel_vgpu_submission *s = &vgpu->submission; 864 struct i915_request *rq = workload->req; 865 int event; 866 867 mutex_lock(&vgpu->vgpu_lock); 868 mutex_lock(&gvt->sched_lock); 869 870 /* For the workload w/ request, needs to wait for the context 871 * switch to make sure request is completed. 872 * For the workload w/o request, directly complete the workload. 873 */ 874 if (rq) { 875 wait_event(workload->shadow_ctx_status_wq, 876 !atomic_read(&workload->shadow_ctx_active)); 877 878 /* If this request caused GPU hang, req->fence.error will 879 * be set to -EIO. Use -EIO to set workload status so 880 * that when this request caused GPU hang, didn't trigger 881 * context switch interrupt to guest. 882 */ 883 if (likely(workload->status == -EINPROGRESS)) { 884 if (workload->req->fence.error == -EIO) 885 workload->status = -EIO; 886 else 887 workload->status = 0; 888 } 889 890 if (!workload->status && !(vgpu->resetting_eng & 891 ENGINE_MASK(ring_id))) { 892 update_guest_context(workload); 893 894 for_each_set_bit(event, workload->pending_events, 895 INTEL_GVT_EVENT_MAX) 896 intel_vgpu_trigger_virtual_event(vgpu, event); 897 } 898 899 /* unpin shadow ctx as the shadow_ctx update is done */ 900 mutex_lock(&rq->i915->drm.struct_mutex); 901 intel_context_unpin(rq->hw_context); 902 mutex_unlock(&rq->i915->drm.struct_mutex); 903 904 i915_request_put(fetch_and_zero(&workload->req)); 905 } 906 907 gvt_dbg_sched("ring id %d complete workload %p status %d\n", 908 ring_id, workload, workload->status); 909 910 scheduler->current_workload[ring_id] = NULL; 911 912 list_del_init(&workload->list); 913 914 if (!workload->status) { 915 release_shadow_batch_buffer(workload); 916 release_shadow_wa_ctx(&workload->wa_ctx); 917 } 918 919 if (workload->status || (vgpu->resetting_eng & ENGINE_MASK(ring_id))) { 920 /* if workload->status is not successful means HW GPU 921 * has occurred GPU hang or something wrong with i915/GVT, 922 * and GVT won't inject context switch interrupt to guest. 923 * So this error is a vGPU hang actually to the guest. 924 * According to this we should emunlate a vGPU hang. If 925 * there are pending workloads which are already submitted 926 * from guest, we should clean them up like HW GPU does. 927 * 928 * if it is in middle of engine resetting, the pending 929 * workloads won't be submitted to HW GPU and will be 930 * cleaned up during the resetting process later, so doing 931 * the workload clean up here doesn't have any impact. 932 **/ 933 intel_vgpu_clean_workloads(vgpu, ENGINE_MASK(ring_id)); 934 } 935 936 workload->complete(workload); 937 938 atomic_dec(&s->running_workload_num); 939 wake_up(&scheduler->workload_complete_wq); 940 941 if (gvt->scheduler.need_reschedule) 942 intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EVENT_SCHED); 943 944 mutex_unlock(&gvt->sched_lock); 945 mutex_unlock(&vgpu->vgpu_lock); 946 } 947 948 struct workload_thread_param { 949 struct intel_gvt *gvt; 950 int ring_id; 951 }; 952 953 static int workload_thread(void *priv) 954 { 955 struct workload_thread_param *p = (struct workload_thread_param *)priv; 956 struct intel_gvt *gvt = p->gvt; 957 int ring_id = p->ring_id; 958 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 959 struct intel_vgpu_workload *workload = NULL; 960 struct intel_vgpu *vgpu = NULL; 961 int ret; 962 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv) 963 || IS_KABYLAKE(gvt->dev_priv) 964 || IS_BROXTON(gvt->dev_priv); 965 DEFINE_WAIT_FUNC(wait, woken_wake_function); 966 967 kfree(p); 968 969 gvt_dbg_core("workload thread for ring %d started\n", ring_id); 970 971 while (!kthread_should_stop()) { 972 add_wait_queue(&scheduler->waitq[ring_id], &wait); 973 do { 974 workload = pick_next_workload(gvt, ring_id); 975 if (workload) 976 break; 977 wait_woken(&wait, TASK_INTERRUPTIBLE, 978 MAX_SCHEDULE_TIMEOUT); 979 } while (!kthread_should_stop()); 980 remove_wait_queue(&scheduler->waitq[ring_id], &wait); 981 982 if (!workload) 983 break; 984 985 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n", 986 workload->ring_id, workload, 987 workload->vgpu->id); 988 989 intel_runtime_pm_get(gvt->dev_priv); 990 991 gvt_dbg_sched("ring id %d will dispatch workload %p\n", 992 workload->ring_id, workload); 993 994 if (need_force_wake) 995 intel_uncore_forcewake_get(gvt->dev_priv, 996 FORCEWAKE_ALL); 997 998 ret = dispatch_workload(workload); 999 1000 if (ret) { 1001 vgpu = workload->vgpu; 1002 gvt_vgpu_err("fail to dispatch workload, skip\n"); 1003 goto complete; 1004 } 1005 1006 gvt_dbg_sched("ring id %d wait workload %p\n", 1007 workload->ring_id, workload); 1008 i915_request_wait(workload->req, 0, MAX_SCHEDULE_TIMEOUT); 1009 1010 complete: 1011 gvt_dbg_sched("will complete workload %p, status: %d\n", 1012 workload, workload->status); 1013 1014 complete_current_workload(gvt, ring_id); 1015 1016 if (need_force_wake) 1017 intel_uncore_forcewake_put(gvt->dev_priv, 1018 FORCEWAKE_ALL); 1019 1020 intel_runtime_pm_put(gvt->dev_priv); 1021 if (ret && (vgpu_is_vm_unhealthy(ret))) 1022 enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR); 1023 } 1024 return 0; 1025 } 1026 1027 void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu) 1028 { 1029 struct intel_vgpu_submission *s = &vgpu->submission; 1030 struct intel_gvt *gvt = vgpu->gvt; 1031 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 1032 1033 if (atomic_read(&s->running_workload_num)) { 1034 gvt_dbg_sched("wait vgpu idle\n"); 1035 1036 wait_event(scheduler->workload_complete_wq, 1037 !atomic_read(&s->running_workload_num)); 1038 } 1039 } 1040 1041 void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt) 1042 { 1043 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 1044 struct intel_engine_cs *engine; 1045 enum intel_engine_id i; 1046 1047 gvt_dbg_core("clean workload scheduler\n"); 1048 1049 for_each_engine(engine, gvt->dev_priv, i) { 1050 atomic_notifier_chain_unregister( 1051 &engine->context_status_notifier, 1052 &gvt->shadow_ctx_notifier_block[i]); 1053 kthread_stop(scheduler->thread[i]); 1054 } 1055 } 1056 1057 int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt) 1058 { 1059 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 1060 struct workload_thread_param *param = NULL; 1061 struct intel_engine_cs *engine; 1062 enum intel_engine_id i; 1063 int ret; 1064 1065 gvt_dbg_core("init workload scheduler\n"); 1066 1067 init_waitqueue_head(&scheduler->workload_complete_wq); 1068 1069 for_each_engine(engine, gvt->dev_priv, i) { 1070 init_waitqueue_head(&scheduler->waitq[i]); 1071 1072 param = kzalloc(sizeof(*param), GFP_KERNEL); 1073 if (!param) { 1074 ret = -ENOMEM; 1075 goto err; 1076 } 1077 1078 param->gvt = gvt; 1079 param->ring_id = i; 1080 1081 scheduler->thread[i] = kthread_run(workload_thread, param, 1082 "gvt workload %d", i); 1083 if (IS_ERR(scheduler->thread[i])) { 1084 gvt_err("fail to create workload thread\n"); 1085 ret = PTR_ERR(scheduler->thread[i]); 1086 goto err; 1087 } 1088 1089 gvt->shadow_ctx_notifier_block[i].notifier_call = 1090 shadow_context_status_change; 1091 atomic_notifier_chain_register(&engine->context_status_notifier, 1092 &gvt->shadow_ctx_notifier_block[i]); 1093 } 1094 return 0; 1095 err: 1096 intel_gvt_clean_workload_scheduler(gvt); 1097 kfree(param); 1098 param = NULL; 1099 return ret; 1100 } 1101 1102 static void 1103 i915_context_ppgtt_root_restore(struct intel_vgpu_submission *s) 1104 { 1105 struct i915_hw_ppgtt *i915_ppgtt = s->shadow_ctx->ppgtt; 1106 int i; 1107 1108 if (i915_vm_is_48bit(&i915_ppgtt->vm)) 1109 px_dma(&i915_ppgtt->pml4) = s->i915_context_pml4; 1110 else { 1111 for (i = 0; i < GEN8_3LVL_PDPES; i++) 1112 px_dma(i915_ppgtt->pdp.page_directory[i]) = 1113 s->i915_context_pdps[i]; 1114 } 1115 } 1116 1117 /** 1118 * intel_vgpu_clean_submission - free submission-related resource for vGPU 1119 * @vgpu: a vGPU 1120 * 1121 * This function is called when a vGPU is being destroyed. 1122 * 1123 */ 1124 void intel_vgpu_clean_submission(struct intel_vgpu *vgpu) 1125 { 1126 struct intel_vgpu_submission *s = &vgpu->submission; 1127 1128 intel_vgpu_select_submission_ops(vgpu, ALL_ENGINES, 0); 1129 i915_context_ppgtt_root_restore(s); 1130 i915_gem_context_put(s->shadow_ctx); 1131 kmem_cache_destroy(s->workloads); 1132 } 1133 1134 1135 /** 1136 * intel_vgpu_reset_submission - reset submission-related resource for vGPU 1137 * @vgpu: a vGPU 1138 * @engine_mask: engines expected to be reset 1139 * 1140 * This function is called when a vGPU is being destroyed. 1141 * 1142 */ 1143 void intel_vgpu_reset_submission(struct intel_vgpu *vgpu, 1144 unsigned long engine_mask) 1145 { 1146 struct intel_vgpu_submission *s = &vgpu->submission; 1147 1148 if (!s->active) 1149 return; 1150 1151 intel_vgpu_clean_workloads(vgpu, engine_mask); 1152 s->ops->reset(vgpu, engine_mask); 1153 } 1154 1155 static void 1156 i915_context_ppgtt_root_save(struct intel_vgpu_submission *s) 1157 { 1158 struct i915_hw_ppgtt *i915_ppgtt = s->shadow_ctx->ppgtt; 1159 int i; 1160 1161 if (i915_vm_is_48bit(&i915_ppgtt->vm)) 1162 s->i915_context_pml4 = px_dma(&i915_ppgtt->pml4); 1163 else { 1164 for (i = 0; i < GEN8_3LVL_PDPES; i++) 1165 s->i915_context_pdps[i] = 1166 px_dma(i915_ppgtt->pdp.page_directory[i]); 1167 } 1168 } 1169 1170 /** 1171 * intel_vgpu_setup_submission - setup submission-related resource for vGPU 1172 * @vgpu: a vGPU 1173 * 1174 * This function is called when a vGPU is being created. 1175 * 1176 * Returns: 1177 * Zero on success, negative error code if failed. 1178 * 1179 */ 1180 int intel_vgpu_setup_submission(struct intel_vgpu *vgpu) 1181 { 1182 struct intel_vgpu_submission *s = &vgpu->submission; 1183 enum intel_engine_id i; 1184 struct intel_engine_cs *engine; 1185 int ret; 1186 1187 s->shadow_ctx = i915_gem_context_create_gvt( 1188 &vgpu->gvt->dev_priv->drm); 1189 if (IS_ERR(s->shadow_ctx)) 1190 return PTR_ERR(s->shadow_ctx); 1191 1192 i915_context_ppgtt_root_save(s); 1193 1194 bitmap_zero(s->shadow_ctx_desc_updated, I915_NUM_ENGINES); 1195 1196 s->workloads = kmem_cache_create_usercopy("gvt-g_vgpu_workload", 1197 sizeof(struct intel_vgpu_workload), 0, 1198 SLAB_HWCACHE_ALIGN, 1199 offsetof(struct intel_vgpu_workload, rb_tail), 1200 sizeof_field(struct intel_vgpu_workload, rb_tail), 1201 NULL); 1202 1203 if (!s->workloads) { 1204 ret = -ENOMEM; 1205 goto out_shadow_ctx; 1206 } 1207 1208 for_each_engine(engine, vgpu->gvt->dev_priv, i) 1209 INIT_LIST_HEAD(&s->workload_q_head[i]); 1210 1211 atomic_set(&s->running_workload_num, 0); 1212 bitmap_zero(s->tlb_handle_pending, I915_NUM_ENGINES); 1213 1214 return 0; 1215 1216 out_shadow_ctx: 1217 i915_gem_context_put(s->shadow_ctx); 1218 return ret; 1219 } 1220 1221 /** 1222 * intel_vgpu_select_submission_ops - select virtual submission interface 1223 * @vgpu: a vGPU 1224 * @engine_mask: either ALL_ENGINES or target engine mask 1225 * @interface: expected vGPU virtual submission interface 1226 * 1227 * This function is called when guest configures submission interface. 1228 * 1229 * Returns: 1230 * Zero on success, negative error code if failed. 1231 * 1232 */ 1233 int intel_vgpu_select_submission_ops(struct intel_vgpu *vgpu, 1234 unsigned long engine_mask, 1235 unsigned int interface) 1236 { 1237 struct intel_vgpu_submission *s = &vgpu->submission; 1238 const struct intel_vgpu_submission_ops *ops[] = { 1239 [INTEL_VGPU_EXECLIST_SUBMISSION] = 1240 &intel_vgpu_execlist_submission_ops, 1241 }; 1242 int ret; 1243 1244 if (WARN_ON(interface >= ARRAY_SIZE(ops))) 1245 return -EINVAL; 1246 1247 if (WARN_ON(interface == 0 && engine_mask != ALL_ENGINES)) 1248 return -EINVAL; 1249 1250 if (s->active) 1251 s->ops->clean(vgpu, engine_mask); 1252 1253 if (interface == 0) { 1254 s->ops = NULL; 1255 s->virtual_submission_interface = 0; 1256 s->active = false; 1257 gvt_dbg_core("vgpu%d: remove submission ops\n", vgpu->id); 1258 return 0; 1259 } 1260 1261 ret = ops[interface]->init(vgpu, engine_mask); 1262 if (ret) 1263 return ret; 1264 1265 s->ops = ops[interface]; 1266 s->virtual_submission_interface = interface; 1267 s->active = true; 1268 1269 gvt_dbg_core("vgpu%d: activate ops [ %s ]\n", 1270 vgpu->id, s->ops->name); 1271 1272 return 0; 1273 } 1274 1275 /** 1276 * intel_vgpu_destroy_workload - destroy a vGPU workload 1277 * @workload: workload to destroy 1278 * 1279 * This function is called when destroy a vGPU workload. 1280 * 1281 */ 1282 void intel_vgpu_destroy_workload(struct intel_vgpu_workload *workload) 1283 { 1284 struct intel_vgpu_submission *s = &workload->vgpu->submission; 1285 1286 if (workload->shadow_mm) 1287 intel_vgpu_mm_put(workload->shadow_mm); 1288 1289 kmem_cache_free(s->workloads, workload); 1290 } 1291 1292 static struct intel_vgpu_workload * 1293 alloc_workload(struct intel_vgpu *vgpu) 1294 { 1295 struct intel_vgpu_submission *s = &vgpu->submission; 1296 struct intel_vgpu_workload *workload; 1297 1298 workload = kmem_cache_zalloc(s->workloads, GFP_KERNEL); 1299 if (!workload) 1300 return ERR_PTR(-ENOMEM); 1301 1302 INIT_LIST_HEAD(&workload->list); 1303 INIT_LIST_HEAD(&workload->shadow_bb); 1304 1305 init_waitqueue_head(&workload->shadow_ctx_status_wq); 1306 atomic_set(&workload->shadow_ctx_active, 0); 1307 1308 workload->status = -EINPROGRESS; 1309 workload->vgpu = vgpu; 1310 1311 return workload; 1312 } 1313 1314 #define RING_CTX_OFF(x) \ 1315 offsetof(struct execlist_ring_context, x) 1316 1317 static void read_guest_pdps(struct intel_vgpu *vgpu, 1318 u64 ring_context_gpa, u32 pdp[8]) 1319 { 1320 u64 gpa; 1321 int i; 1322 1323 gpa = ring_context_gpa + RING_CTX_OFF(pdps[0].val); 1324 1325 for (i = 0; i < 8; i++) 1326 intel_gvt_hypervisor_read_gpa(vgpu, 1327 gpa + i * 8, &pdp[7 - i], 4); 1328 } 1329 1330 static int prepare_mm(struct intel_vgpu_workload *workload) 1331 { 1332 struct execlist_ctx_descriptor_format *desc = &workload->ctx_desc; 1333 struct intel_vgpu_mm *mm; 1334 struct intel_vgpu *vgpu = workload->vgpu; 1335 intel_gvt_gtt_type_t root_entry_type; 1336 u64 pdps[GVT_RING_CTX_NR_PDPS]; 1337 1338 switch (desc->addressing_mode) { 1339 case 1: /* legacy 32-bit */ 1340 root_entry_type = GTT_TYPE_PPGTT_ROOT_L3_ENTRY; 1341 break; 1342 case 3: /* legacy 64-bit */ 1343 root_entry_type = GTT_TYPE_PPGTT_ROOT_L4_ENTRY; 1344 break; 1345 default: 1346 gvt_vgpu_err("Advanced Context mode(SVM) is not supported!\n"); 1347 return -EINVAL; 1348 } 1349 1350 read_guest_pdps(workload->vgpu, workload->ring_context_gpa, (void *)pdps); 1351 1352 mm = intel_vgpu_get_ppgtt_mm(workload->vgpu, root_entry_type, pdps); 1353 if (IS_ERR(mm)) 1354 return PTR_ERR(mm); 1355 1356 workload->shadow_mm = mm; 1357 return 0; 1358 } 1359 1360 #define same_context(a, b) (((a)->context_id == (b)->context_id) && \ 1361 ((a)->lrca == (b)->lrca)) 1362 1363 #define get_last_workload(q) \ 1364 (list_empty(q) ? NULL : container_of(q->prev, \ 1365 struct intel_vgpu_workload, list)) 1366 /** 1367 * intel_vgpu_create_workload - create a vGPU workload 1368 * @vgpu: a vGPU 1369 * @ring_id: ring index 1370 * @desc: a guest context descriptor 1371 * 1372 * This function is called when creating a vGPU workload. 1373 * 1374 * Returns: 1375 * struct intel_vgpu_workload * on success, negative error code in 1376 * pointer if failed. 1377 * 1378 */ 1379 struct intel_vgpu_workload * 1380 intel_vgpu_create_workload(struct intel_vgpu *vgpu, int ring_id, 1381 struct execlist_ctx_descriptor_format *desc) 1382 { 1383 struct intel_vgpu_submission *s = &vgpu->submission; 1384 struct list_head *q = workload_q_head(vgpu, ring_id); 1385 struct intel_vgpu_workload *last_workload = get_last_workload(q); 1386 struct intel_vgpu_workload *workload = NULL; 1387 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 1388 u64 ring_context_gpa; 1389 u32 head, tail, start, ctl, ctx_ctl, per_ctx, indirect_ctx; 1390 int ret; 1391 1392 ring_context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm, 1393 (u32)((desc->lrca + 1) << I915_GTT_PAGE_SHIFT)); 1394 if (ring_context_gpa == INTEL_GVT_INVALID_ADDR) { 1395 gvt_vgpu_err("invalid guest context LRCA: %x\n", desc->lrca); 1396 return ERR_PTR(-EINVAL); 1397 } 1398 1399 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa + 1400 RING_CTX_OFF(ring_header.val), &head, 4); 1401 1402 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa + 1403 RING_CTX_OFF(ring_tail.val), &tail, 4); 1404 1405 head &= RB_HEAD_OFF_MASK; 1406 tail &= RB_TAIL_OFF_MASK; 1407 1408 if (last_workload && same_context(&last_workload->ctx_desc, desc)) { 1409 gvt_dbg_el("ring id %d cur workload == last\n", ring_id); 1410 gvt_dbg_el("ctx head %x real head %lx\n", head, 1411 last_workload->rb_tail); 1412 /* 1413 * cannot use guest context head pointer here, 1414 * as it might not be updated at this time 1415 */ 1416 head = last_workload->rb_tail; 1417 } 1418 1419 gvt_dbg_el("ring id %d begin a new workload\n", ring_id); 1420 1421 /* record some ring buffer register values for scan and shadow */ 1422 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa + 1423 RING_CTX_OFF(rb_start.val), &start, 4); 1424 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa + 1425 RING_CTX_OFF(rb_ctrl.val), &ctl, 4); 1426 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa + 1427 RING_CTX_OFF(ctx_ctrl.val), &ctx_ctl, 4); 1428 1429 workload = alloc_workload(vgpu); 1430 if (IS_ERR(workload)) 1431 return workload; 1432 1433 workload->ring_id = ring_id; 1434 workload->ctx_desc = *desc; 1435 workload->ring_context_gpa = ring_context_gpa; 1436 workload->rb_head = head; 1437 workload->rb_tail = tail; 1438 workload->rb_start = start; 1439 workload->rb_ctl = ctl; 1440 1441 if (ring_id == RCS) { 1442 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa + 1443 RING_CTX_OFF(bb_per_ctx_ptr.val), &per_ctx, 4); 1444 intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa + 1445 RING_CTX_OFF(rcs_indirect_ctx.val), &indirect_ctx, 4); 1446 1447 workload->wa_ctx.indirect_ctx.guest_gma = 1448 indirect_ctx & INDIRECT_CTX_ADDR_MASK; 1449 workload->wa_ctx.indirect_ctx.size = 1450 (indirect_ctx & INDIRECT_CTX_SIZE_MASK) * 1451 CACHELINE_BYTES; 1452 workload->wa_ctx.per_ctx.guest_gma = 1453 per_ctx & PER_CTX_ADDR_MASK; 1454 workload->wa_ctx.per_ctx.valid = per_ctx & 1; 1455 } 1456 1457 gvt_dbg_el("workload %p ring id %d head %x tail %x start %x ctl %x\n", 1458 workload, ring_id, head, tail, start, ctl); 1459 1460 ret = prepare_mm(workload); 1461 if (ret) { 1462 kmem_cache_free(s->workloads, workload); 1463 return ERR_PTR(ret); 1464 } 1465 1466 /* Only scan and shadow the first workload in the queue 1467 * as there is only one pre-allocated buf-obj for shadow. 1468 */ 1469 if (list_empty(workload_q_head(vgpu, ring_id))) { 1470 intel_runtime_pm_get(dev_priv); 1471 mutex_lock(&dev_priv->drm.struct_mutex); 1472 ret = intel_gvt_scan_and_shadow_workload(workload); 1473 mutex_unlock(&dev_priv->drm.struct_mutex); 1474 intel_runtime_pm_put(dev_priv); 1475 } 1476 1477 if (ret && (vgpu_is_vm_unhealthy(ret))) { 1478 enter_failsafe_mode(vgpu, GVT_FAILSAFE_GUEST_ERR); 1479 intel_vgpu_destroy_workload(workload); 1480 return ERR_PTR(ret); 1481 } 1482 1483 return workload; 1484 } 1485 1486 /** 1487 * intel_vgpu_queue_workload - Qeue a vGPU workload 1488 * @workload: the workload to queue in 1489 */ 1490 void intel_vgpu_queue_workload(struct intel_vgpu_workload *workload) 1491 { 1492 list_add_tail(&workload->list, 1493 workload_q_head(workload->vgpu, workload->ring_id)); 1494 intel_gvt_kick_schedule(workload->vgpu->gvt); 1495 wake_up(&workload->vgpu->gvt->scheduler.waitq[workload->ring_id]); 1496 } 1497