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