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