2 * SPDX-License-Identifier: MIT
4 * Copyright © 2011-2012 Intel Corporation
8 * This file implements HW context support. On gen5+ a HW context consists of an
9 * opaque GPU object which is referenced at times of context saves and restores.
10 * With RC6 enabled, the context is also referenced as the GPU enters and exists
11 * from RC6 (GPU has it's own internal power context, except on gen5). Though
12 * something like a context does exist for the media ring, the code only
13 * supports contexts for the render ring.
15 * In software, there is a distinction between contexts created by the user,
16 * and the default HW context. The default HW context is used by GPU clients
17 * that do not request setup of their own hardware context. The default
18 * context's state is never restored to help prevent programming errors. This
19 * would happen if a client ran and piggy-backed off another clients GPU state.
20 * The default context only exists to give the GPU some offset to load as the
21 * current to invoke a save of the context we actually care about. In fact, the
22 * code could likely be constructed, albeit in a more complicated fashion, to
23 * never use the default context, though that limits the driver's ability to
24 * swap out, and/or destroy other contexts.
26 * All other contexts are created as a request by the GPU client. These contexts
27 * store GPU state, and thus allow GPU clients to not re-emit state (and
28 * potentially query certain state) at any time. The kernel driver makes
29 * certain that the appropriate commands are inserted.
31 * The context life cycle is semi-complicated in that context BOs may live
32 * longer than the context itself because of the way the hardware, and object
33 * tracking works. Below is a very crude representation of the state machine
34 * describing the context life.
35 * refcount pincount active
36 * S0: initial state 0 0 0
37 * S1: context created 1 0 0
38 * S2: context is currently running 2 1 X
39 * S3: GPU referenced, but not current 2 0 1
40 * S4: context is current, but destroyed 1 1 0
41 * S5: like S3, but destroyed 1 0 1
43 * The most common (but not all) transitions:
44 * S0->S1: client creates a context
45 * S1->S2: client submits execbuf with context
46 * S2->S3: other clients submits execbuf with context
47 * S3->S1: context object was retired
48 * S3->S2: clients submits another execbuf
49 * S2->S4: context destroy called with current context
50 * S3->S5->S0: destroy path
51 * S4->S5->S0: destroy path on current context
53 * There are two confusing terms used above:
54 * The "current context" means the context which is currently running on the
55 * GPU. The GPU has loaded its state already and has stored away the gtt
56 * offset of the BO. The GPU is not actively referencing the data at this
57 * offset, but it will on the next context switch. The only way to avoid this
58 * is to do a GPU reset.
60 * An "active context' is one which was previously the "current context" and is
61 * on the active list waiting for the next context switch to occur. Until this
62 * happens, the object must remain at the same gtt offset. It is therefore
63 * possible to destroy a context, but it is still active.
67 #include <linux/log2.h>
68 #include <linux/nospec.h>
70 #include <drm/i915_drm.h>
72 #include "gt/intel_engine_heartbeat.h"
73 #include "gt/intel_engine_user.h"
74 #include "gt/intel_lrc_reg.h"
75 #include "gt/intel_ring.h"
77 #include "i915_gem_context.h"
78 #include "i915_globals.h"
79 #include "i915_trace.h"
80 #include "i915_user_extensions.h"
82 #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
84 static struct i915_global_gem_context {
85 struct i915_global base;
86 struct kmem_cache *slab_luts;
89 struct i915_lut_handle *i915_lut_handle_alloc(void)
91 return kmem_cache_alloc(global.slab_luts, GFP_KERNEL);
94 void i915_lut_handle_free(struct i915_lut_handle *lut)
96 return kmem_cache_free(global.slab_luts, lut);
99 static void lut_close(struct i915_gem_context *ctx)
101 struct radix_tree_iter iter;
104 lockdep_assert_held(&ctx->mutex);
107 radix_tree_for_each_slot(slot, &ctx->handles_vma, &iter, 0) {
108 struct i915_vma *vma = rcu_dereference_raw(*slot);
109 struct drm_i915_gem_object *obj = vma->obj;
110 struct i915_lut_handle *lut;
112 if (!kref_get_unless_zero(&obj->base.refcount))
116 i915_gem_object_lock(obj);
117 list_for_each_entry(lut, &obj->lut_list, obj_link) {
121 if (lut->handle != iter.index)
124 list_del(&lut->obj_link);
127 i915_gem_object_unlock(obj);
130 if (&lut->obj_link != &obj->lut_list) {
131 i915_lut_handle_free(lut);
132 radix_tree_iter_delete(&ctx->handles_vma, &iter, slot);
133 if (atomic_dec_and_test(&vma->open_count) &&
134 !i915_vma_is_ggtt(vma))
136 i915_gem_object_put(obj);
139 i915_gem_object_put(obj);
144 static struct intel_context *
145 lookup_user_engine(struct i915_gem_context *ctx,
147 const struct i915_engine_class_instance *ci)
148 #define LOOKUP_USER_INDEX BIT(0)
152 if (!!(flags & LOOKUP_USER_INDEX) != i915_gem_context_user_engines(ctx))
153 return ERR_PTR(-EINVAL);
155 if (!i915_gem_context_user_engines(ctx)) {
156 struct intel_engine_cs *engine;
158 engine = intel_engine_lookup_user(ctx->i915,
160 ci->engine_instance);
162 return ERR_PTR(-EINVAL);
164 idx = engine->legacy_idx;
166 idx = ci->engine_instance;
169 return i915_gem_context_get_engine(ctx, idx);
172 static void __free_engines(struct i915_gem_engines *e, unsigned int count)
175 if (!e->engines[count])
178 intel_context_put(e->engines[count]);
183 static void free_engines(struct i915_gem_engines *e)
185 __free_engines(e, e->num_engines);
188 static void free_engines_rcu(struct rcu_head *rcu)
190 free_engines(container_of(rcu, struct i915_gem_engines, rcu));
193 static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx)
195 const struct intel_gt *gt = &ctx->i915->gt;
196 struct intel_engine_cs *engine;
197 struct i915_gem_engines *e;
198 enum intel_engine_id id;
200 e = kzalloc(struct_size(e, engines, I915_NUM_ENGINES), GFP_KERNEL);
202 return ERR_PTR(-ENOMEM);
204 init_rcu_head(&e->rcu);
205 for_each_engine(engine, gt, id) {
206 struct intel_context *ce;
208 if (engine->legacy_idx == INVALID_ENGINE)
211 GEM_BUG_ON(engine->legacy_idx >= I915_NUM_ENGINES);
212 GEM_BUG_ON(e->engines[engine->legacy_idx]);
214 ce = intel_context_create(ctx, engine);
216 __free_engines(e, e->num_engines + 1);
220 e->engines[engine->legacy_idx] = ce;
221 e->num_engines = max(e->num_engines, engine->legacy_idx);
228 static void i915_gem_context_free(struct i915_gem_context *ctx)
230 GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
232 spin_lock(&ctx->i915->gem.contexts.lock);
233 list_del(&ctx->link);
234 spin_unlock(&ctx->i915->gem.contexts.lock);
236 free_engines(rcu_access_pointer(ctx->engines));
237 mutex_destroy(&ctx->engines_mutex);
239 kfree(ctx->jump_whitelist);
242 intel_timeline_put(ctx->timeline);
247 mutex_destroy(&ctx->mutex);
252 static void contexts_free_all(struct llist_node *list)
254 struct i915_gem_context *ctx, *cn;
256 llist_for_each_entry_safe(ctx, cn, list, free_link)
257 i915_gem_context_free(ctx);
260 static void contexts_flush_free(struct i915_gem_contexts *gc)
262 contexts_free_all(llist_del_all(&gc->free_list));
265 static void contexts_free_worker(struct work_struct *work)
267 struct i915_gem_contexts *gc =
268 container_of(work, typeof(*gc), free_work);
270 contexts_flush_free(gc);
273 void i915_gem_context_release(struct kref *ref)
275 struct i915_gem_context *ctx = container_of(ref, typeof(*ctx), ref);
276 struct i915_gem_contexts *gc = &ctx->i915->gem.contexts;
278 trace_i915_context_free(ctx);
279 if (llist_add(&ctx->free_link, &gc->free_list))
280 schedule_work(&gc->free_work);
283 static inline struct i915_gem_engines *
284 __context_engines_static(const struct i915_gem_context *ctx)
286 return rcu_dereference_protected(ctx->engines, true);
289 static bool __reset_engine(struct intel_engine_cs *engine)
291 struct intel_gt *gt = engine->gt;
292 bool success = false;
294 if (!intel_has_reset_engine(gt))
297 if (!test_and_set_bit(I915_RESET_ENGINE + engine->id,
299 success = intel_engine_reset(engine, NULL) == 0;
300 clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
307 static void __reset_context(struct i915_gem_context *ctx,
308 struct intel_engine_cs *engine)
310 intel_gt_handle_error(engine->gt, engine->mask, 0,
311 "context closure in %s", ctx->name);
314 static bool __cancel_engine(struct intel_engine_cs *engine)
317 * Send a "high priority pulse" down the engine to cause the
318 * current request to be momentarily preempted. (If it fails to
319 * be preempted, it will be reset). As we have marked our context
320 * as banned, any incomplete request, including any running, will
321 * be skipped following the preemption.
323 * If there is no hangchecking (one of the reasons why we try to
324 * cancel the context) and no forced preemption, there may be no
325 * means by which we reset the GPU and evict the persistent hog.
326 * Ergo if we are unable to inject a preemptive pulse that can
327 * kill the banned context, we fallback to doing a local reset
330 if (IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT) &&
331 !intel_engine_pulse(engine))
334 /* If we are unable to send a pulse, try resetting this engine. */
335 return __reset_engine(engine);
338 static struct intel_engine_cs *__active_engine(struct i915_request *rq)
340 struct intel_engine_cs *engine, *locked;
343 * Serialise with __i915_request_submit() so that it sees
344 * is-banned?, or we know the request is already inflight.
346 locked = READ_ONCE(rq->engine);
347 spin_lock_irq(&locked->active.lock);
348 while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
349 spin_unlock(&locked->active.lock);
350 spin_lock(&engine->active.lock);
355 if (i915_request_is_active(rq) && !rq->fence.error)
358 spin_unlock_irq(&locked->active.lock);
363 static struct intel_engine_cs *active_engine(struct intel_context *ce)
365 struct intel_engine_cs *engine = NULL;
366 struct i915_request *rq;
371 mutex_lock(&ce->timeline->mutex);
372 list_for_each_entry_reverse(rq, &ce->timeline->requests, link) {
373 if (i915_request_completed(rq))
376 /* Check with the backend if the request is inflight */
377 engine = __active_engine(rq);
381 mutex_unlock(&ce->timeline->mutex);
386 static void kill_context(struct i915_gem_context *ctx)
388 struct i915_gem_engines_iter it;
389 struct intel_context *ce;
392 * If we are already banned, it was due to a guilty request causing
393 * a reset and the entire context being evicted from the GPU.
395 if (i915_gem_context_is_banned(ctx))
398 i915_gem_context_set_banned(ctx);
401 * Map the user's engine back to the actual engines; one virtual
402 * engine will be mapped to multiple engines, and using ctx->engine[]
403 * the same engine may be have multiple instances in the user's map.
404 * However, we only care about pending requests, so only include
405 * engines on which there are incomplete requests.
407 for_each_gem_engine(ce, __context_engines_static(ctx), it) {
408 struct intel_engine_cs *engine;
411 * Check the current active state of this context; if we
412 * are currently executing on the GPU we need to evict
413 * ourselves. On the other hand, if we haven't yet been
414 * submitted to the GPU or if everything is complete,
415 * we have nothing to do.
417 engine = active_engine(ce);
419 /* First attempt to gracefully cancel the context */
420 if (engine && !__cancel_engine(engine))
422 * If we are unable to send a preemptive pulse to bump
423 * the context from the GPU, we have to resort to a full
424 * reset. We hope the collateral damage is worth it.
426 __reset_context(ctx, engine);
430 static void context_close(struct i915_gem_context *ctx)
432 struct i915_address_space *vm;
434 i915_gem_context_set_closed(ctx);
436 mutex_lock(&ctx->mutex);
438 vm = i915_gem_context_vm(ctx);
442 ctx->file_priv = ERR_PTR(-EBADF);
445 * The LUT uses the VMA as a backpointer to unref the object,
446 * so we need to clear the LUT before we close all the VMA (inside
451 mutex_unlock(&ctx->mutex);
454 * If the user has disabled hangchecking, we can not be sure that
455 * the batches will ever complete after the context is closed,
456 * keeping the context and all resources pinned forever. So in this
457 * case we opt to forcibly kill off all remaining requests on
460 if (!i915_gem_context_is_persistent(ctx) ||
461 !i915_modparams.enable_hangcheck)
464 i915_gem_context_put(ctx);
467 static int __context_set_persistence(struct i915_gem_context *ctx, bool state)
469 if (i915_gem_context_is_persistent(ctx) == state)
474 * Only contexts that are short-lived [that will expire or be
475 * reset] are allowed to survive past termination. We require
476 * hangcheck to ensure that the persistent requests are healthy.
478 if (!i915_modparams.enable_hangcheck)
481 i915_gem_context_set_persistence(ctx);
483 /* To cancel a context we use "preempt-to-idle" */
484 if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PREEMPTION))
487 i915_gem_context_clear_persistence(ctx);
493 static struct i915_gem_context *
494 __create_context(struct drm_i915_private *i915)
496 struct i915_gem_context *ctx;
497 struct i915_gem_engines *e;
501 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
503 return ERR_PTR(-ENOMEM);
505 kref_init(&ctx->ref);
507 ctx->sched.priority = I915_USER_PRIORITY(I915_PRIORITY_NORMAL);
508 mutex_init(&ctx->mutex);
510 mutex_init(&ctx->engines_mutex);
511 e = default_engines(ctx);
516 RCU_INIT_POINTER(ctx->engines, e);
518 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
520 /* NB: Mark all slices as needing a remap so that when the context first
521 * loads it will restore whatever remap state already exists. If there
522 * is no remap info, it will be a NOP. */
523 ctx->remap_slice = ALL_L3_SLICES(i915);
525 i915_gem_context_set_bannable(ctx);
526 i915_gem_context_set_recoverable(ctx);
527 __context_set_persistence(ctx, true /* cgroup hook? */);
529 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp); i++)
530 ctx->hang_timestamp[i] = jiffies - CONTEXT_FAST_HANG_JIFFIES;
532 ctx->jump_whitelist = NULL;
533 ctx->jump_whitelist_cmds = 0;
535 spin_lock(&i915->gem.contexts.lock);
536 list_add_tail(&ctx->link, &i915->gem.contexts.list);
537 spin_unlock(&i915->gem.contexts.lock);
547 context_apply_all(struct i915_gem_context *ctx,
548 void (*fn)(struct intel_context *ce, void *data),
551 struct i915_gem_engines_iter it;
552 struct intel_context *ce;
554 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it)
556 i915_gem_context_unlock_engines(ctx);
559 static void __apply_ppgtt(struct intel_context *ce, void *vm)
562 ce->vm = i915_vm_get(vm);
565 static struct i915_address_space *
566 __set_ppgtt(struct i915_gem_context *ctx, struct i915_address_space *vm)
568 struct i915_address_space *old = i915_gem_context_vm(ctx);
570 GEM_BUG_ON(old && i915_vm_is_4lvl(vm) != i915_vm_is_4lvl(old));
572 rcu_assign_pointer(ctx->vm, i915_vm_open(vm));
573 context_apply_all(ctx, __apply_ppgtt, vm);
578 static void __assign_ppgtt(struct i915_gem_context *ctx,
579 struct i915_address_space *vm)
581 if (vm == rcu_access_pointer(ctx->vm))
584 vm = __set_ppgtt(ctx, vm);
589 static void __set_timeline(struct intel_timeline **dst,
590 struct intel_timeline *src)
592 struct intel_timeline *old = *dst;
594 *dst = src ? intel_timeline_get(src) : NULL;
597 intel_timeline_put(old);
600 static void __apply_timeline(struct intel_context *ce, void *timeline)
602 __set_timeline(&ce->timeline, timeline);
605 static void __assign_timeline(struct i915_gem_context *ctx,
606 struct intel_timeline *timeline)
608 __set_timeline(&ctx->timeline, timeline);
609 context_apply_all(ctx, __apply_timeline, timeline);
612 static struct i915_gem_context *
613 i915_gem_create_context(struct drm_i915_private *i915, unsigned int flags)
615 struct i915_gem_context *ctx;
617 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE &&
618 !HAS_EXECLISTS(i915))
619 return ERR_PTR(-EINVAL);
621 /* Reap the stale contexts */
622 contexts_flush_free(&i915->gem.contexts);
624 ctx = __create_context(i915);
628 if (HAS_FULL_PPGTT(i915)) {
629 struct i915_ppgtt *ppgtt;
631 ppgtt = i915_ppgtt_create(i915);
633 DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
636 return ERR_CAST(ppgtt);
639 mutex_lock(&ctx->mutex);
640 __assign_ppgtt(ctx, &ppgtt->vm);
641 mutex_unlock(&ctx->mutex);
643 i915_vm_put(&ppgtt->vm);
646 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE) {
647 struct intel_timeline *timeline;
649 timeline = intel_timeline_create(&i915->gt, NULL);
650 if (IS_ERR(timeline)) {
652 return ERR_CAST(timeline);
655 __assign_timeline(ctx, timeline);
656 intel_timeline_put(timeline);
659 trace_i915_context_create(ctx);
665 destroy_kernel_context(struct i915_gem_context **ctxp)
667 struct i915_gem_context *ctx;
669 /* Keep the context ref so that we can free it immediately ourselves */
670 ctx = i915_gem_context_get(fetch_and_zero(ctxp));
671 GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
674 i915_gem_context_free(ctx);
677 struct i915_gem_context *
678 i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio)
680 struct i915_gem_context *ctx;
682 ctx = i915_gem_create_context(i915, 0);
686 i915_gem_context_clear_bannable(ctx);
687 i915_gem_context_set_persistence(ctx);
688 ctx->sched.priority = I915_USER_PRIORITY(prio);
690 GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
695 static void init_contexts(struct i915_gem_contexts *gc)
697 spin_lock_init(&gc->lock);
698 INIT_LIST_HEAD(&gc->list);
700 INIT_WORK(&gc->free_work, contexts_free_worker);
701 init_llist_head(&gc->free_list);
704 int i915_gem_init_contexts(struct drm_i915_private *i915)
706 struct i915_gem_context *ctx;
708 /* Reassure ourselves we are only called once */
709 GEM_BUG_ON(i915->kernel_context);
711 init_contexts(&i915->gem.contexts);
713 /* lowest priority; idle task */
714 ctx = i915_gem_context_create_kernel(i915, I915_PRIORITY_MIN);
716 DRM_ERROR("Failed to create default global context\n");
719 i915->kernel_context = ctx;
721 DRM_DEBUG_DRIVER("%s context support initialized\n",
722 DRIVER_CAPS(i915)->has_logical_contexts ?
727 void i915_gem_driver_release__contexts(struct drm_i915_private *i915)
729 destroy_kernel_context(&i915->kernel_context);
730 flush_work(&i915->gem.contexts.free_work);
733 static int context_idr_cleanup(int id, void *p, void *data)
739 static int vm_idr_cleanup(int id, void *p, void *data)
745 static int gem_context_register(struct i915_gem_context *ctx,
746 struct drm_i915_file_private *fpriv)
748 struct i915_address_space *vm;
751 ctx->file_priv = fpriv;
753 mutex_lock(&ctx->mutex);
754 vm = i915_gem_context_vm(ctx);
756 WRITE_ONCE(vm->file, fpriv); /* XXX */
757 mutex_unlock(&ctx->mutex);
759 ctx->pid = get_task_pid(current, PIDTYPE_PID);
760 ctx->name = kasprintf(GFP_KERNEL, "%s[%d]",
761 current->comm, pid_nr(ctx->pid));
767 /* And finally expose ourselves to userspace via the idr */
768 mutex_lock(&fpriv->context_idr_lock);
769 ret = idr_alloc(&fpriv->context_idr, ctx, 0, 0, GFP_KERNEL);
770 mutex_unlock(&fpriv->context_idr_lock);
774 kfree(fetch_and_zero(&ctx->name));
776 put_pid(fetch_and_zero(&ctx->pid));
781 int i915_gem_context_open(struct drm_i915_private *i915,
782 struct drm_file *file)
784 struct drm_i915_file_private *file_priv = file->driver_priv;
785 struct i915_gem_context *ctx;
788 mutex_init(&file_priv->context_idr_lock);
789 mutex_init(&file_priv->vm_idr_lock);
791 idr_init(&file_priv->context_idr);
792 idr_init_base(&file_priv->vm_idr, 1);
794 ctx = i915_gem_create_context(i915, 0);
800 err = gem_context_register(ctx, file_priv);
804 GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
812 idr_destroy(&file_priv->vm_idr);
813 idr_destroy(&file_priv->context_idr);
814 mutex_destroy(&file_priv->vm_idr_lock);
815 mutex_destroy(&file_priv->context_idr_lock);
819 void i915_gem_context_close(struct drm_file *file)
821 struct drm_i915_file_private *file_priv = file->driver_priv;
822 struct drm_i915_private *i915 = file_priv->dev_priv;
824 idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
825 idr_destroy(&file_priv->context_idr);
826 mutex_destroy(&file_priv->context_idr_lock);
828 idr_for_each(&file_priv->vm_idr, vm_idr_cleanup, NULL);
829 idr_destroy(&file_priv->vm_idr);
830 mutex_destroy(&file_priv->vm_idr_lock);
832 contexts_flush_free(&i915->gem.contexts);
835 int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,
836 struct drm_file *file)
838 struct drm_i915_private *i915 = to_i915(dev);
839 struct drm_i915_gem_vm_control *args = data;
840 struct drm_i915_file_private *file_priv = file->driver_priv;
841 struct i915_ppgtt *ppgtt;
844 if (!HAS_FULL_PPGTT(i915))
850 ppgtt = i915_ppgtt_create(i915);
852 return PTR_ERR(ppgtt);
854 ppgtt->vm.file = file_priv;
856 if (args->extensions) {
857 err = i915_user_extensions(u64_to_user_ptr(args->extensions),
864 err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
868 err = idr_alloc(&file_priv->vm_idr, &ppgtt->vm, 0, 0, GFP_KERNEL);
872 GEM_BUG_ON(err == 0); /* reserved for invalid/unassigned ppgtt */
874 mutex_unlock(&file_priv->vm_idr_lock);
880 mutex_unlock(&file_priv->vm_idr_lock);
882 i915_vm_put(&ppgtt->vm);
886 int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,
887 struct drm_file *file)
889 struct drm_i915_file_private *file_priv = file->driver_priv;
890 struct drm_i915_gem_vm_control *args = data;
891 struct i915_address_space *vm;
898 if (args->extensions)
905 err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
909 vm = idr_remove(&file_priv->vm_idr, id);
911 mutex_unlock(&file_priv->vm_idr_lock);
919 struct context_barrier_task {
920 struct i915_active base;
921 void (*task)(void *data);
926 static void cb_retire(struct i915_active *base)
928 struct context_barrier_task *cb = container_of(base, typeof(*cb), base);
933 i915_active_fini(&cb->base);
937 I915_SELFTEST_DECLARE(static intel_engine_mask_t context_barrier_inject_fault);
938 static int context_barrier_task(struct i915_gem_context *ctx,
939 intel_engine_mask_t engines,
940 bool (*skip)(struct intel_context *ce, void *data),
941 int (*emit)(struct i915_request *rq, void *data),
942 void (*task)(void *data),
945 struct context_barrier_task *cb;
946 struct i915_gem_engines_iter it;
947 struct intel_context *ce;
952 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
956 i915_active_init(&cb->base, NULL, cb_retire);
957 err = i915_active_acquire(&cb->base);
963 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
964 struct i915_request *rq;
966 if (I915_SELFTEST_ONLY(context_barrier_inject_fault &
972 if (!(ce->engine->mask & engines))
975 if (skip && skip(ce, data))
978 rq = intel_context_create_request(ce);
986 err = emit(rq, data);
988 err = i915_active_add_request(&cb->base, rq);
990 i915_request_add(rq);
994 i915_gem_context_unlock_engines(ctx);
996 cb->task = err ? NULL : task; /* caller needs to unwind instead */
999 i915_active_release(&cb->base);
1004 static int get_ppgtt(struct drm_i915_file_private *file_priv,
1005 struct i915_gem_context *ctx,
1006 struct drm_i915_gem_context_param *args)
1008 struct i915_address_space *vm;
1011 if (!rcu_access_pointer(ctx->vm))
1015 vm = i915_vm_get(ctx->vm);
1018 ret = mutex_lock_interruptible(&file_priv->vm_idr_lock);
1022 ret = idr_alloc(&file_priv->vm_idr, vm, 0, 0, GFP_KERNEL);
1034 mutex_unlock(&file_priv->vm_idr_lock);
1040 static void set_ppgtt_barrier(void *data)
1042 struct i915_address_space *old = data;
1044 if (INTEL_GEN(old->i915) < 8)
1045 gen6_ppgtt_unpin_all(i915_vm_to_ppgtt(old));
1050 static int emit_ppgtt_update(struct i915_request *rq, void *data)
1052 struct i915_address_space *vm = rq->hw_context->vm;
1053 struct intel_engine_cs *engine = rq->engine;
1054 u32 base = engine->mmio_base;
1058 if (i915_vm_is_4lvl(vm)) {
1059 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1060 const dma_addr_t pd_daddr = px_dma(ppgtt->pd);
1062 cs = intel_ring_begin(rq, 6);
1066 *cs++ = MI_LOAD_REGISTER_IMM(2);
1068 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, 0));
1069 *cs++ = upper_32_bits(pd_daddr);
1070 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, 0));
1071 *cs++ = lower_32_bits(pd_daddr);
1074 intel_ring_advance(rq, cs);
1075 } else if (HAS_LOGICAL_RING_CONTEXTS(engine->i915)) {
1076 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1079 /* Magic required to prevent forcewake errors! */
1080 err = engine->emit_flush(rq, EMIT_INVALIDATE);
1084 cs = intel_ring_begin(rq, 4 * GEN8_3LVL_PDPES + 2);
1088 *cs++ = MI_LOAD_REGISTER_IMM(2 * GEN8_3LVL_PDPES) | MI_LRI_FORCE_POSTED;
1089 for (i = GEN8_3LVL_PDPES; i--; ) {
1090 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1092 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, i));
1093 *cs++ = upper_32_bits(pd_daddr);
1094 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, i));
1095 *cs++ = lower_32_bits(pd_daddr);
1098 intel_ring_advance(rq, cs);
1100 /* ppGTT is not part of the legacy context image */
1101 gen6_ppgtt_pin(i915_vm_to_ppgtt(vm));
1107 static bool skip_ppgtt_update(struct intel_context *ce, void *data)
1109 if (HAS_LOGICAL_RING_CONTEXTS(ce->engine->i915))
1112 return !atomic_read(&ce->pin_count);
1115 static int set_ppgtt(struct drm_i915_file_private *file_priv,
1116 struct i915_gem_context *ctx,
1117 struct drm_i915_gem_context_param *args)
1119 struct i915_address_space *vm, *old;
1125 if (!rcu_access_pointer(ctx->vm))
1128 if (upper_32_bits(args->value))
1132 vm = idr_find(&file_priv->vm_idr, args->value);
1133 if (vm && !kref_get_unless_zero(&vm->ref))
1139 err = mutex_lock_interruptible(&ctx->mutex);
1143 if (i915_gem_context_is_closed(ctx)) {
1148 if (vm == rcu_access_pointer(ctx->vm))
1151 /* Teardown the existing obj:vma cache, it will have to be rebuilt. */
1154 old = __set_ppgtt(ctx, vm);
1157 * We need to flush any requests using the current ppgtt before
1158 * we release it as the requests do not hold a reference themselves,
1159 * only indirectly through the context.
1161 err = context_barrier_task(ctx, ALL_ENGINES,
1167 i915_vm_close(__set_ppgtt(ctx, old));
1172 mutex_unlock(&ctx->mutex);
1178 static int gen8_emit_rpcs_config(struct i915_request *rq,
1179 struct intel_context *ce,
1180 struct intel_sseu sseu)
1185 cs = intel_ring_begin(rq, 4);
1189 offset = i915_ggtt_offset(ce->state) +
1190 LRC_STATE_PN * PAGE_SIZE +
1191 CTX_R_PWR_CLK_STATE * 4;
1193 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1194 *cs++ = lower_32_bits(offset);
1195 *cs++ = upper_32_bits(offset);
1196 *cs++ = intel_sseu_make_rpcs(rq->i915, &sseu);
1198 intel_ring_advance(rq, cs);
1204 gen8_modify_rpcs(struct intel_context *ce, struct intel_sseu sseu)
1206 struct i915_request *rq;
1209 lockdep_assert_held(&ce->pin_mutex);
1212 * If the context is not idle, we have to submit an ordered request to
1213 * modify its context image via the kernel context (writing to our own
1214 * image, or into the registers directory, does not stick). Pristine
1215 * and idle contexts will be configured on pinning.
1217 if (!intel_context_is_pinned(ce))
1220 rq = i915_request_create(ce->engine->kernel_context);
1224 /* Serialise with the remote context */
1225 ret = intel_context_prepare_remote_request(ce, rq);
1227 ret = gen8_emit_rpcs_config(rq, ce, sseu);
1229 i915_request_add(rq);
1234 intel_context_reconfigure_sseu(struct intel_context *ce, struct intel_sseu sseu)
1238 GEM_BUG_ON(INTEL_GEN(ce->engine->i915) < 8);
1240 ret = intel_context_lock_pinned(ce);
1244 /* Nothing to do if unmodified. */
1245 if (!memcmp(&ce->sseu, &sseu, sizeof(sseu)))
1248 ret = gen8_modify_rpcs(ce, sseu);
1253 intel_context_unlock_pinned(ce);
1258 user_to_context_sseu(struct drm_i915_private *i915,
1259 const struct drm_i915_gem_context_param_sseu *user,
1260 struct intel_sseu *context)
1262 const struct sseu_dev_info *device = &RUNTIME_INFO(i915)->sseu;
1264 /* No zeros in any field. */
1265 if (!user->slice_mask || !user->subslice_mask ||
1266 !user->min_eus_per_subslice || !user->max_eus_per_subslice)
1270 if (user->max_eus_per_subslice < user->min_eus_per_subslice)
1274 * Some future proofing on the types since the uAPI is wider than the
1275 * current internal implementation.
1277 if (overflows_type(user->slice_mask, context->slice_mask) ||
1278 overflows_type(user->subslice_mask, context->subslice_mask) ||
1279 overflows_type(user->min_eus_per_subslice,
1280 context->min_eus_per_subslice) ||
1281 overflows_type(user->max_eus_per_subslice,
1282 context->max_eus_per_subslice))
1285 /* Check validity against hardware. */
1286 if (user->slice_mask & ~device->slice_mask)
1289 if (user->subslice_mask & ~device->subslice_mask[0])
1292 if (user->max_eus_per_subslice > device->max_eus_per_subslice)
1295 context->slice_mask = user->slice_mask;
1296 context->subslice_mask = user->subslice_mask;
1297 context->min_eus_per_subslice = user->min_eus_per_subslice;
1298 context->max_eus_per_subslice = user->max_eus_per_subslice;
1300 /* Part specific restrictions. */
1301 if (IS_GEN(i915, 11)) {
1302 unsigned int hw_s = hweight8(device->slice_mask);
1303 unsigned int hw_ss_per_s = hweight8(device->subslice_mask[0]);
1304 unsigned int req_s = hweight8(context->slice_mask);
1305 unsigned int req_ss = hweight8(context->subslice_mask);
1308 * Only full subslice enablement is possible if more than one
1309 * slice is turned on.
1311 if (req_s > 1 && req_ss != hw_ss_per_s)
1315 * If more than four (SScount bitfield limit) subslices are
1316 * requested then the number has to be even.
1318 if (req_ss > 4 && (req_ss & 1))
1322 * If only one slice is enabled and subslice count is below the
1323 * device full enablement, it must be at most half of the all
1324 * available subslices.
1326 if (req_s == 1 && req_ss < hw_ss_per_s &&
1327 req_ss > (hw_ss_per_s / 2))
1330 /* ABI restriction - VME use case only. */
1332 /* All slices or one slice only. */
1333 if (req_s != 1 && req_s != hw_s)
1337 * Half subslices or full enablement only when one slice is
1341 (req_ss != hw_ss_per_s && req_ss != (hw_ss_per_s / 2)))
1344 /* No EU configuration changes. */
1345 if ((user->min_eus_per_subslice !=
1346 device->max_eus_per_subslice) ||
1347 (user->max_eus_per_subslice !=
1348 device->max_eus_per_subslice))
1355 static int set_sseu(struct i915_gem_context *ctx,
1356 struct drm_i915_gem_context_param *args)
1358 struct drm_i915_private *i915 = ctx->i915;
1359 struct drm_i915_gem_context_param_sseu user_sseu;
1360 struct intel_context *ce;
1361 struct intel_sseu sseu;
1362 unsigned long lookup;
1365 if (args->size < sizeof(user_sseu))
1368 if (!IS_GEN(i915, 11))
1371 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
1378 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
1382 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
1383 lookup |= LOOKUP_USER_INDEX;
1385 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
1389 /* Only render engine supports RPCS configuration. */
1390 if (ce->engine->class != RENDER_CLASS) {
1395 ret = user_to_context_sseu(i915, &user_sseu, &sseu);
1399 ret = intel_context_reconfigure_sseu(ce, sseu);
1403 args->size = sizeof(user_sseu);
1406 intel_context_put(ce);
1410 struct set_engines {
1411 struct i915_gem_context *ctx;
1412 struct i915_gem_engines *engines;
1416 set_engines__load_balance(struct i915_user_extension __user *base, void *data)
1418 struct i915_context_engines_load_balance __user *ext =
1419 container_of_user(base, typeof(*ext), base);
1420 const struct set_engines *set = data;
1421 struct intel_engine_cs *stack[16];
1422 struct intel_engine_cs **siblings;
1423 struct intel_context *ce;
1424 u16 num_siblings, idx;
1428 if (!HAS_EXECLISTS(set->ctx->i915))
1431 if (USES_GUC_SUBMISSION(set->ctx->i915))
1432 return -ENODEV; /* not implement yet */
1434 if (get_user(idx, &ext->engine_index))
1437 if (idx >= set->engines->num_engines) {
1438 DRM_DEBUG("Invalid placement value, %d >= %d\n",
1439 idx, set->engines->num_engines);
1443 idx = array_index_nospec(idx, set->engines->num_engines);
1444 if (set->engines->engines[idx]) {
1445 DRM_DEBUG("Invalid placement[%d], already occupied\n", idx);
1449 if (get_user(num_siblings, &ext->num_siblings))
1452 err = check_user_mbz(&ext->flags);
1456 err = check_user_mbz(&ext->mbz64);
1461 if (num_siblings > ARRAY_SIZE(stack)) {
1462 siblings = kmalloc_array(num_siblings,
1469 for (n = 0; n < num_siblings; n++) {
1470 struct i915_engine_class_instance ci;
1472 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) {
1477 siblings[n] = intel_engine_lookup_user(set->ctx->i915,
1479 ci.engine_instance);
1481 DRM_DEBUG("Invalid sibling[%d]: { class:%d, inst:%d }\n",
1482 n, ci.engine_class, ci.engine_instance);
1488 ce = intel_execlists_create_virtual(set->ctx, siblings, n);
1494 if (cmpxchg(&set->engines->engines[idx], NULL, ce)) {
1495 intel_context_put(ce);
1501 if (siblings != stack)
1508 set_engines__bond(struct i915_user_extension __user *base, void *data)
1510 struct i915_context_engines_bond __user *ext =
1511 container_of_user(base, typeof(*ext), base);
1512 const struct set_engines *set = data;
1513 struct i915_engine_class_instance ci;
1514 struct intel_engine_cs *virtual;
1515 struct intel_engine_cs *master;
1519 if (get_user(idx, &ext->virtual_index))
1522 if (idx >= set->engines->num_engines) {
1523 DRM_DEBUG("Invalid index for virtual engine: %d >= %d\n",
1524 idx, set->engines->num_engines);
1528 idx = array_index_nospec(idx, set->engines->num_engines);
1529 if (!set->engines->engines[idx]) {
1530 DRM_DEBUG("Invalid engine at %d\n", idx);
1533 virtual = set->engines->engines[idx]->engine;
1535 err = check_user_mbz(&ext->flags);
1539 for (n = 0; n < ARRAY_SIZE(ext->mbz64); n++) {
1540 err = check_user_mbz(&ext->mbz64[n]);
1545 if (copy_from_user(&ci, &ext->master, sizeof(ci)))
1548 master = intel_engine_lookup_user(set->ctx->i915,
1549 ci.engine_class, ci.engine_instance);
1551 DRM_DEBUG("Unrecognised master engine: { class:%u, instance:%u }\n",
1552 ci.engine_class, ci.engine_instance);
1556 if (get_user(num_bonds, &ext->num_bonds))
1559 for (n = 0; n < num_bonds; n++) {
1560 struct intel_engine_cs *bond;
1562 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci)))
1565 bond = intel_engine_lookup_user(set->ctx->i915,
1567 ci.engine_instance);
1569 DRM_DEBUG("Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n",
1570 n, ci.engine_class, ci.engine_instance);
1575 * A non-virtual engine has no siblings to choose between; and
1576 * a submit fence will always be directed to the one engine.
1578 if (intel_engine_is_virtual(virtual)) {
1579 err = intel_virtual_engine_attach_bond(virtual,
1590 static const i915_user_extension_fn set_engines__extensions[] = {
1591 [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE] = set_engines__load_balance,
1592 [I915_CONTEXT_ENGINES_EXT_BOND] = set_engines__bond,
1596 set_engines(struct i915_gem_context *ctx,
1597 const struct drm_i915_gem_context_param *args)
1599 struct i915_context_param_engines __user *user =
1600 u64_to_user_ptr(args->value);
1601 struct set_engines set = { .ctx = ctx };
1602 unsigned int num_engines, n;
1606 if (!args->size) { /* switch back to legacy user_ring_map */
1607 if (!i915_gem_context_user_engines(ctx))
1610 set.engines = default_engines(ctx);
1611 if (IS_ERR(set.engines))
1612 return PTR_ERR(set.engines);
1617 BUILD_BUG_ON(!IS_ALIGNED(sizeof(*user), sizeof(*user->engines)));
1618 if (args->size < sizeof(*user) ||
1619 !IS_ALIGNED(args->size, sizeof(*user->engines))) {
1620 DRM_DEBUG("Invalid size for engine array: %d\n",
1626 * Note that I915_EXEC_RING_MASK limits execbuf to only using the
1627 * first 64 engines defined here.
1629 num_engines = (args->size - sizeof(*user)) / sizeof(*user->engines);
1631 set.engines = kmalloc(struct_size(set.engines, engines, num_engines),
1636 init_rcu_head(&set.engines->rcu);
1637 for (n = 0; n < num_engines; n++) {
1638 struct i915_engine_class_instance ci;
1639 struct intel_engine_cs *engine;
1640 struct intel_context *ce;
1642 if (copy_from_user(&ci, &user->engines[n], sizeof(ci))) {
1643 __free_engines(set.engines, n);
1647 if (ci.engine_class == (u16)I915_ENGINE_CLASS_INVALID &&
1648 ci.engine_instance == (u16)I915_ENGINE_CLASS_INVALID_NONE) {
1649 set.engines->engines[n] = NULL;
1653 engine = intel_engine_lookup_user(ctx->i915,
1655 ci.engine_instance);
1657 DRM_DEBUG("Invalid engine[%d]: { class:%d, instance:%d }\n",
1658 n, ci.engine_class, ci.engine_instance);
1659 __free_engines(set.engines, n);
1663 ce = intel_context_create(ctx, engine);
1665 __free_engines(set.engines, n);
1669 set.engines->engines[n] = ce;
1671 set.engines->num_engines = num_engines;
1674 if (!get_user(extensions, &user->extensions))
1675 err = i915_user_extensions(u64_to_user_ptr(extensions),
1676 set_engines__extensions,
1677 ARRAY_SIZE(set_engines__extensions),
1680 free_engines(set.engines);
1685 mutex_lock(&ctx->engines_mutex);
1687 i915_gem_context_set_user_engines(ctx);
1689 i915_gem_context_clear_user_engines(ctx);
1690 set.engines = rcu_replace_pointer(ctx->engines, set.engines, 1);
1691 mutex_unlock(&ctx->engines_mutex);
1693 call_rcu(&set.engines->rcu, free_engines_rcu);
1698 static struct i915_gem_engines *
1699 __copy_engines(struct i915_gem_engines *e)
1701 struct i915_gem_engines *copy;
1704 copy = kmalloc(struct_size(e, engines, e->num_engines), GFP_KERNEL);
1706 return ERR_PTR(-ENOMEM);
1708 init_rcu_head(©->rcu);
1709 for (n = 0; n < e->num_engines; n++) {
1711 copy->engines[n] = intel_context_get(e->engines[n]);
1713 copy->engines[n] = NULL;
1715 copy->num_engines = n;
1721 get_engines(struct i915_gem_context *ctx,
1722 struct drm_i915_gem_context_param *args)
1724 struct i915_context_param_engines __user *user;
1725 struct i915_gem_engines *e;
1726 size_t n, count, size;
1729 err = mutex_lock_interruptible(&ctx->engines_mutex);
1734 if (i915_gem_context_user_engines(ctx))
1735 e = __copy_engines(i915_gem_context_engines(ctx));
1736 mutex_unlock(&ctx->engines_mutex);
1737 if (IS_ERR_OR_NULL(e)) {
1739 return PTR_ERR_OR_ZERO(e);
1742 count = e->num_engines;
1744 /* Be paranoid in case we have an impedance mismatch */
1745 if (!check_struct_size(user, engines, count, &size)) {
1749 if (overflows_type(size, args->size)) {
1759 if (args->size < size) {
1764 user = u64_to_user_ptr(args->value);
1765 if (!access_ok(user, size)) {
1770 if (put_user(0, &user->extensions)) {
1775 for (n = 0; n < count; n++) {
1776 struct i915_engine_class_instance ci = {
1777 .engine_class = I915_ENGINE_CLASS_INVALID,
1778 .engine_instance = I915_ENGINE_CLASS_INVALID_NONE,
1781 if (e->engines[n]) {
1782 ci.engine_class = e->engines[n]->engine->uabi_class;
1783 ci.engine_instance = e->engines[n]->engine->uabi_instance;
1786 if (copy_to_user(&user->engines[n], &ci, sizeof(ci))) {
1800 set_persistence(struct i915_gem_context *ctx,
1801 const struct drm_i915_gem_context_param *args)
1806 return __context_set_persistence(ctx, args->value);
1809 static int ctx_setparam(struct drm_i915_file_private *fpriv,
1810 struct i915_gem_context *ctx,
1811 struct drm_i915_gem_context_param *args)
1815 switch (args->param) {
1816 case I915_CONTEXT_PARAM_NO_ZEROMAP:
1819 else if (args->value)
1820 set_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
1822 clear_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
1825 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
1828 else if (args->value)
1829 i915_gem_context_set_no_error_capture(ctx);
1831 i915_gem_context_clear_no_error_capture(ctx);
1834 case I915_CONTEXT_PARAM_BANNABLE:
1837 else if (!capable(CAP_SYS_ADMIN) && !args->value)
1839 else if (args->value)
1840 i915_gem_context_set_bannable(ctx);
1842 i915_gem_context_clear_bannable(ctx);
1845 case I915_CONTEXT_PARAM_RECOVERABLE:
1848 else if (args->value)
1849 i915_gem_context_set_recoverable(ctx);
1851 i915_gem_context_clear_recoverable(ctx);
1854 case I915_CONTEXT_PARAM_PRIORITY:
1856 s64 priority = args->value;
1860 else if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PRIORITY))
1862 else if (priority > I915_CONTEXT_MAX_USER_PRIORITY ||
1863 priority < I915_CONTEXT_MIN_USER_PRIORITY)
1865 else if (priority > I915_CONTEXT_DEFAULT_PRIORITY &&
1866 !capable(CAP_SYS_NICE))
1869 ctx->sched.priority =
1870 I915_USER_PRIORITY(priority);
1874 case I915_CONTEXT_PARAM_SSEU:
1875 ret = set_sseu(ctx, args);
1878 case I915_CONTEXT_PARAM_VM:
1879 ret = set_ppgtt(fpriv, ctx, args);
1882 case I915_CONTEXT_PARAM_ENGINES:
1883 ret = set_engines(ctx, args);
1886 case I915_CONTEXT_PARAM_PERSISTENCE:
1887 ret = set_persistence(ctx, args);
1890 case I915_CONTEXT_PARAM_BAN_PERIOD:
1900 struct i915_gem_context *ctx;
1901 struct drm_i915_file_private *fpriv;
1904 static int create_setparam(struct i915_user_extension __user *ext, void *data)
1906 struct drm_i915_gem_context_create_ext_setparam local;
1907 const struct create_ext *arg = data;
1909 if (copy_from_user(&local, ext, sizeof(local)))
1912 if (local.param.ctx_id)
1915 return ctx_setparam(arg->fpriv, arg->ctx, &local.param);
1918 static int clone_engines(struct i915_gem_context *dst,
1919 struct i915_gem_context *src)
1921 struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1922 struct i915_gem_engines *clone;
1926 clone = kmalloc(struct_size(e, engines, e->num_engines), GFP_KERNEL);
1930 init_rcu_head(&clone->rcu);
1931 for (n = 0; n < e->num_engines; n++) {
1932 struct intel_engine_cs *engine;
1934 if (!e->engines[n]) {
1935 clone->engines[n] = NULL;
1938 engine = e->engines[n]->engine;
1941 * Virtual engines are singletons; they can only exist
1942 * inside a single context, because they embed their
1943 * HW context... As each virtual context implies a single
1944 * timeline (each engine can only dequeue a single request
1945 * at any time), it would be surprising for two contexts
1946 * to use the same engine. So let's create a copy of
1947 * the virtual engine instead.
1949 if (intel_engine_is_virtual(engine))
1951 intel_execlists_clone_virtual(dst, engine);
1953 clone->engines[n] = intel_context_create(dst, engine);
1954 if (IS_ERR_OR_NULL(clone->engines[n])) {
1955 __free_engines(clone, n);
1959 clone->num_engines = n;
1961 user_engines = i915_gem_context_user_engines(src);
1962 i915_gem_context_unlock_engines(src);
1964 free_engines(dst->engines);
1965 RCU_INIT_POINTER(dst->engines, clone);
1967 i915_gem_context_set_user_engines(dst);
1969 i915_gem_context_clear_user_engines(dst);
1973 i915_gem_context_unlock_engines(src);
1977 static int clone_flags(struct i915_gem_context *dst,
1978 struct i915_gem_context *src)
1980 dst->user_flags = src->user_flags;
1984 static int clone_schedattr(struct i915_gem_context *dst,
1985 struct i915_gem_context *src)
1987 dst->sched = src->sched;
1991 static int clone_sseu(struct i915_gem_context *dst,
1992 struct i915_gem_context *src)
1994 struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1995 struct i915_gem_engines *clone;
1999 clone = dst->engines; /* no locking required; sole access */
2000 if (e->num_engines != clone->num_engines) {
2005 for (n = 0; n < e->num_engines; n++) {
2006 struct intel_context *ce = e->engines[n];
2008 if (clone->engines[n]->engine->class != ce->engine->class) {
2009 /* Must have compatible engine maps! */
2014 /* serialises with set_sseu */
2015 err = intel_context_lock_pinned(ce);
2019 clone->engines[n]->sseu = ce->sseu;
2020 intel_context_unlock_pinned(ce);
2025 i915_gem_context_unlock_engines(src);
2029 static int clone_timeline(struct i915_gem_context *dst,
2030 struct i915_gem_context *src)
2033 __assign_timeline(dst, src->timeline);
2038 static int clone_vm(struct i915_gem_context *dst,
2039 struct i915_gem_context *src)
2041 struct i915_address_space *vm;
2046 vm = rcu_dereference(src->vm);
2050 if (!kref_get_unless_zero(&vm->ref))
2054 * This ppgtt may have be reallocated between
2055 * the read and the kref, and reassigned to a third
2056 * context. In order to avoid inadvertent sharing
2057 * of this ppgtt with that third context (and not
2058 * src), we have to confirm that we have the same
2059 * ppgtt after passing through the strong memory
2060 * barrier implied by a successful
2061 * kref_get_unless_zero().
2063 * Once we have acquired the current ppgtt of src,
2064 * we no longer care if it is released from src, as
2065 * it cannot be reallocated elsewhere.
2068 if (vm == rcu_access_pointer(src->vm))
2076 if (!mutex_lock_interruptible(&dst->mutex)) {
2077 __assign_ppgtt(dst, vm);
2078 mutex_unlock(&dst->mutex);
2088 static int create_clone(struct i915_user_extension __user *ext, void *data)
2090 static int (* const fn[])(struct i915_gem_context *dst,
2091 struct i915_gem_context *src) = {
2092 #define MAP(x, y) [ilog2(I915_CONTEXT_CLONE_##x)] = y
2093 MAP(ENGINES, clone_engines),
2094 MAP(FLAGS, clone_flags),
2095 MAP(SCHEDATTR, clone_schedattr),
2096 MAP(SSEU, clone_sseu),
2097 MAP(TIMELINE, clone_timeline),
2101 struct drm_i915_gem_context_create_ext_clone local;
2102 const struct create_ext *arg = data;
2103 struct i915_gem_context *dst = arg->ctx;
2104 struct i915_gem_context *src;
2107 if (copy_from_user(&local, ext, sizeof(local)))
2110 BUILD_BUG_ON(GENMASK(BITS_PER_TYPE(local.flags) - 1, ARRAY_SIZE(fn)) !=
2111 I915_CONTEXT_CLONE_UNKNOWN);
2113 if (local.flags & I915_CONTEXT_CLONE_UNKNOWN)
2120 src = __i915_gem_context_lookup_rcu(arg->fpriv, local.clone_id);
2125 GEM_BUG_ON(src == dst);
2127 for (bit = 0; bit < ARRAY_SIZE(fn); bit++) {
2128 if (!(local.flags & BIT(bit)))
2131 err = fn[bit](dst, src);
2139 static const i915_user_extension_fn create_extensions[] = {
2140 [I915_CONTEXT_CREATE_EXT_SETPARAM] = create_setparam,
2141 [I915_CONTEXT_CREATE_EXT_CLONE] = create_clone,
2144 static bool client_is_banned(struct drm_i915_file_private *file_priv)
2146 return atomic_read(&file_priv->ban_score) >= I915_CLIENT_SCORE_BANNED;
2149 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
2150 struct drm_file *file)
2152 struct drm_i915_private *i915 = to_i915(dev);
2153 struct drm_i915_gem_context_create_ext *args = data;
2154 struct create_ext ext_data;
2157 if (!DRIVER_CAPS(i915)->has_logical_contexts)
2160 if (args->flags & I915_CONTEXT_CREATE_FLAGS_UNKNOWN)
2163 ret = intel_gt_terminally_wedged(&i915->gt);
2167 ext_data.fpriv = file->driver_priv;
2168 if (client_is_banned(ext_data.fpriv)) {
2169 DRM_DEBUG("client %s[%d] banned from creating ctx\n",
2171 pid_nr(get_task_pid(current, PIDTYPE_PID)));
2175 ext_data.ctx = i915_gem_create_context(i915, args->flags);
2176 if (IS_ERR(ext_data.ctx))
2177 return PTR_ERR(ext_data.ctx);
2179 if (args->flags & I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS) {
2180 ret = i915_user_extensions(u64_to_user_ptr(args->extensions),
2182 ARRAY_SIZE(create_extensions),
2188 ret = gem_context_register(ext_data.ctx, ext_data.fpriv);
2193 DRM_DEBUG("HW context %d created\n", args->ctx_id);
2198 context_close(ext_data.ctx);
2202 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
2203 struct drm_file *file)
2205 struct drm_i915_gem_context_destroy *args = data;
2206 struct drm_i915_file_private *file_priv = file->driver_priv;
2207 struct i915_gem_context *ctx;
2215 if (mutex_lock_interruptible(&file_priv->context_idr_lock))
2218 ctx = idr_remove(&file_priv->context_idr, args->ctx_id);
2219 mutex_unlock(&file_priv->context_idr_lock);
2227 static int get_sseu(struct i915_gem_context *ctx,
2228 struct drm_i915_gem_context_param *args)
2230 struct drm_i915_gem_context_param_sseu user_sseu;
2231 struct intel_context *ce;
2232 unsigned long lookup;
2235 if (args->size == 0)
2237 else if (args->size < sizeof(user_sseu))
2240 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
2247 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
2251 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
2252 lookup |= LOOKUP_USER_INDEX;
2254 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
2258 err = intel_context_lock_pinned(ce); /* serialises with set_sseu */
2260 intel_context_put(ce);
2264 user_sseu.slice_mask = ce->sseu.slice_mask;
2265 user_sseu.subslice_mask = ce->sseu.subslice_mask;
2266 user_sseu.min_eus_per_subslice = ce->sseu.min_eus_per_subslice;
2267 user_sseu.max_eus_per_subslice = ce->sseu.max_eus_per_subslice;
2269 intel_context_unlock_pinned(ce);
2270 intel_context_put(ce);
2272 if (copy_to_user(u64_to_user_ptr(args->value), &user_sseu,
2277 args->size = sizeof(user_sseu);
2282 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
2283 struct drm_file *file)
2285 struct drm_i915_file_private *file_priv = file->driver_priv;
2286 struct drm_i915_gem_context_param *args = data;
2287 struct i915_gem_context *ctx;
2290 ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2294 switch (args->param) {
2295 case I915_CONTEXT_PARAM_NO_ZEROMAP:
2297 args->value = test_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
2300 case I915_CONTEXT_PARAM_GTT_SIZE:
2303 if (rcu_access_pointer(ctx->vm))
2304 args->value = rcu_dereference(ctx->vm)->total;
2306 args->value = to_i915(dev)->ggtt.vm.total;
2310 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
2312 args->value = i915_gem_context_no_error_capture(ctx);
2315 case I915_CONTEXT_PARAM_BANNABLE:
2317 args->value = i915_gem_context_is_bannable(ctx);
2320 case I915_CONTEXT_PARAM_RECOVERABLE:
2322 args->value = i915_gem_context_is_recoverable(ctx);
2325 case I915_CONTEXT_PARAM_PRIORITY:
2327 args->value = ctx->sched.priority >> I915_USER_PRIORITY_SHIFT;
2330 case I915_CONTEXT_PARAM_SSEU:
2331 ret = get_sseu(ctx, args);
2334 case I915_CONTEXT_PARAM_VM:
2335 ret = get_ppgtt(file_priv, ctx, args);
2338 case I915_CONTEXT_PARAM_ENGINES:
2339 ret = get_engines(ctx, args);
2342 case I915_CONTEXT_PARAM_PERSISTENCE:
2344 args->value = i915_gem_context_is_persistent(ctx);
2347 case I915_CONTEXT_PARAM_BAN_PERIOD:
2353 i915_gem_context_put(ctx);
2357 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
2358 struct drm_file *file)
2360 struct drm_i915_file_private *file_priv = file->driver_priv;
2361 struct drm_i915_gem_context_param *args = data;
2362 struct i915_gem_context *ctx;
2365 ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2369 ret = ctx_setparam(file_priv, ctx, args);
2371 i915_gem_context_put(ctx);
2375 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
2376 void *data, struct drm_file *file)
2378 struct drm_i915_private *i915 = to_i915(dev);
2379 struct drm_i915_reset_stats *args = data;
2380 struct i915_gem_context *ctx;
2383 if (args->flags || args->pad)
2388 ctx = __i915_gem_context_lookup_rcu(file->driver_priv, args->ctx_id);
2393 * We opt for unserialised reads here. This may result in tearing
2394 * in the extremely unlikely event of a GPU hang on this context
2395 * as we are querying them. If we need that extra layer of protection,
2396 * we should wrap the hangstats with a seqlock.
2399 if (capable(CAP_SYS_ADMIN))
2400 args->reset_count = i915_reset_count(&i915->gpu_error);
2402 args->reset_count = 0;
2404 args->batch_active = atomic_read(&ctx->guilty_count);
2405 args->batch_pending = atomic_read(&ctx->active_count);
2413 /* GEM context-engines iterator: for_each_gem_engine() */
2414 struct intel_context *
2415 i915_gem_engines_iter_next(struct i915_gem_engines_iter *it)
2417 const struct i915_gem_engines *e = it->engines;
2418 struct intel_context *ctx;
2421 if (it->idx >= e->num_engines)
2424 ctx = e->engines[it->idx++];
2430 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2431 #include "selftests/mock_context.c"
2432 #include "selftests/i915_gem_context.c"
2435 static void i915_global_gem_context_shrink(void)
2437 kmem_cache_shrink(global.slab_luts);
2440 static void i915_global_gem_context_exit(void)
2442 kmem_cache_destroy(global.slab_luts);
2445 static struct i915_global_gem_context global = { {
2446 .shrink = i915_global_gem_context_shrink,
2447 .exit = i915_global_gem_context_exit,
2450 int __init i915_global_gem_context_init(void)
2452 global.slab_luts = KMEM_CACHE(i915_lut_handle, 0);
2453 if (!global.slab_luts)
2456 i915_global_register(&global.base);