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