Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2011-2016 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Zhi Wang <zhi.a.wang@intel.com> |
| 25 | * |
| 26 | * Contributors: |
| 27 | * Ping Gao <ping.a.gao@intel.com> |
| 28 | * Tina Zhang <tina.zhang@intel.com> |
| 29 | * Chanbin Du <changbin.du@intel.com> |
| 30 | * Min He <min.he@intel.com> |
| 31 | * Bing Niu <bing.niu@intel.com> |
| 32 | * Zhenyu Wang <zhenyuw@linux.intel.com> |
| 33 | * |
| 34 | */ |
| 35 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 36 | #include <linux/kthread.h> |
| 37 | |
Zhenyu Wang | feddf6e | 2016-10-20 17:15:03 +0800 | [diff] [blame] | 38 | #include "i915_drv.h" |
| 39 | #include "gvt.h" |
| 40 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 41 | #define RING_CTX_OFF(x) \ |
| 42 | offsetof(struct execlist_ring_context, x) |
| 43 | |
Du, Changbin | 999ccb4 | 2016-10-20 14:08:47 +0800 | [diff] [blame] | 44 | static void set_context_pdp_root_pointer( |
| 45 | struct execlist_ring_context *ring_context, |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 46 | u32 pdp[8]) |
| 47 | { |
| 48 | struct execlist_mmio_pair *pdp_pair = &ring_context->pdp3_UDW; |
| 49 | int i; |
| 50 | |
| 51 | for (i = 0; i < 8; i++) |
| 52 | pdp_pair[i].val = pdp[7 - i]; |
| 53 | } |
| 54 | |
| 55 | static int populate_shadow_context(struct intel_vgpu_workload *workload) |
| 56 | { |
| 57 | struct intel_vgpu *vgpu = workload->vgpu; |
| 58 | struct intel_gvt *gvt = vgpu->gvt; |
| 59 | int ring_id = workload->ring_id; |
| 60 | struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx; |
| 61 | struct drm_i915_gem_object *ctx_obj = |
| 62 | shadow_ctx->engine[ring_id].state->obj; |
| 63 | struct execlist_ring_context *shadow_ring_context; |
| 64 | struct page *page; |
| 65 | void *dst; |
| 66 | unsigned long context_gpa, context_page_num; |
| 67 | int i; |
| 68 | |
| 69 | gvt_dbg_sched("ring id %d workload lrca %x", ring_id, |
| 70 | workload->ctx_desc.lrca); |
| 71 | |
Joonas Lahtinen | 63ffbcd | 2017-04-28 10:53:36 +0300 | [diff] [blame] | 72 | context_page_num = gvt->dev_priv->engine[ring_id]->context_size; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 73 | |
| 74 | context_page_num = context_page_num >> PAGE_SHIFT; |
| 75 | |
| 76 | if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS) |
| 77 | context_page_num = 19; |
| 78 | |
| 79 | i = 2; |
| 80 | |
| 81 | while (i < context_page_num) { |
| 82 | context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm, |
| 83 | (u32)((workload->ctx_desc.lrca + i) << |
| 84 | GTT_PAGE_SHIFT)); |
| 85 | if (context_gpa == INTEL_GVT_INVALID_ADDR) { |
Tina Zhang | 695fbc0 | 2017-03-10 04:26:53 -0500 | [diff] [blame] | 86 | gvt_vgpu_err("Invalid guest context descriptor\n"); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i); |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 91 | dst = kmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 92 | intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst, |
| 93 | GTT_PAGE_SIZE); |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 94 | kunmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 95 | i++; |
| 96 | } |
| 97 | |
| 98 | page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN); |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 99 | shadow_ring_context = kmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 100 | |
| 101 | #define COPY_REG(name) \ |
| 102 | intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \ |
| 103 | + RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4) |
| 104 | |
| 105 | COPY_REG(ctx_ctrl); |
| 106 | COPY_REG(ctx_timestamp); |
| 107 | |
| 108 | if (ring_id == RCS) { |
| 109 | COPY_REG(bb_per_ctx_ptr); |
| 110 | COPY_REG(rcs_indirect_ctx); |
| 111 | COPY_REG(rcs_indirect_ctx_offset); |
| 112 | } |
| 113 | #undef COPY_REG |
| 114 | |
| 115 | set_context_pdp_root_pointer(shadow_ring_context, |
| 116 | workload->shadow_mm->shadow_page_table); |
| 117 | |
| 118 | intel_gvt_hypervisor_read_gpa(vgpu, |
| 119 | workload->ring_context_gpa + |
| 120 | sizeof(*shadow_ring_context), |
| 121 | (void *)shadow_ring_context + |
| 122 | sizeof(*shadow_ring_context), |
| 123 | GTT_PAGE_SIZE - sizeof(*shadow_ring_context)); |
| 124 | |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 125 | kunmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Changbin Du | bc2d4b6 | 2017-03-22 12:35:31 +0800 | [diff] [blame] | 129 | static inline bool is_gvt_request(struct drm_i915_gem_request *req) |
| 130 | { |
| 131 | return i915_gem_context_force_single_submission(req->ctx); |
| 132 | } |
| 133 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 134 | static int shadow_context_status_change(struct notifier_block *nb, |
| 135 | unsigned long action, void *data) |
| 136 | { |
Changbin Du | 3fc0306 | 2017-03-13 10:47:11 +0800 | [diff] [blame] | 137 | struct drm_i915_gem_request *req = (struct drm_i915_gem_request *)data; |
| 138 | struct intel_gvt *gvt = container_of(nb, struct intel_gvt, |
| 139 | shadow_ctx_notifier_block[req->engine->id]); |
| 140 | struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; |
Changbin Du | 0e86cc9 | 2017-05-04 10:52:38 +0800 | [diff] [blame] | 141 | enum intel_engine_id ring_id = req->engine->id; |
| 142 | struct intel_vgpu_workload *workload; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 143 | |
Changbin Du | 0e86cc9 | 2017-05-04 10:52:38 +0800 | [diff] [blame] | 144 | if (!is_gvt_request(req)) { |
| 145 | spin_lock_bh(&scheduler->mmio_context_lock); |
| 146 | if (action == INTEL_CONTEXT_SCHEDULE_IN && |
| 147 | scheduler->engine_owner[ring_id]) { |
| 148 | /* Switch ring from vGPU to host. */ |
| 149 | intel_gvt_switch_mmio(scheduler->engine_owner[ring_id], |
| 150 | NULL, ring_id); |
| 151 | scheduler->engine_owner[ring_id] = NULL; |
| 152 | } |
| 153 | spin_unlock_bh(&scheduler->mmio_context_lock); |
| 154 | |
| 155 | return NOTIFY_OK; |
| 156 | } |
| 157 | |
| 158 | workload = scheduler->current_workload[ring_id]; |
| 159 | if (unlikely(!workload)) |
Chuanxiao Dong | 9272f73 | 2017-02-17 19:29:52 +0800 | [diff] [blame] | 160 | return NOTIFY_OK; |
| 161 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 162 | switch (action) { |
| 163 | case INTEL_CONTEXT_SCHEDULE_IN: |
Changbin Du | 0e86cc9 | 2017-05-04 10:52:38 +0800 | [diff] [blame] | 164 | spin_lock_bh(&scheduler->mmio_context_lock); |
| 165 | if (workload->vgpu != scheduler->engine_owner[ring_id]) { |
| 166 | /* Switch ring from host to vGPU or vGPU to vGPU. */ |
| 167 | intel_gvt_switch_mmio(scheduler->engine_owner[ring_id], |
| 168 | workload->vgpu, ring_id); |
| 169 | scheduler->engine_owner[ring_id] = workload->vgpu; |
| 170 | } else |
| 171 | gvt_dbg_sched("skip ring %d mmio switch for vgpu%d\n", |
| 172 | ring_id, workload->vgpu->id); |
| 173 | spin_unlock_bh(&scheduler->mmio_context_lock); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 174 | atomic_set(&workload->shadow_ctx_active, 1); |
| 175 | break; |
| 176 | case INTEL_CONTEXT_SCHEDULE_OUT: |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 177 | atomic_set(&workload->shadow_ctx_active, 0); |
| 178 | break; |
| 179 | default: |
| 180 | WARN_ON(1); |
| 181 | return NOTIFY_OK; |
| 182 | } |
| 183 | wake_up(&workload->shadow_ctx_status_wq); |
| 184 | return NOTIFY_OK; |
| 185 | } |
| 186 | |
Ping Gao | 89ea20b | 2017-06-29 12:22:42 +0800 | [diff] [blame] | 187 | /** |
| 188 | * intel_gvt_scan_and_shadow_workload - audit the workload by scanning and |
| 189 | * shadow it as well, include ringbuffer,wa_ctx and ctx. |
| 190 | * @workload: an abstract entity for each execlist submission. |
| 191 | * |
| 192 | * This function is called before the workload submitting to i915, to make |
| 193 | * sure the content of the workload is valid. |
| 194 | */ |
| 195 | int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload) |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 196 | { |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 197 | int ring_id = workload->ring_id; |
| 198 | struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx; |
| 199 | struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv; |
Chris Wilson | 0eb742d | 2016-10-20 17:29:36 +0800 | [diff] [blame] | 200 | struct drm_i915_gem_request *rq; |
Tina Zhang | 695fbc0 | 2017-03-10 04:26:53 -0500 | [diff] [blame] | 201 | struct intel_vgpu *vgpu = workload->vgpu; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 202 | int ret; |
| 203 | |
Ping Gao | 87e919d | 2017-07-04 14:53:03 +0800 | [diff] [blame^] | 204 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 205 | |
Ping Gao | d0302e7 | 2017-06-29 12:22:43 +0800 | [diff] [blame] | 206 | if (workload->shadowed) |
| 207 | return 0; |
| 208 | |
Zhenyu Wang | 03806ed | 2017-02-13 17:07:19 +0800 | [diff] [blame] | 209 | shadow_ctx->desc_template &= ~(0x3 << GEN8_CTX_ADDRESSING_MODE_SHIFT); |
| 210 | shadow_ctx->desc_template |= workload->ctx_desc.addressing_mode << |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 211 | GEN8_CTX_ADDRESSING_MODE_SHIFT; |
| 212 | |
Chris Wilson | 0eb742d | 2016-10-20 17:29:36 +0800 | [diff] [blame] | 213 | rq = i915_gem_request_alloc(dev_priv->engine[ring_id], shadow_ctx); |
| 214 | if (IS_ERR(rq)) { |
Tina Zhang | 695fbc0 | 2017-03-10 04:26:53 -0500 | [diff] [blame] | 215 | gvt_vgpu_err("fail to allocate gem request\n"); |
Zhenyu Wang | 53d6f812 | 2016-11-24 15:55:49 +0800 | [diff] [blame] | 216 | ret = PTR_ERR(rq); |
| 217 | goto out; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Chris Wilson | 0eb742d | 2016-10-20 17:29:36 +0800 | [diff] [blame] | 220 | gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq); |
| 221 | |
| 222 | workload->req = i915_gem_request_get(rq); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 223 | |
Ping Gao | 89ea20b | 2017-06-29 12:22:42 +0800 | [diff] [blame] | 224 | ret = intel_gvt_scan_and_shadow_ringbuffer(workload); |
Zhi Wang | be1da70 | 2016-05-03 18:26:57 -0400 | [diff] [blame] | 225 | if (ret) |
Pei Zhang | 90d27a1 | 2016-11-14 18:02:57 +0800 | [diff] [blame] | 226 | goto out; |
Zhi Wang | be1da70 | 2016-05-03 18:26:57 -0400 | [diff] [blame] | 227 | |
Tina Zhang | 17f1b1a | 2017-03-15 23:16:01 -0400 | [diff] [blame] | 228 | if ((workload->ring_id == RCS) && |
| 229 | (workload->wa_ctx.indirect_ctx.size != 0)) { |
| 230 | ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx); |
| 231 | if (ret) |
| 232 | goto out; |
| 233 | } |
Zhi Wang | be1da70 | 2016-05-03 18:26:57 -0400 | [diff] [blame] | 234 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 235 | ret = populate_shadow_context(workload); |
Ping Gao | d0302e7 | 2017-06-29 12:22:43 +0800 | [diff] [blame] | 236 | if (ret) |
| 237 | goto out; |
| 238 | |
| 239 | workload->shadowed = true; |
Ping Gao | 89ea20b | 2017-06-29 12:22:42 +0800 | [diff] [blame] | 240 | |
| 241 | out: |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static int dispatch_workload(struct intel_vgpu_workload *workload) |
| 246 | { |
| 247 | int ring_id = workload->ring_id; |
| 248 | struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx; |
| 249 | struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv; |
| 250 | struct intel_engine_cs *engine = dev_priv->engine[ring_id]; |
| 251 | struct intel_vgpu *vgpu = workload->vgpu; |
| 252 | struct intel_ring *ring; |
| 253 | int ret = 0; |
| 254 | |
| 255 | gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n", |
| 256 | ring_id, workload); |
| 257 | |
| 258 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 259 | |
| 260 | ret = intel_gvt_scan_and_shadow_workload(workload); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 261 | if (ret) |
Pei Zhang | 90d27a1 | 2016-11-14 18:02:57 +0800 | [diff] [blame] | 262 | goto out; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 263 | |
| 264 | if (workload->prepare) { |
| 265 | ret = workload->prepare(workload); |
| 266 | if (ret) |
Pei Zhang | 90d27a1 | 2016-11-14 18:02:57 +0800 | [diff] [blame] | 267 | goto out; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Ping Gao | 89ea20b | 2017-06-29 12:22:42 +0800 | [diff] [blame] | 270 | /* pin shadow context by gvt even the shadow context will be pinned |
| 271 | * when i915 alloc request. That is because gvt will update the guest |
| 272 | * context from shadow context when workload is completed, and at that |
| 273 | * moment, i915 may already unpined the shadow context to make the |
| 274 | * shadow_ctx pages invalid. So gvt need to pin itself. After update |
| 275 | * the guest context, gvt can unpin the shadow_ctx safely. |
| 276 | */ |
| 277 | ring = engine->context_pin(engine, shadow_ctx); |
| 278 | if (IS_ERR(ring)) { |
| 279 | ret = PTR_ERR(ring); |
| 280 | gvt_vgpu_err("fail to pin shadow context\n"); |
| 281 | goto out; |
| 282 | } |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 283 | |
Pei Zhang | 90d27a1 | 2016-11-14 18:02:57 +0800 | [diff] [blame] | 284 | out: |
| 285 | if (ret) |
| 286 | workload->status = ret; |
Chris Wilson | 0eb742d | 2016-10-20 17:29:36 +0800 | [diff] [blame] | 287 | |
Ping Gao | 89ea20b | 2017-06-29 12:22:42 +0800 | [diff] [blame] | 288 | if (!IS_ERR_OR_NULL(workload->req)) { |
| 289 | gvt_dbg_sched("ring id %d submit workload to i915 %p\n", |
| 290 | ring_id, workload->req); |
| 291 | i915_add_request(workload->req); |
| 292 | workload->dispatched = true; |
| 293 | } |
Chuanxiao Dong | 3cd23b8 | 2017-03-16 09:47:58 +0800 | [diff] [blame] | 294 | |
Pei Zhang | 90d27a1 | 2016-11-14 18:02:57 +0800 | [diff] [blame] | 295 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static struct intel_vgpu_workload *pick_next_workload( |
| 300 | struct intel_gvt *gvt, int ring_id) |
| 301 | { |
| 302 | struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; |
| 303 | struct intel_vgpu_workload *workload = NULL; |
| 304 | |
| 305 | mutex_lock(&gvt->lock); |
| 306 | |
| 307 | /* |
| 308 | * no current vgpu / will be scheduled out / no workload |
| 309 | * bail out |
| 310 | */ |
| 311 | if (!scheduler->current_vgpu) { |
| 312 | gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id); |
| 313 | goto out; |
| 314 | } |
| 315 | |
| 316 | if (scheduler->need_reschedule) { |
| 317 | gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id); |
| 318 | goto out; |
| 319 | } |
| 320 | |
Zhenyu Wang | 954180a | 2017-04-12 14:22:50 +0800 | [diff] [blame] | 321 | if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 322 | goto out; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * still have current workload, maybe the workload disptacher |
| 326 | * fail to submit it for some reason, resubmit it. |
| 327 | */ |
| 328 | if (scheduler->current_workload[ring_id]) { |
| 329 | workload = scheduler->current_workload[ring_id]; |
| 330 | gvt_dbg_sched("ring id %d still have current workload %p\n", |
| 331 | ring_id, workload); |
| 332 | goto out; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * pick a workload as current workload |
| 337 | * once current workload is set, schedule policy routines |
| 338 | * will wait the current workload is finished when trying to |
| 339 | * schedule out a vgpu. |
| 340 | */ |
| 341 | scheduler->current_workload[ring_id] = container_of( |
| 342 | workload_q_head(scheduler->current_vgpu, ring_id)->next, |
| 343 | struct intel_vgpu_workload, list); |
| 344 | |
| 345 | workload = scheduler->current_workload[ring_id]; |
| 346 | |
| 347 | gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload); |
| 348 | |
| 349 | atomic_inc(&workload->vgpu->running_workload_num); |
| 350 | out: |
| 351 | mutex_unlock(&gvt->lock); |
| 352 | return workload; |
| 353 | } |
| 354 | |
| 355 | static void update_guest_context(struct intel_vgpu_workload *workload) |
| 356 | { |
| 357 | struct intel_vgpu *vgpu = workload->vgpu; |
| 358 | struct intel_gvt *gvt = vgpu->gvt; |
| 359 | int ring_id = workload->ring_id; |
| 360 | struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx; |
| 361 | struct drm_i915_gem_object *ctx_obj = |
| 362 | shadow_ctx->engine[ring_id].state->obj; |
| 363 | struct execlist_ring_context *shadow_ring_context; |
| 364 | struct page *page; |
| 365 | void *src; |
| 366 | unsigned long context_gpa, context_page_num; |
| 367 | int i; |
| 368 | |
| 369 | gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id, |
| 370 | workload->ctx_desc.lrca); |
| 371 | |
Joonas Lahtinen | 63ffbcd | 2017-04-28 10:53:36 +0300 | [diff] [blame] | 372 | context_page_num = gvt->dev_priv->engine[ring_id]->context_size; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 373 | |
| 374 | context_page_num = context_page_num >> PAGE_SHIFT; |
| 375 | |
| 376 | if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS) |
| 377 | context_page_num = 19; |
| 378 | |
| 379 | i = 2; |
| 380 | |
| 381 | while (i < context_page_num) { |
| 382 | context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm, |
| 383 | (u32)((workload->ctx_desc.lrca + i) << |
| 384 | GTT_PAGE_SHIFT)); |
| 385 | if (context_gpa == INTEL_GVT_INVALID_ADDR) { |
Tina Zhang | 695fbc0 | 2017-03-10 04:26:53 -0500 | [diff] [blame] | 386 | gvt_vgpu_err("invalid guest context descriptor\n"); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | |
| 390 | page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i); |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 391 | src = kmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 392 | intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src, |
| 393 | GTT_PAGE_SIZE); |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 394 | kunmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 395 | i++; |
| 396 | } |
| 397 | |
| 398 | intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + |
| 399 | RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4); |
| 400 | |
| 401 | page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN); |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 402 | shadow_ring_context = kmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 403 | |
| 404 | #define COPY_REG(name) \ |
| 405 | intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \ |
| 406 | RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4) |
| 407 | |
| 408 | COPY_REG(ctx_ctrl); |
| 409 | COPY_REG(ctx_timestamp); |
| 410 | |
| 411 | #undef COPY_REG |
| 412 | |
| 413 | intel_gvt_hypervisor_write_gpa(vgpu, |
| 414 | workload->ring_context_gpa + |
| 415 | sizeof(*shadow_ring_context), |
| 416 | (void *)shadow_ring_context + |
| 417 | sizeof(*shadow_ring_context), |
| 418 | GTT_PAGE_SIZE - sizeof(*shadow_ring_context)); |
| 419 | |
Xiaoguang Chen | c754936 | 2016-11-03 18:38:30 +0800 | [diff] [blame] | 420 | kunmap(page); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static void complete_current_workload(struct intel_gvt *gvt, int ring_id) |
| 424 | { |
| 425 | struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; |
| 426 | struct intel_vgpu_workload *workload; |
Changbin Du | 440a9b9 | 2017-01-05 16:49:03 +0800 | [diff] [blame] | 427 | struct intel_vgpu *vgpu; |
Zhi Wang | be1da70 | 2016-05-03 18:26:57 -0400 | [diff] [blame] | 428 | int event; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 429 | |
| 430 | mutex_lock(&gvt->lock); |
| 431 | |
| 432 | workload = scheduler->current_workload[ring_id]; |
Changbin Du | 440a9b9 | 2017-01-05 16:49:03 +0800 | [diff] [blame] | 433 | vgpu = workload->vgpu; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 434 | |
Chuanxiao Dong | 8f1117a | 2017-03-06 13:05:24 +0800 | [diff] [blame] | 435 | /* For the workload w/ request, needs to wait for the context |
| 436 | * switch to make sure request is completed. |
| 437 | * For the workload w/o request, directly complete the workload. |
| 438 | */ |
| 439 | if (workload->req) { |
Chuanxiao Dong | 3cd23b8 | 2017-03-16 09:47:58 +0800 | [diff] [blame] | 440 | struct drm_i915_private *dev_priv = |
| 441 | workload->vgpu->gvt->dev_priv; |
| 442 | struct intel_engine_cs *engine = |
| 443 | dev_priv->engine[workload->ring_id]; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 444 | wait_event(workload->shadow_ctx_status_wq, |
| 445 | !atomic_read(&workload->shadow_ctx_active)); |
| 446 | |
Chuanxiao Dong | 0cf5ec4 | 2017-06-23 13:01:11 +0800 | [diff] [blame] | 447 | /* If this request caused GPU hang, req->fence.error will |
| 448 | * be set to -EIO. Use -EIO to set workload status so |
| 449 | * that when this request caused GPU hang, didn't trigger |
| 450 | * context switch interrupt to guest. |
| 451 | */ |
| 452 | if (likely(workload->status == -EINPROGRESS)) { |
| 453 | if (workload->req->fence.error == -EIO) |
| 454 | workload->status = -EIO; |
| 455 | else |
| 456 | workload->status = 0; |
| 457 | } |
| 458 | |
Chuanxiao Dong | 8f1117a | 2017-03-06 13:05:24 +0800 | [diff] [blame] | 459 | i915_gem_request_put(fetch_and_zero(&workload->req)); |
Zhi Wang | be1da70 | 2016-05-03 18:26:57 -0400 | [diff] [blame] | 460 | |
Chuanxiao Dong | 8f1117a | 2017-03-06 13:05:24 +0800 | [diff] [blame] | 461 | if (!workload->status && !vgpu->resetting) { |
| 462 | update_guest_context(workload); |
| 463 | |
| 464 | for_each_set_bit(event, workload->pending_events, |
| 465 | INTEL_GVT_EVENT_MAX) |
| 466 | intel_vgpu_trigger_virtual_event(vgpu, event); |
| 467 | } |
Chuanxiao Dong | 3cd23b8 | 2017-03-16 09:47:58 +0800 | [diff] [blame] | 468 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 469 | /* unpin shadow ctx as the shadow_ctx update is done */ |
| 470 | engine->context_unpin(engine, workload->vgpu->shadow_ctx); |
| 471 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | gvt_dbg_sched("ring id %d complete workload %p status %d\n", |
| 475 | ring_id, workload, workload->status); |
| 476 | |
| 477 | scheduler->current_workload[ring_id] = NULL; |
| 478 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 479 | list_del_init(&workload->list); |
| 480 | workload->complete(workload); |
| 481 | |
Changbin Du | 440a9b9 | 2017-01-05 16:49:03 +0800 | [diff] [blame] | 482 | atomic_dec(&vgpu->running_workload_num); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 483 | wake_up(&scheduler->workload_complete_wq); |
Ping Gao | f100dae | 2017-05-24 09:14:11 +0800 | [diff] [blame] | 484 | |
| 485 | if (gvt->scheduler.need_reschedule) |
| 486 | intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EVENT_SCHED); |
| 487 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 488 | mutex_unlock(&gvt->lock); |
| 489 | } |
| 490 | |
| 491 | struct workload_thread_param { |
| 492 | struct intel_gvt *gvt; |
| 493 | int ring_id; |
| 494 | }; |
| 495 | |
| 496 | static int workload_thread(void *priv) |
| 497 | { |
| 498 | struct workload_thread_param *p = (struct workload_thread_param *)priv; |
| 499 | struct intel_gvt *gvt = p->gvt; |
| 500 | int ring_id = p->ring_id; |
| 501 | struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; |
| 502 | struct intel_vgpu_workload *workload = NULL; |
Tina Zhang | 695fbc0 | 2017-03-10 04:26:53 -0500 | [diff] [blame] | 503 | struct intel_vgpu *vgpu = NULL; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 504 | int ret; |
Xu Han | e3476c0 | 2017-03-29 10:13:59 +0800 | [diff] [blame] | 505 | bool need_force_wake = IS_SKYLAKE(gvt->dev_priv) |
| 506 | || IS_KABYLAKE(gvt->dev_priv); |
Du, Changbin | e45d7b7 | 2016-10-27 11:10:31 +0800 | [diff] [blame] | 507 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 508 | |
| 509 | kfree(p); |
| 510 | |
| 511 | gvt_dbg_core("workload thread for ring %d started\n", ring_id); |
| 512 | |
| 513 | while (!kthread_should_stop()) { |
Du, Changbin | e45d7b7 | 2016-10-27 11:10:31 +0800 | [diff] [blame] | 514 | add_wait_queue(&scheduler->waitq[ring_id], &wait); |
| 515 | do { |
| 516 | workload = pick_next_workload(gvt, ring_id); |
| 517 | if (workload) |
| 518 | break; |
| 519 | wait_woken(&wait, TASK_INTERRUPTIBLE, |
| 520 | MAX_SCHEDULE_TIMEOUT); |
| 521 | } while (!kthread_should_stop()); |
| 522 | remove_wait_queue(&scheduler->waitq[ring_id], &wait); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 523 | |
Du, Changbin | e45d7b7 | 2016-10-27 11:10:31 +0800 | [diff] [blame] | 524 | if (!workload) |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 525 | break; |
| 526 | |
| 527 | gvt_dbg_sched("ring id %d next workload %p vgpu %d\n", |
| 528 | workload->ring_id, workload, |
| 529 | workload->vgpu->id); |
| 530 | |
| 531 | intel_runtime_pm_get(gvt->dev_priv); |
| 532 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 533 | gvt_dbg_sched("ring id %d will dispatch workload %p\n", |
| 534 | workload->ring_id, workload); |
| 535 | |
| 536 | if (need_force_wake) |
| 537 | intel_uncore_forcewake_get(gvt->dev_priv, |
| 538 | FORCEWAKE_ALL); |
| 539 | |
Pei Zhang | 90d27a1 | 2016-11-14 18:02:57 +0800 | [diff] [blame] | 540 | mutex_lock(&gvt->lock); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 541 | ret = dispatch_workload(workload); |
Pei Zhang | 90d27a1 | 2016-11-14 18:02:57 +0800 | [diff] [blame] | 542 | mutex_unlock(&gvt->lock); |
Chris Wilson | 66bbc3b | 2016-10-19 11:11:44 +0100 | [diff] [blame] | 543 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 544 | if (ret) { |
Tina Zhang | 695fbc0 | 2017-03-10 04:26:53 -0500 | [diff] [blame] | 545 | vgpu = workload->vgpu; |
| 546 | gvt_vgpu_err("fail to dispatch workload, skip\n"); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 547 | goto complete; |
| 548 | } |
| 549 | |
| 550 | gvt_dbg_sched("ring id %d wait workload %p\n", |
| 551 | workload->ring_id, workload); |
Chris Wilson | 3dce2ac | 2017-03-08 22:08:08 +0000 | [diff] [blame] | 552 | i915_wait_request(workload->req, 0, MAX_SCHEDULE_TIMEOUT); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 553 | |
| 554 | complete: |
Changbin Du | 3ce3274 | 2017-02-09 10:13:16 +0800 | [diff] [blame] | 555 | gvt_dbg_sched("will complete workload %p, status: %d\n", |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 556 | workload, workload->status); |
| 557 | |
Changbin Du | 2e51ef3 | 2017-01-05 13:28:05 +0800 | [diff] [blame] | 558 | complete_current_workload(gvt, ring_id); |
| 559 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 560 | if (need_force_wake) |
| 561 | intel_uncore_forcewake_put(gvt->dev_priv, |
| 562 | FORCEWAKE_ALL); |
| 563 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 564 | intel_runtime_pm_put(gvt->dev_priv); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu) |
| 570 | { |
| 571 | struct intel_gvt *gvt = vgpu->gvt; |
| 572 | struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; |
| 573 | |
| 574 | if (atomic_read(&vgpu->running_workload_num)) { |
| 575 | gvt_dbg_sched("wait vgpu idle\n"); |
| 576 | |
| 577 | wait_event(scheduler->workload_complete_wq, |
| 578 | !atomic_read(&vgpu->running_workload_num)); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt) |
| 583 | { |
| 584 | struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; |
Changbin Du | 3fc0306 | 2017-03-13 10:47:11 +0800 | [diff] [blame] | 585 | struct intel_engine_cs *engine; |
| 586 | enum intel_engine_id i; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 587 | |
| 588 | gvt_dbg_core("clean workload scheduler\n"); |
| 589 | |
Changbin Du | 3fc0306 | 2017-03-13 10:47:11 +0800 | [diff] [blame] | 590 | for_each_engine(engine, gvt->dev_priv, i) { |
| 591 | atomic_notifier_chain_unregister( |
| 592 | &engine->context_status_notifier, |
| 593 | &gvt->shadow_ctx_notifier_block[i]); |
| 594 | kthread_stop(scheduler->thread[i]); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt) |
| 599 | { |
| 600 | struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; |
| 601 | struct workload_thread_param *param = NULL; |
Changbin Du | 3fc0306 | 2017-03-13 10:47:11 +0800 | [diff] [blame] | 602 | struct intel_engine_cs *engine; |
| 603 | enum intel_engine_id i; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 604 | int ret; |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 605 | |
| 606 | gvt_dbg_core("init workload scheduler\n"); |
| 607 | |
| 608 | init_waitqueue_head(&scheduler->workload_complete_wq); |
| 609 | |
Changbin Du | 3fc0306 | 2017-03-13 10:47:11 +0800 | [diff] [blame] | 610 | for_each_engine(engine, gvt->dev_priv, i) { |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 611 | init_waitqueue_head(&scheduler->waitq[i]); |
| 612 | |
| 613 | param = kzalloc(sizeof(*param), GFP_KERNEL); |
| 614 | if (!param) { |
| 615 | ret = -ENOMEM; |
| 616 | goto err; |
| 617 | } |
| 618 | |
| 619 | param->gvt = gvt; |
| 620 | param->ring_id = i; |
| 621 | |
| 622 | scheduler->thread[i] = kthread_run(workload_thread, param, |
| 623 | "gvt workload %d", i); |
| 624 | if (IS_ERR(scheduler->thread[i])) { |
| 625 | gvt_err("fail to create workload thread\n"); |
| 626 | ret = PTR_ERR(scheduler->thread[i]); |
| 627 | goto err; |
| 628 | } |
Changbin Du | 3fc0306 | 2017-03-13 10:47:11 +0800 | [diff] [blame] | 629 | |
| 630 | gvt->shadow_ctx_notifier_block[i].notifier_call = |
| 631 | shadow_context_status_change; |
| 632 | atomic_notifier_chain_register(&engine->context_status_notifier, |
| 633 | &gvt->shadow_ctx_notifier_block[i]); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 634 | } |
| 635 | return 0; |
| 636 | err: |
| 637 | intel_gvt_clean_workload_scheduler(gvt); |
| 638 | kfree(param); |
| 639 | param = NULL; |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu) |
| 644 | { |
Chris Wilson | 5f09a9c | 2017-06-20 12:05:46 +0100 | [diff] [blame] | 645 | i915_gem_context_put(vgpu->shadow_ctx); |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu) |
| 649 | { |
| 650 | atomic_set(&vgpu->running_workload_num, 0); |
| 651 | |
| 652 | vgpu->shadow_ctx = i915_gem_context_create_gvt( |
| 653 | &vgpu->gvt->dev_priv->drm); |
| 654 | if (IS_ERR(vgpu->shadow_ctx)) |
| 655 | return PTR_ERR(vgpu->shadow_ctx); |
| 656 | |
| 657 | vgpu->shadow_ctx->engine[RCS].initialised = true; |
| 658 | |
Zhi Wang | e473405 | 2016-05-01 07:42:16 -0400 | [diff] [blame] | 659 | return 0; |
| 660 | } |