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