blob: 2302a97cebf69bfb5e0e4641630d306a26a9a29e [file] [log] [blame]
Zhi Wange4734052016-05-01 07:42:16 -04001/*
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
36#include "i915_drv.h"
37
38#include <linux/kthread.h>
39
40#define RING_CTX_OFF(x) \
41 offsetof(struct execlist_ring_context, x)
42
43void set_context_pdp_root_pointer(struct execlist_ring_context *ring_context,
44 u32 pdp[8])
45{
46 struct execlist_mmio_pair *pdp_pair = &ring_context->pdp3_UDW;
47 int i;
48
49 for (i = 0; i < 8; i++)
50 pdp_pair[i].val = pdp[7 - i];
51}
52
53static int populate_shadow_context(struct intel_vgpu_workload *workload)
54{
55 struct intel_vgpu *vgpu = workload->vgpu;
56 struct intel_gvt *gvt = vgpu->gvt;
57 int ring_id = workload->ring_id;
58 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
59 struct drm_i915_gem_object *ctx_obj =
60 shadow_ctx->engine[ring_id].state->obj;
61 struct execlist_ring_context *shadow_ring_context;
62 struct page *page;
63 void *dst;
64 unsigned long context_gpa, context_page_num;
65 int i;
66
67 gvt_dbg_sched("ring id %d workload lrca %x", ring_id,
68 workload->ctx_desc.lrca);
69
70 context_page_num = intel_lr_context_size(
71 &gvt->dev_priv->engine[ring_id]);
72
73 context_page_num = context_page_num >> PAGE_SHIFT;
74
75 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
76 context_page_num = 19;
77
78 i = 2;
79
80 while (i < context_page_num) {
81 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
82 (u32)((workload->ctx_desc.lrca + i) <<
83 GTT_PAGE_SHIFT));
84 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
85 gvt_err("Invalid guest context descriptor\n");
86 return -EINVAL;
87 }
88
89 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
90 dst = kmap_atomic(page);
91 intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst,
92 GTT_PAGE_SIZE);
93 kunmap_atomic(dst);
94 i++;
95 }
96
97 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
98 shadow_ring_context = kmap_atomic(page);
99
100#define COPY_REG(name) \
101 intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \
102 + RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
103
104 COPY_REG(ctx_ctrl);
105 COPY_REG(ctx_timestamp);
106
107 if (ring_id == RCS) {
108 COPY_REG(bb_per_ctx_ptr);
109 COPY_REG(rcs_indirect_ctx);
110 COPY_REG(rcs_indirect_ctx_offset);
111 }
112#undef COPY_REG
113
114 set_context_pdp_root_pointer(shadow_ring_context,
115 workload->shadow_mm->shadow_page_table);
116
117 intel_gvt_hypervisor_read_gpa(vgpu,
118 workload->ring_context_gpa +
119 sizeof(*shadow_ring_context),
120 (void *)shadow_ring_context +
121 sizeof(*shadow_ring_context),
122 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
123
124 kunmap_atomic(shadow_ring_context);
125 return 0;
126}
127
128static int shadow_context_status_change(struct notifier_block *nb,
129 unsigned long action, void *data)
130{
131 struct intel_vgpu *vgpu = container_of(nb,
132 struct intel_vgpu, shadow_ctx_notifier_block);
133 struct drm_i915_gem_request *req =
134 (struct drm_i915_gem_request *)data;
135 struct intel_gvt_workload_scheduler *scheduler =
136 &vgpu->gvt->scheduler;
137 struct intel_vgpu_workload *workload =
138 scheduler->current_workload[req->engine->id];
139
140 switch (action) {
141 case INTEL_CONTEXT_SCHEDULE_IN:
142 atomic_set(&workload->shadow_ctx_active, 1);
143 break;
144 case INTEL_CONTEXT_SCHEDULE_OUT:
145 atomic_set(&workload->shadow_ctx_active, 0);
146 break;
147 default:
148 WARN_ON(1);
149 return NOTIFY_OK;
150 }
151 wake_up(&workload->shadow_ctx_status_wq);
152 return NOTIFY_OK;
153}
154
155static int dispatch_workload(struct intel_vgpu_workload *workload)
156{
157 struct intel_vgpu *vgpu = workload->vgpu;
158 struct intel_gvt *gvt = vgpu->gvt;
159 int ring_id = workload->ring_id;
160 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
161 struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
162 int ret;
163
164 gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
165 ring_id, workload);
166
167 shadow_ctx->desc_template = workload->ctx_desc.addressing_mode <<
168 GEN8_CTX_ADDRESSING_MODE_SHIFT;
169
170 workload->req = i915_gem_request_alloc(&dev_priv->engine[ring_id],
171 shadow_ctx);
172 if (IS_ERR_OR_NULL(workload->req)) {
173 gvt_err("fail to allocate gem request\n");
174 workload->status = PTR_ERR(workload->req);
175 workload->req = NULL;
176 return workload->status;
177 }
178
179 gvt_dbg_sched("ring id %d get i915 gem request %p\n",
180 ring_id, workload->req);
181
182 mutex_lock(&gvt->lock);
183
184 ret = populate_shadow_context(workload);
185 if (ret)
186 goto err;
187
188 if (workload->prepare) {
189 ret = workload->prepare(workload);
190 if (ret)
191 goto err;
192 }
193
194 mutex_unlock(&gvt->lock);
195
196 gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
197 ring_id, workload->req);
198
199 i915_add_request_no_flush(workload->req);
200
201 workload->dispatched = true;
202 return 0;
203err:
204 workload->status = ret;
205 if (workload->req)
206 workload->req = NULL;
207
208 mutex_unlock(&gvt->lock);
209 return ret;
210}
211
212static struct intel_vgpu_workload *pick_next_workload(
213 struct intel_gvt *gvt, int ring_id)
214{
215 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
216 struct intel_vgpu_workload *workload = NULL;
217
218 mutex_lock(&gvt->lock);
219
220 /*
221 * no current vgpu / will be scheduled out / no workload
222 * bail out
223 */
224 if (!scheduler->current_vgpu) {
225 gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
226 goto out;
227 }
228
229 if (scheduler->need_reschedule) {
230 gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
231 goto out;
232 }
233
234 if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) {
235 gvt_dbg_sched("ring id %d stop - no available workload\n",
236 ring_id);
237 goto out;
238 }
239
240 /*
241 * still have current workload, maybe the workload disptacher
242 * fail to submit it for some reason, resubmit it.
243 */
244 if (scheduler->current_workload[ring_id]) {
245 workload = scheduler->current_workload[ring_id];
246 gvt_dbg_sched("ring id %d still have current workload %p\n",
247 ring_id, workload);
248 goto out;
249 }
250
251 /*
252 * pick a workload as current workload
253 * once current workload is set, schedule policy routines
254 * will wait the current workload is finished when trying to
255 * schedule out a vgpu.
256 */
257 scheduler->current_workload[ring_id] = container_of(
258 workload_q_head(scheduler->current_vgpu, ring_id)->next,
259 struct intel_vgpu_workload, list);
260
261 workload = scheduler->current_workload[ring_id];
262
263 gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
264
265 atomic_inc(&workload->vgpu->running_workload_num);
266out:
267 mutex_unlock(&gvt->lock);
268 return workload;
269}
270
271static void update_guest_context(struct intel_vgpu_workload *workload)
272{
273 struct intel_vgpu *vgpu = workload->vgpu;
274 struct intel_gvt *gvt = vgpu->gvt;
275 int ring_id = workload->ring_id;
276 struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
277 struct drm_i915_gem_object *ctx_obj =
278 shadow_ctx->engine[ring_id].state->obj;
279 struct execlist_ring_context *shadow_ring_context;
280 struct page *page;
281 void *src;
282 unsigned long context_gpa, context_page_num;
283 int i;
284
285 gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
286 workload->ctx_desc.lrca);
287
288 context_page_num = intel_lr_context_size(
289 &gvt->dev_priv->engine[ring_id]);
290
291 context_page_num = context_page_num >> PAGE_SHIFT;
292
293 if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
294 context_page_num = 19;
295
296 i = 2;
297
298 while (i < context_page_num) {
299 context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
300 (u32)((workload->ctx_desc.lrca + i) <<
301 GTT_PAGE_SHIFT));
302 if (context_gpa == INTEL_GVT_INVALID_ADDR) {
303 gvt_err("invalid guest context descriptor\n");
304 return;
305 }
306
307 page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
308 src = kmap_atomic(page);
309 intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
310 GTT_PAGE_SIZE);
311 kunmap_atomic(src);
312 i++;
313 }
314
315 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
316 RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
317
318 page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
319 shadow_ring_context = kmap_atomic(page);
320
321#define COPY_REG(name) \
322 intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
323 RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
324
325 COPY_REG(ctx_ctrl);
326 COPY_REG(ctx_timestamp);
327
328#undef COPY_REG
329
330 intel_gvt_hypervisor_write_gpa(vgpu,
331 workload->ring_context_gpa +
332 sizeof(*shadow_ring_context),
333 (void *)shadow_ring_context +
334 sizeof(*shadow_ring_context),
335 GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
336
337 kunmap_atomic(shadow_ring_context);
338}
339
340static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
341{
342 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
343 struct intel_vgpu_workload *workload;
344
345 mutex_lock(&gvt->lock);
346
347 workload = scheduler->current_workload[ring_id];
348
349 if (!workload->status && !workload->vgpu->resetting) {
350 wait_event(workload->shadow_ctx_status_wq,
351 !atomic_read(&workload->shadow_ctx_active));
352
353 update_guest_context(workload);
354 }
355
356 gvt_dbg_sched("ring id %d complete workload %p status %d\n",
357 ring_id, workload, workload->status);
358
359 scheduler->current_workload[ring_id] = NULL;
360
361 atomic_dec(&workload->vgpu->running_workload_num);
362
363 list_del_init(&workload->list);
364 workload->complete(workload);
365
366 wake_up(&scheduler->workload_complete_wq);
367 mutex_unlock(&gvt->lock);
368}
369
370struct workload_thread_param {
371 struct intel_gvt *gvt;
372 int ring_id;
373};
374
375static int workload_thread(void *priv)
376{
377 struct workload_thread_param *p = (struct workload_thread_param *)priv;
378 struct intel_gvt *gvt = p->gvt;
379 int ring_id = p->ring_id;
380 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
381 struct intel_vgpu_workload *workload = NULL;
382 int ret;
383 bool need_force_wake = IS_SKYLAKE(gvt->dev_priv);
384
385 kfree(p);
386
387 gvt_dbg_core("workload thread for ring %d started\n", ring_id);
388
389 while (!kthread_should_stop()) {
390 ret = wait_event_interruptible(scheduler->waitq[ring_id],
391 kthread_should_stop() ||
392 (workload = pick_next_workload(gvt, ring_id)));
393
394 WARN_ON_ONCE(ret);
395
396 if (kthread_should_stop())
397 break;
398
399 gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
400 workload->ring_id, workload,
401 workload->vgpu->id);
402
403 intel_runtime_pm_get(gvt->dev_priv);
404
405 /*
406 * Always take i915 big lock first
407 */
408 ret = i915_mutex_lock_interruptible(&gvt->dev_priv->drm);
409 if (ret < 0) {
410 gvt_err("i915 submission is not available, retry\n");
411 schedule_timeout(1);
412 continue;
413 }
414
415 gvt_dbg_sched("ring id %d will dispatch workload %p\n",
416 workload->ring_id, workload);
417
418 if (need_force_wake)
419 intel_uncore_forcewake_get(gvt->dev_priv,
420 FORCEWAKE_ALL);
421
422 ret = dispatch_workload(workload);
423 if (ret) {
424 gvt_err("fail to dispatch workload, skip\n");
425 goto complete;
426 }
427
428 gvt_dbg_sched("ring id %d wait workload %p\n",
429 workload->ring_id, workload);
430
431 workload->status = i915_wait_request(workload->req,
432 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
433 NULL, NULL);
434 if (workload->status != 0)
435 gvt_err("fail to wait workload, skip\n");
436
437complete:
438 gvt_dbg_sched("will complete workload %p\n, status: %d\n",
439 workload, workload->status);
440
441 complete_current_workload(gvt, ring_id);
442
443 if (need_force_wake)
444 intel_uncore_forcewake_put(gvt->dev_priv,
445 FORCEWAKE_ALL);
446
447 mutex_unlock(&gvt->dev_priv->drm.struct_mutex);
448
449 intel_runtime_pm_put(gvt->dev_priv);
450 }
451 return 0;
452}
453
454void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
455{
456 struct intel_gvt *gvt = vgpu->gvt;
457 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
458
459 if (atomic_read(&vgpu->running_workload_num)) {
460 gvt_dbg_sched("wait vgpu idle\n");
461
462 wait_event(scheduler->workload_complete_wq,
463 !atomic_read(&vgpu->running_workload_num));
464 }
465}
466
467void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
468{
469 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
470 int i;
471
472 gvt_dbg_core("clean workload scheduler\n");
473
474 for (i = 0; i < I915_NUM_ENGINES; i++) {
475 if (scheduler->thread[i]) {
476 kthread_stop(scheduler->thread[i]);
477 scheduler->thread[i] = NULL;
478 }
479 }
480}
481
482int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
483{
484 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
485 struct workload_thread_param *param = NULL;
486 int ret;
487 int i;
488
489 gvt_dbg_core("init workload scheduler\n");
490
491 init_waitqueue_head(&scheduler->workload_complete_wq);
492
493 for (i = 0; i < I915_NUM_ENGINES; i++) {
494 init_waitqueue_head(&scheduler->waitq[i]);
495
496 param = kzalloc(sizeof(*param), GFP_KERNEL);
497 if (!param) {
498 ret = -ENOMEM;
499 goto err;
500 }
501
502 param->gvt = gvt;
503 param->ring_id = i;
504
505 scheduler->thread[i] = kthread_run(workload_thread, param,
506 "gvt workload %d", i);
507 if (IS_ERR(scheduler->thread[i])) {
508 gvt_err("fail to create workload thread\n");
509 ret = PTR_ERR(scheduler->thread[i]);
510 goto err;
511 }
512 }
513 return 0;
514err:
515 intel_gvt_clean_workload_scheduler(gvt);
516 kfree(param);
517 param = NULL;
518 return ret;
519}
520
521void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
522{
523 struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
524
525 atomic_notifier_chain_unregister(&vgpu->shadow_ctx->status_notifier,
526 &vgpu->shadow_ctx_notifier_block);
527
528 mutex_lock(&dev_priv->drm.struct_mutex);
529
530 /* a little hacky to mark as ctx closed */
531 vgpu->shadow_ctx->closed = true;
532 i915_gem_context_put(vgpu->shadow_ctx);
533
534 mutex_unlock(&dev_priv->drm.struct_mutex);
535}
536
537int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu)
538{
539 atomic_set(&vgpu->running_workload_num, 0);
540
541 vgpu->shadow_ctx = i915_gem_context_create_gvt(
542 &vgpu->gvt->dev_priv->drm);
543 if (IS_ERR(vgpu->shadow_ctx))
544 return PTR_ERR(vgpu->shadow_ctx);
545
546 vgpu->shadow_ctx->engine[RCS].initialised = true;
547
548 vgpu->shadow_ctx_notifier_block.notifier_call =
549 shadow_context_status_change;
550
551 atomic_notifier_chain_register(&vgpu->shadow_ctx->status_notifier,
552 &vgpu->shadow_ctx_notifier_block);
553 return 0;
554}