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