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 "gt/gen6_ppgtt.h"
71 #include "gt/intel_context.h"
72 #include "gt/intel_context_param.h"
73 #include "gt/intel_engine_heartbeat.h"
74 #include "gt/intel_engine_user.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);
134 i915_gem_object_put(obj);
137 i915_gem_object_put(obj);
142 static struct intel_context *
143 lookup_user_engine(struct i915_gem_context *ctx,
145 const struct i915_engine_class_instance *ci)
146 #define LOOKUP_USER_INDEX BIT(0)
150 if (!!(flags & LOOKUP_USER_INDEX) != i915_gem_context_user_engines(ctx))
151 return ERR_PTR(-EINVAL);
153 if (!i915_gem_context_user_engines(ctx)) {
154 struct intel_engine_cs *engine;
156 engine = intel_engine_lookup_user(ctx->i915,
158 ci->engine_instance);
160 return ERR_PTR(-EINVAL);
162 idx = engine->legacy_idx;
164 idx = ci->engine_instance;
167 return i915_gem_context_get_engine(ctx, idx);
170 static struct i915_address_space *
171 context_get_vm_rcu(struct i915_gem_context *ctx)
173 GEM_BUG_ON(!rcu_access_pointer(ctx->vm));
176 struct i915_address_space *vm;
179 * We do not allow downgrading from full-ppgtt [to a shared
180 * global gtt], so ctx->vm cannot become NULL.
182 vm = rcu_dereference(ctx->vm);
183 if (!kref_get_unless_zero(&vm->ref))
187 * This ppgtt may have be reallocated between
188 * the read and the kref, and reassigned to a third
189 * context. In order to avoid inadvertent sharing
190 * of this ppgtt with that third context (and not
191 * src), we have to confirm that we have the same
192 * ppgtt after passing through the strong memory
193 * barrier implied by a successful
194 * kref_get_unless_zero().
196 * Once we have acquired the current ppgtt of ctx,
197 * we no longer care if it is released from ctx, as
198 * it cannot be reallocated elsewhere.
201 if (vm == rcu_access_pointer(ctx->vm))
202 return rcu_pointer_handoff(vm);
208 static void intel_context_set_gem(struct intel_context *ce,
209 struct i915_gem_context *ctx)
211 GEM_BUG_ON(rcu_access_pointer(ce->gem_context));
212 RCU_INIT_POINTER(ce->gem_context, ctx);
214 if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags))
215 ce->ring = __intel_context_ring_size(SZ_16K);
217 if (rcu_access_pointer(ctx->vm)) {
218 struct i915_address_space *vm;
221 vm = context_get_vm_rcu(ctx); /* hmm */
228 GEM_BUG_ON(ce->timeline);
230 ce->timeline = intel_timeline_get(ctx->timeline);
232 if (ctx->sched.priority >= I915_PRIORITY_NORMAL &&
233 intel_engine_has_timeslices(ce->engine))
234 __set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
237 static void __free_engines(struct i915_gem_engines *e, unsigned int count)
240 if (!e->engines[count])
243 intel_context_put(e->engines[count]);
248 static void free_engines(struct i915_gem_engines *e)
250 __free_engines(e, e->num_engines);
253 static void free_engines_rcu(struct rcu_head *rcu)
255 struct i915_gem_engines *engines =
256 container_of(rcu, struct i915_gem_engines, rcu);
258 i915_sw_fence_fini(&engines->fence);
259 free_engines(engines);
262 static int __i915_sw_fence_call
263 engines_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
265 struct i915_gem_engines *engines =
266 container_of(fence, typeof(*engines), fence);
270 if (!list_empty(&engines->link)) {
271 struct i915_gem_context *ctx = engines->ctx;
274 spin_lock_irqsave(&ctx->stale.lock, flags);
275 list_del(&engines->link);
276 spin_unlock_irqrestore(&ctx->stale.lock, flags);
278 i915_gem_context_put(engines->ctx);
282 init_rcu_head(&engines->rcu);
283 call_rcu(&engines->rcu, free_engines_rcu);
290 static struct i915_gem_engines *alloc_engines(unsigned int count)
292 struct i915_gem_engines *e;
294 e = kzalloc(struct_size(e, engines, count), GFP_KERNEL);
298 i915_sw_fence_init(&e->fence, engines_notify);
302 static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx)
304 const struct intel_gt *gt = &ctx->i915->gt;
305 struct intel_engine_cs *engine;
306 struct i915_gem_engines *e;
307 enum intel_engine_id id;
309 e = alloc_engines(I915_NUM_ENGINES);
311 return ERR_PTR(-ENOMEM);
313 for_each_engine(engine, gt, id) {
314 struct intel_context *ce;
316 if (engine->legacy_idx == INVALID_ENGINE)
319 GEM_BUG_ON(engine->legacy_idx >= I915_NUM_ENGINES);
320 GEM_BUG_ON(e->engines[engine->legacy_idx]);
322 ce = intel_context_create(engine);
324 __free_engines(e, e->num_engines + 1);
328 intel_context_set_gem(ce, ctx);
330 e->engines[engine->legacy_idx] = ce;
331 e->num_engines = max(e->num_engines, engine->legacy_idx);
338 static void i915_gem_context_free(struct i915_gem_context *ctx)
340 GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
342 spin_lock(&ctx->i915->gem.contexts.lock);
343 list_del(&ctx->link);
344 spin_unlock(&ctx->i915->gem.contexts.lock);
346 mutex_destroy(&ctx->engines_mutex);
349 intel_timeline_put(ctx->timeline);
352 mutex_destroy(&ctx->mutex);
357 static void contexts_free_all(struct llist_node *list)
359 struct i915_gem_context *ctx, *cn;
361 llist_for_each_entry_safe(ctx, cn, list, free_link)
362 i915_gem_context_free(ctx);
365 static void contexts_flush_free(struct i915_gem_contexts *gc)
367 contexts_free_all(llist_del_all(&gc->free_list));
370 static void contexts_free_worker(struct work_struct *work)
372 struct i915_gem_contexts *gc =
373 container_of(work, typeof(*gc), free_work);
375 contexts_flush_free(gc);
378 void i915_gem_context_release(struct kref *ref)
380 struct i915_gem_context *ctx = container_of(ref, typeof(*ctx), ref);
381 struct i915_gem_contexts *gc = &ctx->i915->gem.contexts;
383 trace_i915_context_free(ctx);
384 if (llist_add(&ctx->free_link, &gc->free_list))
385 schedule_work(&gc->free_work);
388 static inline struct i915_gem_engines *
389 __context_engines_static(const struct i915_gem_context *ctx)
391 return rcu_dereference_protected(ctx->engines, true);
394 static bool __reset_engine(struct intel_engine_cs *engine)
396 struct intel_gt *gt = engine->gt;
397 bool success = false;
399 if (!intel_has_reset_engine(gt))
402 if (!test_and_set_bit(I915_RESET_ENGINE + engine->id,
404 success = intel_engine_reset(engine, NULL) == 0;
405 clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
412 static void __reset_context(struct i915_gem_context *ctx,
413 struct intel_engine_cs *engine)
415 intel_gt_handle_error(engine->gt, engine->mask, 0,
416 "context closure in %s", ctx->name);
419 static bool __cancel_engine(struct intel_engine_cs *engine)
422 * Send a "high priority pulse" down the engine to cause the
423 * current request to be momentarily preempted. (If it fails to
424 * be preempted, it will be reset). As we have marked our context
425 * as banned, any incomplete request, including any running, will
426 * be skipped following the preemption.
428 * If there is no hangchecking (one of the reasons why we try to
429 * cancel the context) and no forced preemption, there may be no
430 * means by which we reset the GPU and evict the persistent hog.
431 * Ergo if we are unable to inject a preemptive pulse that can
432 * kill the banned context, we fallback to doing a local reset
435 if (IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT) &&
436 !intel_engine_pulse(engine))
439 /* If we are unable to send a pulse, try resetting this engine. */
440 return __reset_engine(engine);
443 static struct intel_engine_cs *__active_engine(struct i915_request *rq)
445 struct intel_engine_cs *engine, *locked;
448 * Serialise with __i915_request_submit() so that it sees
449 * is-banned?, or we know the request is already inflight.
451 locked = READ_ONCE(rq->engine);
452 spin_lock_irq(&locked->active.lock);
453 while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
454 spin_unlock(&locked->active.lock);
455 spin_lock(&engine->active.lock);
460 if (i915_request_is_active(rq) && rq->fence.error != -EIO)
463 spin_unlock_irq(&locked->active.lock);
468 static struct intel_engine_cs *active_engine(struct intel_context *ce)
470 struct intel_engine_cs *engine = NULL;
471 struct i915_request *rq;
476 mutex_lock(&ce->timeline->mutex);
477 list_for_each_entry_reverse(rq, &ce->timeline->requests, link) {
478 if (i915_request_completed(rq))
481 /* Check with the backend if the request is inflight */
482 engine = __active_engine(rq);
486 mutex_unlock(&ce->timeline->mutex);
491 static void kill_engines(struct i915_gem_engines *engines)
493 struct i915_gem_engines_iter it;
494 struct intel_context *ce;
497 * Map the user's engine back to the actual engines; one virtual
498 * engine will be mapped to multiple engines, and using ctx->engine[]
499 * the same engine may be have multiple instances in the user's map.
500 * However, we only care about pending requests, so only include
501 * engines on which there are incomplete requests.
503 for_each_gem_engine(ce, engines, it) {
504 struct intel_engine_cs *engine;
506 if (intel_context_set_banned(ce))
510 * Check the current active state of this context; if we
511 * are currently executing on the GPU we need to evict
512 * ourselves. On the other hand, if we haven't yet been
513 * submitted to the GPU or if everything is complete,
514 * we have nothing to do.
516 engine = active_engine(ce);
518 /* First attempt to gracefully cancel the context */
519 if (engine && !__cancel_engine(engine))
521 * If we are unable to send a preemptive pulse to bump
522 * the context from the GPU, we have to resort to a full
523 * reset. We hope the collateral damage is worth it.
525 __reset_context(engines->ctx, engine);
529 static void kill_stale_engines(struct i915_gem_context *ctx)
531 struct i915_gem_engines *pos, *next;
533 spin_lock_irq(&ctx->stale.lock);
534 GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
535 list_for_each_entry_safe(pos, next, &ctx->stale.engines, link) {
536 if (!i915_sw_fence_await(&pos->fence)) {
537 list_del_init(&pos->link);
541 spin_unlock_irq(&ctx->stale.lock);
545 spin_lock_irq(&ctx->stale.lock);
546 GEM_BUG_ON(i915_sw_fence_signaled(&pos->fence));
547 list_safe_reset_next(pos, next, link);
548 list_del_init(&pos->link); /* decouple from FENCE_COMPLETE */
550 i915_sw_fence_complete(&pos->fence);
552 spin_unlock_irq(&ctx->stale.lock);
555 static void kill_context(struct i915_gem_context *ctx)
557 kill_stale_engines(ctx);
560 static void engines_idle_release(struct i915_gem_context *ctx,
561 struct i915_gem_engines *engines)
563 struct i915_gem_engines_iter it;
564 struct intel_context *ce;
566 INIT_LIST_HEAD(&engines->link);
568 engines->ctx = i915_gem_context_get(ctx);
570 for_each_gem_engine(ce, engines, it) {
573 /* serialises with execbuf */
574 set_bit(CONTEXT_CLOSED_BIT, &ce->flags);
575 if (!intel_context_pin_if_active(ce))
578 /* Wait until context is finally scheduled out and retired */
579 err = i915_sw_fence_await_active(&engines->fence,
581 I915_ACTIVE_AWAIT_BARRIER);
582 intel_context_unpin(ce);
587 spin_lock_irq(&ctx->stale.lock);
588 if (!i915_gem_context_is_closed(ctx))
589 list_add_tail(&engines->link, &ctx->stale.engines);
590 spin_unlock_irq(&ctx->stale.lock);
593 if (list_empty(&engines->link)) /* raced, already closed */
594 kill_engines(engines);
596 i915_sw_fence_commit(&engines->fence);
599 static void set_closed_name(struct i915_gem_context *ctx)
603 /* Replace '[]' with '<>' to indicate closed in debug prints */
605 s = strrchr(ctx->name, '[');
611 s = strchr(s + 1, ']');
616 static void context_close(struct i915_gem_context *ctx)
618 struct i915_address_space *vm;
620 /* Flush any concurrent set_engines() */
621 mutex_lock(&ctx->engines_mutex);
622 engines_idle_release(ctx, rcu_replace_pointer(ctx->engines, NULL, 1));
623 i915_gem_context_set_closed(ctx);
624 mutex_unlock(&ctx->engines_mutex);
626 mutex_lock(&ctx->mutex);
628 set_closed_name(ctx);
630 vm = i915_gem_context_vm(ctx);
634 ctx->file_priv = ERR_PTR(-EBADF);
637 * The LUT uses the VMA as a backpointer to unref the object,
638 * so we need to clear the LUT before we close all the VMA (inside
643 mutex_unlock(&ctx->mutex);
646 * If the user has disabled hangchecking, we can not be sure that
647 * the batches will ever complete after the context is closed,
648 * keeping the context and all resources pinned forever. So in this
649 * case we opt to forcibly kill off all remaining requests on
652 if (!i915_gem_context_is_persistent(ctx) ||
653 !i915_modparams.enable_hangcheck)
656 i915_gem_context_put(ctx);
659 static int __context_set_persistence(struct i915_gem_context *ctx, bool state)
661 if (i915_gem_context_is_persistent(ctx) == state)
666 * Only contexts that are short-lived [that will expire or be
667 * reset] are allowed to survive past termination. We require
668 * hangcheck to ensure that the persistent requests are healthy.
670 if (!i915_modparams.enable_hangcheck)
673 i915_gem_context_set_persistence(ctx);
675 /* To cancel a context we use "preempt-to-idle" */
676 if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PREEMPTION))
680 * If the cancel fails, we then need to reset, cleanly!
682 * If the per-engine reset fails, all hope is lost! We resort
683 * to a full GPU reset in that unlikely case, but realistically
684 * if the engine could not reset, the full reset does not fare
685 * much better. The damage has been done.
687 * However, if we cannot reset an engine by itself, we cannot
688 * cleanup a hanging persistent context without causing
689 * colateral damage, and we should not pretend we can by
690 * exposing the interface.
692 if (!intel_has_reset_engine(&ctx->i915->gt))
695 i915_gem_context_clear_persistence(ctx);
701 static struct i915_gem_context *
702 __create_context(struct drm_i915_private *i915)
704 struct i915_gem_context *ctx;
705 struct i915_gem_engines *e;
709 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
711 return ERR_PTR(-ENOMEM);
713 kref_init(&ctx->ref);
715 ctx->sched.priority = I915_USER_PRIORITY(I915_PRIORITY_NORMAL);
716 mutex_init(&ctx->mutex);
718 spin_lock_init(&ctx->stale.lock);
719 INIT_LIST_HEAD(&ctx->stale.engines);
721 mutex_init(&ctx->engines_mutex);
722 e = default_engines(ctx);
727 RCU_INIT_POINTER(ctx->engines, e);
729 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
731 /* NB: Mark all slices as needing a remap so that when the context first
732 * loads it will restore whatever remap state already exists. If there
733 * is no remap info, it will be a NOP. */
734 ctx->remap_slice = ALL_L3_SLICES(i915);
736 i915_gem_context_set_bannable(ctx);
737 i915_gem_context_set_recoverable(ctx);
738 __context_set_persistence(ctx, true /* cgroup hook? */);
740 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp); i++)
741 ctx->hang_timestamp[i] = jiffies - CONTEXT_FAST_HANG_JIFFIES;
743 spin_lock(&i915->gem.contexts.lock);
744 list_add_tail(&ctx->link, &i915->gem.contexts.list);
745 spin_unlock(&i915->gem.contexts.lock);
754 static inline struct i915_gem_engines *
755 __context_engines_await(const struct i915_gem_context *ctx)
757 struct i915_gem_engines *engines;
761 engines = rcu_dereference(ctx->engines);
762 GEM_BUG_ON(!engines);
764 if (unlikely(!i915_sw_fence_await(&engines->fence)))
767 if (likely(engines == rcu_access_pointer(ctx->engines)))
770 i915_sw_fence_complete(&engines->fence);
778 context_apply_all(struct i915_gem_context *ctx,
779 int (*fn)(struct intel_context *ce, void *data),
782 struct i915_gem_engines_iter it;
783 struct i915_gem_engines *e;
784 struct intel_context *ce;
787 e = __context_engines_await(ctx);
788 for_each_gem_engine(ce, e, it) {
793 i915_sw_fence_complete(&e->fence);
798 static int __apply_ppgtt(struct intel_context *ce, void *vm)
801 ce->vm = i915_vm_get(vm);
805 static struct i915_address_space *
806 __set_ppgtt(struct i915_gem_context *ctx, struct i915_address_space *vm)
808 struct i915_address_space *old;
810 old = rcu_replace_pointer(ctx->vm,
812 lockdep_is_held(&ctx->mutex));
813 GEM_BUG_ON(old && i915_vm_is_4lvl(vm) != i915_vm_is_4lvl(old));
815 context_apply_all(ctx, __apply_ppgtt, vm);
820 static void __assign_ppgtt(struct i915_gem_context *ctx,
821 struct i915_address_space *vm)
823 if (vm == rcu_access_pointer(ctx->vm))
826 vm = __set_ppgtt(ctx, vm);
831 static void __set_timeline(struct intel_timeline **dst,
832 struct intel_timeline *src)
834 struct intel_timeline *old = *dst;
836 *dst = src ? intel_timeline_get(src) : NULL;
839 intel_timeline_put(old);
842 static int __apply_timeline(struct intel_context *ce, void *timeline)
844 __set_timeline(&ce->timeline, timeline);
848 static void __assign_timeline(struct i915_gem_context *ctx,
849 struct intel_timeline *timeline)
851 __set_timeline(&ctx->timeline, timeline);
852 context_apply_all(ctx, __apply_timeline, timeline);
855 static struct i915_gem_context *
856 i915_gem_create_context(struct drm_i915_private *i915, unsigned int flags)
858 struct i915_gem_context *ctx;
860 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE &&
861 !HAS_EXECLISTS(i915))
862 return ERR_PTR(-EINVAL);
864 /* Reap the stale contexts */
865 contexts_flush_free(&i915->gem.contexts);
867 ctx = __create_context(i915);
871 if (HAS_FULL_PPGTT(i915)) {
872 struct i915_ppgtt *ppgtt;
874 ppgtt = i915_ppgtt_create(&i915->gt);
876 drm_dbg(&i915->drm, "PPGTT setup failed (%ld)\n",
879 return ERR_CAST(ppgtt);
882 mutex_lock(&ctx->mutex);
883 __assign_ppgtt(ctx, &ppgtt->vm);
884 mutex_unlock(&ctx->mutex);
886 i915_vm_put(&ppgtt->vm);
889 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE) {
890 struct intel_timeline *timeline;
892 timeline = intel_timeline_create(&i915->gt, NULL);
893 if (IS_ERR(timeline)) {
895 return ERR_CAST(timeline);
898 __assign_timeline(ctx, timeline);
899 intel_timeline_put(timeline);
902 trace_i915_context_create(ctx);
907 static void init_contexts(struct i915_gem_contexts *gc)
909 spin_lock_init(&gc->lock);
910 INIT_LIST_HEAD(&gc->list);
912 INIT_WORK(&gc->free_work, contexts_free_worker);
913 init_llist_head(&gc->free_list);
916 void i915_gem_init__contexts(struct drm_i915_private *i915)
918 init_contexts(&i915->gem.contexts);
919 drm_dbg(&i915->drm, "%s context support initialized\n",
920 DRIVER_CAPS(i915)->has_logical_contexts ?
924 void i915_gem_driver_release__contexts(struct drm_i915_private *i915)
926 flush_work(&i915->gem.contexts.free_work);
927 rcu_barrier(); /* and flush the left over RCU frees */
930 static int gem_context_register(struct i915_gem_context *ctx,
931 struct drm_i915_file_private *fpriv,
934 struct i915_address_space *vm;
937 ctx->file_priv = fpriv;
939 mutex_lock(&ctx->mutex);
940 vm = i915_gem_context_vm(ctx);
942 WRITE_ONCE(vm->file, fpriv); /* XXX */
943 mutex_unlock(&ctx->mutex);
945 ctx->pid = get_task_pid(current, PIDTYPE_PID);
946 snprintf(ctx->name, sizeof(ctx->name), "%s[%d]",
947 current->comm, pid_nr(ctx->pid));
949 /* And finally expose ourselves to userspace via the idr */
950 ret = xa_alloc(&fpriv->context_xa, id, ctx, xa_limit_32b, GFP_KERNEL);
952 put_pid(fetch_and_zero(&ctx->pid));
957 int i915_gem_context_open(struct drm_i915_private *i915,
958 struct drm_file *file)
960 struct drm_i915_file_private *file_priv = file->driver_priv;
961 struct i915_gem_context *ctx;
965 xa_init_flags(&file_priv->context_xa, XA_FLAGS_ALLOC);
967 /* 0 reserved for invalid/unassigned ppgtt */
968 xa_init_flags(&file_priv->vm_xa, XA_FLAGS_ALLOC1);
970 ctx = i915_gem_create_context(i915, 0);
976 err = gem_context_register(ctx, file_priv, &id);
986 xa_destroy(&file_priv->vm_xa);
987 xa_destroy(&file_priv->context_xa);
991 void i915_gem_context_close(struct drm_file *file)
993 struct drm_i915_file_private *file_priv = file->driver_priv;
994 struct drm_i915_private *i915 = file_priv->dev_priv;
995 struct i915_address_space *vm;
996 struct i915_gem_context *ctx;
999 xa_for_each(&file_priv->context_xa, idx, ctx)
1001 xa_destroy(&file_priv->context_xa);
1003 xa_for_each(&file_priv->vm_xa, idx, vm)
1005 xa_destroy(&file_priv->vm_xa);
1007 contexts_flush_free(&i915->gem.contexts);
1010 int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,
1011 struct drm_file *file)
1013 struct drm_i915_private *i915 = to_i915(dev);
1014 struct drm_i915_gem_vm_control *args = data;
1015 struct drm_i915_file_private *file_priv = file->driver_priv;
1016 struct i915_ppgtt *ppgtt;
1020 if (!HAS_FULL_PPGTT(i915))
1026 ppgtt = i915_ppgtt_create(&i915->gt);
1028 return PTR_ERR(ppgtt);
1030 ppgtt->vm.file = file_priv;
1032 if (args->extensions) {
1033 err = i915_user_extensions(u64_to_user_ptr(args->extensions),
1040 err = xa_alloc(&file_priv->vm_xa, &id, &ppgtt->vm,
1041 xa_limit_32b, GFP_KERNEL);
1045 GEM_BUG_ON(id == 0); /* reserved for invalid/unassigned ppgtt */
1050 i915_vm_put(&ppgtt->vm);
1054 int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,
1055 struct drm_file *file)
1057 struct drm_i915_file_private *file_priv = file->driver_priv;
1058 struct drm_i915_gem_vm_control *args = data;
1059 struct i915_address_space *vm;
1064 if (args->extensions)
1067 vm = xa_erase(&file_priv->vm_xa, args->vm_id);
1075 struct context_barrier_task {
1076 struct i915_active base;
1077 void (*task)(void *data);
1082 static void cb_retire(struct i915_active *base)
1084 struct context_barrier_task *cb = container_of(base, typeof(*cb), base);
1089 i915_active_fini(&cb->base);
1093 I915_SELFTEST_DECLARE(static intel_engine_mask_t context_barrier_inject_fault);
1094 static int context_barrier_task(struct i915_gem_context *ctx,
1095 intel_engine_mask_t engines,
1096 bool (*skip)(struct intel_context *ce, void *data),
1097 int (*emit)(struct i915_request *rq, void *data),
1098 void (*task)(void *data),
1101 struct context_barrier_task *cb;
1102 struct i915_gem_engines_iter it;
1103 struct i915_gem_engines *e;
1104 struct intel_context *ce;
1109 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1113 i915_active_init(&cb->base, NULL, cb_retire);
1114 err = i915_active_acquire(&cb->base);
1120 e = __context_engines_await(ctx);
1122 i915_active_release(&cb->base);
1126 for_each_gem_engine(ce, e, it) {
1127 struct i915_request *rq;
1129 if (I915_SELFTEST_ONLY(context_barrier_inject_fault &
1130 ce->engine->mask)) {
1135 if (!(ce->engine->mask & engines))
1138 if (skip && skip(ce, data))
1141 rq = intel_context_create_request(ce);
1149 err = emit(rq, data);
1151 err = i915_active_add_request(&cb->base, rq);
1153 i915_request_add(rq);
1157 i915_sw_fence_complete(&e->fence);
1159 cb->task = err ? NULL : task; /* caller needs to unwind instead */
1162 i915_active_release(&cb->base);
1167 static int get_ppgtt(struct drm_i915_file_private *file_priv,
1168 struct i915_gem_context *ctx,
1169 struct drm_i915_gem_context_param *args)
1171 struct i915_address_space *vm;
1175 if (!rcu_access_pointer(ctx->vm))
1179 vm = context_get_vm_rcu(ctx);
1184 err = xa_alloc(&file_priv->vm_xa, &id, vm, xa_limit_32b, GFP_KERNEL);
1190 GEM_BUG_ON(id == 0); /* reserved for invalid/unassigned ppgtt */
1199 static void set_ppgtt_barrier(void *data)
1201 struct i915_address_space *old = data;
1203 if (INTEL_GEN(old->i915) < 8)
1204 gen6_ppgtt_unpin_all(i915_vm_to_ppgtt(old));
1209 static int emit_ppgtt_update(struct i915_request *rq, void *data)
1211 struct i915_address_space *vm = rq->context->vm;
1212 struct intel_engine_cs *engine = rq->engine;
1213 u32 base = engine->mmio_base;
1217 if (i915_vm_is_4lvl(vm)) {
1218 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1219 const dma_addr_t pd_daddr = px_dma(ppgtt->pd);
1221 cs = intel_ring_begin(rq, 6);
1225 *cs++ = MI_LOAD_REGISTER_IMM(2);
1227 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, 0));
1228 *cs++ = upper_32_bits(pd_daddr);
1229 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, 0));
1230 *cs++ = lower_32_bits(pd_daddr);
1233 intel_ring_advance(rq, cs);
1234 } else if (HAS_LOGICAL_RING_CONTEXTS(engine->i915)) {
1235 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1238 /* Magic required to prevent forcewake errors! */
1239 err = engine->emit_flush(rq, EMIT_INVALIDATE);
1243 cs = intel_ring_begin(rq, 4 * GEN8_3LVL_PDPES + 2);
1247 *cs++ = MI_LOAD_REGISTER_IMM(2 * GEN8_3LVL_PDPES) | MI_LRI_FORCE_POSTED;
1248 for (i = GEN8_3LVL_PDPES; i--; ) {
1249 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1251 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, i));
1252 *cs++ = upper_32_bits(pd_daddr);
1253 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, i));
1254 *cs++ = lower_32_bits(pd_daddr);
1257 intel_ring_advance(rq, cs);
1263 static bool skip_ppgtt_update(struct intel_context *ce, void *data)
1265 if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags))
1268 if (HAS_LOGICAL_RING_CONTEXTS(ce->engine->i915))
1271 if (!atomic_read(&ce->pin_count))
1274 /* ppGTT is not part of the legacy context image */
1275 if (gen6_ppgtt_pin(i915_vm_to_ppgtt(ce->vm)))
1281 static int set_ppgtt(struct drm_i915_file_private *file_priv,
1282 struct i915_gem_context *ctx,
1283 struct drm_i915_gem_context_param *args)
1285 struct i915_address_space *vm, *old;
1291 if (!rcu_access_pointer(ctx->vm))
1294 if (upper_32_bits(args->value))
1298 vm = xa_load(&file_priv->vm_xa, args->value);
1299 if (vm && !kref_get_unless_zero(&vm->ref))
1305 err = mutex_lock_interruptible(&ctx->mutex);
1309 if (i915_gem_context_is_closed(ctx)) {
1314 if (vm == rcu_access_pointer(ctx->vm))
1317 /* Teardown the existing obj:vma cache, it will have to be rebuilt. */
1320 old = __set_ppgtt(ctx, vm);
1323 * We need to flush any requests using the current ppgtt before
1324 * we release it as the requests do not hold a reference themselves,
1325 * only indirectly through the context.
1327 err = context_barrier_task(ctx, ALL_ENGINES,
1333 i915_vm_close(__set_ppgtt(ctx, old));
1338 mutex_unlock(&ctx->mutex);
1344 static int __apply_ringsize(struct intel_context *ce, void *sz)
1346 return intel_context_set_ring_size(ce, (unsigned long)sz);
1349 static int set_ringsize(struct i915_gem_context *ctx,
1350 struct drm_i915_gem_context_param *args)
1352 if (!HAS_LOGICAL_RING_CONTEXTS(ctx->i915))
1358 if (!IS_ALIGNED(args->value, I915_GTT_PAGE_SIZE))
1361 if (args->value < I915_GTT_PAGE_SIZE)
1364 if (args->value > 128 * I915_GTT_PAGE_SIZE)
1367 return context_apply_all(ctx,
1369 __intel_context_ring_size(args->value));
1372 static int __get_ringsize(struct intel_context *ce, void *arg)
1376 sz = intel_context_get_ring_size(ce);
1377 GEM_BUG_ON(sz > INT_MAX);
1379 return sz; /* stop on first engine */
1382 static int get_ringsize(struct i915_gem_context *ctx,
1383 struct drm_i915_gem_context_param *args)
1387 if (!HAS_LOGICAL_RING_CONTEXTS(ctx->i915))
1393 sz = context_apply_all(ctx, __get_ringsize, NULL);
1402 i915_gem_user_to_context_sseu(struct drm_i915_private *i915,
1403 const struct drm_i915_gem_context_param_sseu *user,
1404 struct intel_sseu *context)
1406 const struct sseu_dev_info *device = &RUNTIME_INFO(i915)->sseu;
1408 /* No zeros in any field. */
1409 if (!user->slice_mask || !user->subslice_mask ||
1410 !user->min_eus_per_subslice || !user->max_eus_per_subslice)
1414 if (user->max_eus_per_subslice < user->min_eus_per_subslice)
1418 * Some future proofing on the types since the uAPI is wider than the
1419 * current internal implementation.
1421 if (overflows_type(user->slice_mask, context->slice_mask) ||
1422 overflows_type(user->subslice_mask, context->subslice_mask) ||
1423 overflows_type(user->min_eus_per_subslice,
1424 context->min_eus_per_subslice) ||
1425 overflows_type(user->max_eus_per_subslice,
1426 context->max_eus_per_subslice))
1429 /* Check validity against hardware. */
1430 if (user->slice_mask & ~device->slice_mask)
1433 if (user->subslice_mask & ~device->subslice_mask[0])
1436 if (user->max_eus_per_subslice > device->max_eus_per_subslice)
1439 context->slice_mask = user->slice_mask;
1440 context->subslice_mask = user->subslice_mask;
1441 context->min_eus_per_subslice = user->min_eus_per_subslice;
1442 context->max_eus_per_subslice = user->max_eus_per_subslice;
1444 /* Part specific restrictions. */
1445 if (IS_GEN(i915, 11)) {
1446 unsigned int hw_s = hweight8(device->slice_mask);
1447 unsigned int hw_ss_per_s = hweight8(device->subslice_mask[0]);
1448 unsigned int req_s = hweight8(context->slice_mask);
1449 unsigned int req_ss = hweight8(context->subslice_mask);
1452 * Only full subslice enablement is possible if more than one
1453 * slice is turned on.
1455 if (req_s > 1 && req_ss != hw_ss_per_s)
1459 * If more than four (SScount bitfield limit) subslices are
1460 * requested then the number has to be even.
1462 if (req_ss > 4 && (req_ss & 1))
1466 * If only one slice is enabled and subslice count is below the
1467 * device full enablement, it must be at most half of the all
1468 * available subslices.
1470 if (req_s == 1 && req_ss < hw_ss_per_s &&
1471 req_ss > (hw_ss_per_s / 2))
1474 /* ABI restriction - VME use case only. */
1476 /* All slices or one slice only. */
1477 if (req_s != 1 && req_s != hw_s)
1481 * Half subslices or full enablement only when one slice is
1485 (req_ss != hw_ss_per_s && req_ss != (hw_ss_per_s / 2)))
1488 /* No EU configuration changes. */
1489 if ((user->min_eus_per_subslice !=
1490 device->max_eus_per_subslice) ||
1491 (user->max_eus_per_subslice !=
1492 device->max_eus_per_subslice))
1499 static int set_sseu(struct i915_gem_context *ctx,
1500 struct drm_i915_gem_context_param *args)
1502 struct drm_i915_private *i915 = ctx->i915;
1503 struct drm_i915_gem_context_param_sseu user_sseu;
1504 struct intel_context *ce;
1505 struct intel_sseu sseu;
1506 unsigned long lookup;
1509 if (args->size < sizeof(user_sseu))
1512 if (!IS_GEN(i915, 11))
1515 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
1522 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
1526 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
1527 lookup |= LOOKUP_USER_INDEX;
1529 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
1533 /* Only render engine supports RPCS configuration. */
1534 if (ce->engine->class != RENDER_CLASS) {
1539 ret = i915_gem_user_to_context_sseu(i915, &user_sseu, &sseu);
1543 ret = intel_context_reconfigure_sseu(ce, sseu);
1547 args->size = sizeof(user_sseu);
1550 intel_context_put(ce);
1554 struct set_engines {
1555 struct i915_gem_context *ctx;
1556 struct i915_gem_engines *engines;
1560 set_engines__load_balance(struct i915_user_extension __user *base, void *data)
1562 struct i915_context_engines_load_balance __user *ext =
1563 container_of_user(base, typeof(*ext), base);
1564 const struct set_engines *set = data;
1565 struct drm_i915_private *i915 = set->ctx->i915;
1566 struct intel_engine_cs *stack[16];
1567 struct intel_engine_cs **siblings;
1568 struct intel_context *ce;
1569 u16 num_siblings, idx;
1573 if (!HAS_EXECLISTS(i915))
1576 if (intel_uc_uses_guc_submission(&i915->gt.uc))
1577 return -ENODEV; /* not implement yet */
1579 if (get_user(idx, &ext->engine_index))
1582 if (idx >= set->engines->num_engines) {
1583 drm_dbg(&i915->drm, "Invalid placement value, %d >= %d\n",
1584 idx, set->engines->num_engines);
1588 idx = array_index_nospec(idx, set->engines->num_engines);
1589 if (set->engines->engines[idx]) {
1591 "Invalid placement[%d], already occupied\n", idx);
1595 if (get_user(num_siblings, &ext->num_siblings))
1598 err = check_user_mbz(&ext->flags);
1602 err = check_user_mbz(&ext->mbz64);
1607 if (num_siblings > ARRAY_SIZE(stack)) {
1608 siblings = kmalloc_array(num_siblings,
1615 for (n = 0; n < num_siblings; n++) {
1616 struct i915_engine_class_instance ci;
1618 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) {
1623 siblings[n] = intel_engine_lookup_user(i915,
1625 ci.engine_instance);
1628 "Invalid sibling[%d]: { class:%d, inst:%d }\n",
1629 n, ci.engine_class, ci.engine_instance);
1635 ce = intel_execlists_create_virtual(siblings, n);
1641 intel_context_set_gem(ce, set->ctx);
1643 if (cmpxchg(&set->engines->engines[idx], NULL, ce)) {
1644 intel_context_put(ce);
1650 if (siblings != stack)
1657 set_engines__bond(struct i915_user_extension __user *base, void *data)
1659 struct i915_context_engines_bond __user *ext =
1660 container_of_user(base, typeof(*ext), base);
1661 const struct set_engines *set = data;
1662 struct drm_i915_private *i915 = set->ctx->i915;
1663 struct i915_engine_class_instance ci;
1664 struct intel_engine_cs *virtual;
1665 struct intel_engine_cs *master;
1669 if (get_user(idx, &ext->virtual_index))
1672 if (idx >= set->engines->num_engines) {
1674 "Invalid index for virtual engine: %d >= %d\n",
1675 idx, set->engines->num_engines);
1679 idx = array_index_nospec(idx, set->engines->num_engines);
1680 if (!set->engines->engines[idx]) {
1681 drm_dbg(&i915->drm, "Invalid engine at %d\n", idx);
1684 virtual = set->engines->engines[idx]->engine;
1686 err = check_user_mbz(&ext->flags);
1690 for (n = 0; n < ARRAY_SIZE(ext->mbz64); n++) {
1691 err = check_user_mbz(&ext->mbz64[n]);
1696 if (copy_from_user(&ci, &ext->master, sizeof(ci)))
1699 master = intel_engine_lookup_user(i915,
1700 ci.engine_class, ci.engine_instance);
1703 "Unrecognised master engine: { class:%u, instance:%u }\n",
1704 ci.engine_class, ci.engine_instance);
1708 if (get_user(num_bonds, &ext->num_bonds))
1711 for (n = 0; n < num_bonds; n++) {
1712 struct intel_engine_cs *bond;
1714 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci)))
1717 bond = intel_engine_lookup_user(i915,
1719 ci.engine_instance);
1722 "Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n",
1723 n, ci.engine_class, ci.engine_instance);
1728 * A non-virtual engine has no siblings to choose between; and
1729 * a submit fence will always be directed to the one engine.
1731 if (intel_engine_is_virtual(virtual)) {
1732 err = intel_virtual_engine_attach_bond(virtual,
1743 static const i915_user_extension_fn set_engines__extensions[] = {
1744 [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE] = set_engines__load_balance,
1745 [I915_CONTEXT_ENGINES_EXT_BOND] = set_engines__bond,
1749 set_engines(struct i915_gem_context *ctx,
1750 const struct drm_i915_gem_context_param *args)
1752 struct drm_i915_private *i915 = ctx->i915;
1753 struct i915_context_param_engines __user *user =
1754 u64_to_user_ptr(args->value);
1755 struct set_engines set = { .ctx = ctx };
1756 unsigned int num_engines, n;
1760 if (!args->size) { /* switch back to legacy user_ring_map */
1761 if (!i915_gem_context_user_engines(ctx))
1764 set.engines = default_engines(ctx);
1765 if (IS_ERR(set.engines))
1766 return PTR_ERR(set.engines);
1771 BUILD_BUG_ON(!IS_ALIGNED(sizeof(*user), sizeof(*user->engines)));
1772 if (args->size < sizeof(*user) ||
1773 !IS_ALIGNED(args->size, sizeof(*user->engines))) {
1774 drm_dbg(&i915->drm, "Invalid size for engine array: %d\n",
1780 * Note that I915_EXEC_RING_MASK limits execbuf to only using the
1781 * first 64 engines defined here.
1783 num_engines = (args->size - sizeof(*user)) / sizeof(*user->engines);
1784 set.engines = alloc_engines(num_engines);
1788 for (n = 0; n < num_engines; n++) {
1789 struct i915_engine_class_instance ci;
1790 struct intel_engine_cs *engine;
1791 struct intel_context *ce;
1793 if (copy_from_user(&ci, &user->engines[n], sizeof(ci))) {
1794 __free_engines(set.engines, n);
1798 if (ci.engine_class == (u16)I915_ENGINE_CLASS_INVALID &&
1799 ci.engine_instance == (u16)I915_ENGINE_CLASS_INVALID_NONE) {
1800 set.engines->engines[n] = NULL;
1804 engine = intel_engine_lookup_user(ctx->i915,
1806 ci.engine_instance);
1809 "Invalid engine[%d]: { class:%d, instance:%d }\n",
1810 n, ci.engine_class, ci.engine_instance);
1811 __free_engines(set.engines, n);
1815 ce = intel_context_create(engine);
1817 __free_engines(set.engines, n);
1821 intel_context_set_gem(ce, ctx);
1823 set.engines->engines[n] = ce;
1825 set.engines->num_engines = num_engines;
1828 if (!get_user(extensions, &user->extensions))
1829 err = i915_user_extensions(u64_to_user_ptr(extensions),
1830 set_engines__extensions,
1831 ARRAY_SIZE(set_engines__extensions),
1834 free_engines(set.engines);
1839 mutex_lock(&ctx->engines_mutex);
1840 if (i915_gem_context_is_closed(ctx)) {
1841 mutex_unlock(&ctx->engines_mutex);
1842 free_engines(set.engines);
1846 i915_gem_context_set_user_engines(ctx);
1848 i915_gem_context_clear_user_engines(ctx);
1849 set.engines = rcu_replace_pointer(ctx->engines, set.engines, 1);
1850 mutex_unlock(&ctx->engines_mutex);
1852 /* Keep track of old engine sets for kill_context() */
1853 engines_idle_release(ctx, set.engines);
1858 static struct i915_gem_engines *
1859 __copy_engines(struct i915_gem_engines *e)
1861 struct i915_gem_engines *copy;
1864 copy = alloc_engines(e->num_engines);
1866 return ERR_PTR(-ENOMEM);
1868 for (n = 0; n < e->num_engines; n++) {
1870 copy->engines[n] = intel_context_get(e->engines[n]);
1872 copy->engines[n] = NULL;
1874 copy->num_engines = n;
1880 get_engines(struct i915_gem_context *ctx,
1881 struct drm_i915_gem_context_param *args)
1883 struct i915_context_param_engines __user *user;
1884 struct i915_gem_engines *e;
1885 size_t n, count, size;
1888 err = mutex_lock_interruptible(&ctx->engines_mutex);
1893 if (i915_gem_context_user_engines(ctx))
1894 e = __copy_engines(i915_gem_context_engines(ctx));
1895 mutex_unlock(&ctx->engines_mutex);
1896 if (IS_ERR_OR_NULL(e)) {
1898 return PTR_ERR_OR_ZERO(e);
1901 count = e->num_engines;
1903 /* Be paranoid in case we have an impedance mismatch */
1904 if (!check_struct_size(user, engines, count, &size)) {
1908 if (overflows_type(size, args->size)) {
1918 if (args->size < size) {
1923 user = u64_to_user_ptr(args->value);
1924 if (put_user(0, &user->extensions)) {
1929 for (n = 0; n < count; n++) {
1930 struct i915_engine_class_instance ci = {
1931 .engine_class = I915_ENGINE_CLASS_INVALID,
1932 .engine_instance = I915_ENGINE_CLASS_INVALID_NONE,
1935 if (e->engines[n]) {
1936 ci.engine_class = e->engines[n]->engine->uabi_class;
1937 ci.engine_instance = e->engines[n]->engine->uabi_instance;
1940 if (copy_to_user(&user->engines[n], &ci, sizeof(ci))) {
1954 set_persistence(struct i915_gem_context *ctx,
1955 const struct drm_i915_gem_context_param *args)
1960 return __context_set_persistence(ctx, args->value);
1963 static int __apply_priority(struct intel_context *ce, void *arg)
1965 struct i915_gem_context *ctx = arg;
1967 if (!intel_engine_has_timeslices(ce->engine))
1970 if (ctx->sched.priority >= I915_PRIORITY_NORMAL)
1971 intel_context_set_use_semaphores(ce);
1973 intel_context_clear_use_semaphores(ce);
1978 static int set_priority(struct i915_gem_context *ctx,
1979 const struct drm_i915_gem_context_param *args)
1981 s64 priority = args->value;
1986 if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PRIORITY))
1989 if (priority > I915_CONTEXT_MAX_USER_PRIORITY ||
1990 priority < I915_CONTEXT_MIN_USER_PRIORITY)
1993 if (priority > I915_CONTEXT_DEFAULT_PRIORITY &&
1994 !capable(CAP_SYS_NICE))
1997 ctx->sched.priority = I915_USER_PRIORITY(priority);
1998 context_apply_all(ctx, __apply_priority, ctx);
2003 static int ctx_setparam(struct drm_i915_file_private *fpriv,
2004 struct i915_gem_context *ctx,
2005 struct drm_i915_gem_context_param *args)
2009 switch (args->param) {
2010 case I915_CONTEXT_PARAM_NO_ZEROMAP:
2013 else if (args->value)
2014 set_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
2016 clear_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
2019 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
2022 else if (args->value)
2023 i915_gem_context_set_no_error_capture(ctx);
2025 i915_gem_context_clear_no_error_capture(ctx);
2028 case I915_CONTEXT_PARAM_BANNABLE:
2031 else if (!capable(CAP_SYS_ADMIN) && !args->value)
2033 else if (args->value)
2034 i915_gem_context_set_bannable(ctx);
2036 i915_gem_context_clear_bannable(ctx);
2039 case I915_CONTEXT_PARAM_RECOVERABLE:
2042 else if (args->value)
2043 i915_gem_context_set_recoverable(ctx);
2045 i915_gem_context_clear_recoverable(ctx);
2048 case I915_CONTEXT_PARAM_PRIORITY:
2049 ret = set_priority(ctx, args);
2052 case I915_CONTEXT_PARAM_SSEU:
2053 ret = set_sseu(ctx, args);
2056 case I915_CONTEXT_PARAM_VM:
2057 ret = set_ppgtt(fpriv, ctx, args);
2060 case I915_CONTEXT_PARAM_ENGINES:
2061 ret = set_engines(ctx, args);
2064 case I915_CONTEXT_PARAM_PERSISTENCE:
2065 ret = set_persistence(ctx, args);
2068 case I915_CONTEXT_PARAM_RINGSIZE:
2069 ret = set_ringsize(ctx, args);
2072 case I915_CONTEXT_PARAM_BAN_PERIOD:
2082 struct i915_gem_context *ctx;
2083 struct drm_i915_file_private *fpriv;
2086 static int create_setparam(struct i915_user_extension __user *ext, void *data)
2088 struct drm_i915_gem_context_create_ext_setparam local;
2089 const struct create_ext *arg = data;
2091 if (copy_from_user(&local, ext, sizeof(local)))
2094 if (local.param.ctx_id)
2097 return ctx_setparam(arg->fpriv, arg->ctx, &local.param);
2100 static int copy_ring_size(struct intel_context *dst,
2101 struct intel_context *src)
2105 sz = intel_context_get_ring_size(src);
2109 return intel_context_set_ring_size(dst, sz);
2112 static int clone_engines(struct i915_gem_context *dst,
2113 struct i915_gem_context *src)
2115 struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
2116 struct i915_gem_engines *clone;
2120 clone = alloc_engines(e->num_engines);
2124 for (n = 0; n < e->num_engines; n++) {
2125 struct intel_engine_cs *engine;
2127 if (!e->engines[n]) {
2128 clone->engines[n] = NULL;
2131 engine = e->engines[n]->engine;
2134 * Virtual engines are singletons; they can only exist
2135 * inside a single context, because they embed their
2136 * HW context... As each virtual context implies a single
2137 * timeline (each engine can only dequeue a single request
2138 * at any time), it would be surprising for two contexts
2139 * to use the same engine. So let's create a copy of
2140 * the virtual engine instead.
2142 if (intel_engine_is_virtual(engine))
2144 intel_execlists_clone_virtual(engine);
2146 clone->engines[n] = intel_context_create(engine);
2147 if (IS_ERR_OR_NULL(clone->engines[n])) {
2148 __free_engines(clone, n);
2152 intel_context_set_gem(clone->engines[n], dst);
2154 /* Copy across the preferred ringsize */
2155 if (copy_ring_size(clone->engines[n], e->engines[n])) {
2156 __free_engines(clone, n + 1);
2160 clone->num_engines = n;
2162 user_engines = i915_gem_context_user_engines(src);
2163 i915_gem_context_unlock_engines(src);
2165 /* Serialised by constructor */
2166 engines_idle_release(dst, rcu_replace_pointer(dst->engines, clone, 1));
2168 i915_gem_context_set_user_engines(dst);
2170 i915_gem_context_clear_user_engines(dst);
2174 i915_gem_context_unlock_engines(src);
2178 static int clone_flags(struct i915_gem_context *dst,
2179 struct i915_gem_context *src)
2181 dst->user_flags = src->user_flags;
2185 static int clone_schedattr(struct i915_gem_context *dst,
2186 struct i915_gem_context *src)
2188 dst->sched = src->sched;
2192 static int clone_sseu(struct i915_gem_context *dst,
2193 struct i915_gem_context *src)
2195 struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
2196 struct i915_gem_engines *clone;
2200 /* no locking required; sole access under constructor*/
2201 clone = __context_engines_static(dst);
2202 if (e->num_engines != clone->num_engines) {
2207 for (n = 0; n < e->num_engines; n++) {
2208 struct intel_context *ce = e->engines[n];
2210 if (clone->engines[n]->engine->class != ce->engine->class) {
2211 /* Must have compatible engine maps! */
2216 /* serialises with set_sseu */
2217 err = intel_context_lock_pinned(ce);
2221 clone->engines[n]->sseu = ce->sseu;
2222 intel_context_unlock_pinned(ce);
2227 i915_gem_context_unlock_engines(src);
2231 static int clone_timeline(struct i915_gem_context *dst,
2232 struct i915_gem_context *src)
2235 __assign_timeline(dst, src->timeline);
2240 static int clone_vm(struct i915_gem_context *dst,
2241 struct i915_gem_context *src)
2243 struct i915_address_space *vm;
2246 if (!rcu_access_pointer(src->vm))
2250 vm = context_get_vm_rcu(src);
2253 if (!mutex_lock_interruptible(&dst->mutex)) {
2254 __assign_ppgtt(dst, vm);
2255 mutex_unlock(&dst->mutex);
2264 static int create_clone(struct i915_user_extension __user *ext, void *data)
2266 static int (* const fn[])(struct i915_gem_context *dst,
2267 struct i915_gem_context *src) = {
2268 #define MAP(x, y) [ilog2(I915_CONTEXT_CLONE_##x)] = y
2269 MAP(ENGINES, clone_engines),
2270 MAP(FLAGS, clone_flags),
2271 MAP(SCHEDATTR, clone_schedattr),
2272 MAP(SSEU, clone_sseu),
2273 MAP(TIMELINE, clone_timeline),
2277 struct drm_i915_gem_context_create_ext_clone local;
2278 const struct create_ext *arg = data;
2279 struct i915_gem_context *dst = arg->ctx;
2280 struct i915_gem_context *src;
2283 if (copy_from_user(&local, ext, sizeof(local)))
2286 BUILD_BUG_ON(GENMASK(BITS_PER_TYPE(local.flags) - 1, ARRAY_SIZE(fn)) !=
2287 I915_CONTEXT_CLONE_UNKNOWN);
2289 if (local.flags & I915_CONTEXT_CLONE_UNKNOWN)
2296 src = __i915_gem_context_lookup_rcu(arg->fpriv, local.clone_id);
2301 GEM_BUG_ON(src == dst);
2303 for (bit = 0; bit < ARRAY_SIZE(fn); bit++) {
2304 if (!(local.flags & BIT(bit)))
2307 err = fn[bit](dst, src);
2315 static const i915_user_extension_fn create_extensions[] = {
2316 [I915_CONTEXT_CREATE_EXT_SETPARAM] = create_setparam,
2317 [I915_CONTEXT_CREATE_EXT_CLONE] = create_clone,
2320 static bool client_is_banned(struct drm_i915_file_private *file_priv)
2322 return atomic_read(&file_priv->ban_score) >= I915_CLIENT_SCORE_BANNED;
2325 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
2326 struct drm_file *file)
2328 struct drm_i915_private *i915 = to_i915(dev);
2329 struct drm_i915_gem_context_create_ext *args = data;
2330 struct create_ext ext_data;
2334 if (!DRIVER_CAPS(i915)->has_logical_contexts)
2337 if (args->flags & I915_CONTEXT_CREATE_FLAGS_UNKNOWN)
2340 ret = intel_gt_terminally_wedged(&i915->gt);
2344 ext_data.fpriv = file->driver_priv;
2345 if (client_is_banned(ext_data.fpriv)) {
2347 "client %s[%d] banned from creating ctx\n",
2348 current->comm, task_pid_nr(current));
2352 ext_data.ctx = i915_gem_create_context(i915, args->flags);
2353 if (IS_ERR(ext_data.ctx))
2354 return PTR_ERR(ext_data.ctx);
2356 if (args->flags & I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS) {
2357 ret = i915_user_extensions(u64_to_user_ptr(args->extensions),
2359 ARRAY_SIZE(create_extensions),
2365 ret = gem_context_register(ext_data.ctx, ext_data.fpriv, &id);
2370 drm_dbg(&i915->drm, "HW context %d created\n", args->ctx_id);
2375 context_close(ext_data.ctx);
2379 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
2380 struct drm_file *file)
2382 struct drm_i915_gem_context_destroy *args = data;
2383 struct drm_i915_file_private *file_priv = file->driver_priv;
2384 struct i915_gem_context *ctx;
2392 ctx = xa_erase(&file_priv->context_xa, args->ctx_id);
2400 static int get_sseu(struct i915_gem_context *ctx,
2401 struct drm_i915_gem_context_param *args)
2403 struct drm_i915_gem_context_param_sseu user_sseu;
2404 struct intel_context *ce;
2405 unsigned long lookup;
2408 if (args->size == 0)
2410 else if (args->size < sizeof(user_sseu))
2413 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
2420 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
2424 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
2425 lookup |= LOOKUP_USER_INDEX;
2427 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
2431 err = intel_context_lock_pinned(ce); /* serialises with set_sseu */
2433 intel_context_put(ce);
2437 user_sseu.slice_mask = ce->sseu.slice_mask;
2438 user_sseu.subslice_mask = ce->sseu.subslice_mask;
2439 user_sseu.min_eus_per_subslice = ce->sseu.min_eus_per_subslice;
2440 user_sseu.max_eus_per_subslice = ce->sseu.max_eus_per_subslice;
2442 intel_context_unlock_pinned(ce);
2443 intel_context_put(ce);
2445 if (copy_to_user(u64_to_user_ptr(args->value), &user_sseu,
2450 args->size = sizeof(user_sseu);
2455 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
2456 struct drm_file *file)
2458 struct drm_i915_file_private *file_priv = file->driver_priv;
2459 struct drm_i915_gem_context_param *args = data;
2460 struct i915_gem_context *ctx;
2463 ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2467 switch (args->param) {
2468 case I915_CONTEXT_PARAM_NO_ZEROMAP:
2470 args->value = test_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
2473 case I915_CONTEXT_PARAM_GTT_SIZE:
2476 if (rcu_access_pointer(ctx->vm))
2477 args->value = rcu_dereference(ctx->vm)->total;
2479 args->value = to_i915(dev)->ggtt.vm.total;
2483 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
2485 args->value = i915_gem_context_no_error_capture(ctx);
2488 case I915_CONTEXT_PARAM_BANNABLE:
2490 args->value = i915_gem_context_is_bannable(ctx);
2493 case I915_CONTEXT_PARAM_RECOVERABLE:
2495 args->value = i915_gem_context_is_recoverable(ctx);
2498 case I915_CONTEXT_PARAM_PRIORITY:
2500 args->value = ctx->sched.priority >> I915_USER_PRIORITY_SHIFT;
2503 case I915_CONTEXT_PARAM_SSEU:
2504 ret = get_sseu(ctx, args);
2507 case I915_CONTEXT_PARAM_VM:
2508 ret = get_ppgtt(file_priv, ctx, args);
2511 case I915_CONTEXT_PARAM_ENGINES:
2512 ret = get_engines(ctx, args);
2515 case I915_CONTEXT_PARAM_PERSISTENCE:
2517 args->value = i915_gem_context_is_persistent(ctx);
2520 case I915_CONTEXT_PARAM_RINGSIZE:
2521 ret = get_ringsize(ctx, args);
2524 case I915_CONTEXT_PARAM_BAN_PERIOD:
2530 i915_gem_context_put(ctx);
2534 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
2535 struct drm_file *file)
2537 struct drm_i915_file_private *file_priv = file->driver_priv;
2538 struct drm_i915_gem_context_param *args = data;
2539 struct i915_gem_context *ctx;
2542 ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2546 ret = ctx_setparam(file_priv, ctx, args);
2548 i915_gem_context_put(ctx);
2552 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
2553 void *data, struct drm_file *file)
2555 struct drm_i915_private *i915 = to_i915(dev);
2556 struct drm_i915_reset_stats *args = data;
2557 struct i915_gem_context *ctx;
2560 if (args->flags || args->pad)
2565 ctx = __i915_gem_context_lookup_rcu(file->driver_priv, args->ctx_id);
2570 * We opt for unserialised reads here. This may result in tearing
2571 * in the extremely unlikely event of a GPU hang on this context
2572 * as we are querying them. If we need that extra layer of protection,
2573 * we should wrap the hangstats with a seqlock.
2576 if (capable(CAP_SYS_ADMIN))
2577 args->reset_count = i915_reset_count(&i915->gpu_error);
2579 args->reset_count = 0;
2581 args->batch_active = atomic_read(&ctx->guilty_count);
2582 args->batch_pending = atomic_read(&ctx->active_count);
2590 /* GEM context-engines iterator: for_each_gem_engine() */
2591 struct intel_context *
2592 i915_gem_engines_iter_next(struct i915_gem_engines_iter *it)
2594 const struct i915_gem_engines *e = it->engines;
2595 struct intel_context *ctx;
2601 if (it->idx >= e->num_engines)
2604 ctx = e->engines[it->idx++];
2610 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2611 #include "selftests/mock_context.c"
2612 #include "selftests/i915_gem_context.c"
2615 static void i915_global_gem_context_shrink(void)
2617 kmem_cache_shrink(global.slab_luts);
2620 static void i915_global_gem_context_exit(void)
2622 kmem_cache_destroy(global.slab_luts);
2625 static struct i915_global_gem_context global = { {
2626 .shrink = i915_global_gem_context_shrink,
2627 .exit = i915_global_gem_context_exit,
2630 int __init i915_global_gem_context_init(void)
2632 global.slab_luts = KMEM_CACHE(i915_lut_handle, 0);
2633 if (!global.slab_luts)
2636 i915_global_register(&global.base);