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