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_lrc_reg.h"
74 #include "i915_gem_context.h"
75 #include "i915_globals.h"
76 #include "i915_trace.h"
77 #include "i915_user_extensions.h"
79 #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
81 static struct i915_global_gem_context {
82 struct i915_global base;
83 struct kmem_cache *slab_luts;
86 struct i915_lut_handle *i915_lut_handle_alloc(void)
88 return kmem_cache_alloc(global.slab_luts, GFP_KERNEL);
91 void i915_lut_handle_free(struct i915_lut_handle *lut)
93 return kmem_cache_free(global.slab_luts, lut);
96 static void lut_close(struct i915_gem_context *ctx)
98 struct radix_tree_iter iter;
101 lockdep_assert_held(&ctx->mutex);
104 radix_tree_for_each_slot(slot, &ctx->handles_vma, &iter, 0) {
105 struct i915_vma *vma = rcu_dereference_raw(*slot);
106 struct drm_i915_gem_object *obj = vma->obj;
107 struct i915_lut_handle *lut;
109 if (!kref_get_unless_zero(&obj->base.refcount))
113 i915_gem_object_lock(obj);
114 list_for_each_entry(lut, &obj->lut_list, obj_link) {
118 if (lut->handle != iter.index)
121 list_del(&lut->obj_link);
124 i915_gem_object_unlock(obj);
127 if (&lut->obj_link != &obj->lut_list) {
128 i915_lut_handle_free(lut);
129 radix_tree_iter_delete(&ctx->handles_vma, &iter, slot);
130 if (atomic_dec_and_test(&vma->open_count) &&
131 !i915_vma_is_ggtt(vma))
133 i915_gem_object_put(obj);
136 i915_gem_object_put(obj);
141 static struct intel_context *
142 lookup_user_engine(struct i915_gem_context *ctx,
144 const struct i915_engine_class_instance *ci)
145 #define LOOKUP_USER_INDEX BIT(0)
149 if (!!(flags & LOOKUP_USER_INDEX) != i915_gem_context_user_engines(ctx))
150 return ERR_PTR(-EINVAL);
152 if (!i915_gem_context_user_engines(ctx)) {
153 struct intel_engine_cs *engine;
155 engine = intel_engine_lookup_user(ctx->i915,
157 ci->engine_instance);
159 return ERR_PTR(-EINVAL);
163 idx = ci->engine_instance;
166 return i915_gem_context_get_engine(ctx, idx);
169 static inline int new_hw_id(struct drm_i915_private *i915, gfp_t gfp)
173 lockdep_assert_held(&i915->contexts.mutex);
175 if (INTEL_GEN(i915) >= 11)
176 max = GEN11_MAX_CONTEXT_HW_ID;
177 else if (USES_GUC_SUBMISSION(i915))
179 * When using GuC in proxy submission, GuC consumes the
180 * highest bit in the context id to indicate proxy submission.
182 max = MAX_GUC_CONTEXT_HW_ID;
184 max = MAX_CONTEXT_HW_ID;
186 return ida_simple_get(&i915->contexts.hw_ida, 0, max, gfp);
189 static int steal_hw_id(struct drm_i915_private *i915)
191 struct i915_gem_context *ctx, *cn;
195 lockdep_assert_held(&i915->contexts.mutex);
197 list_for_each_entry_safe(ctx, cn,
198 &i915->contexts.hw_id_list, hw_id_link) {
199 if (atomic_read(&ctx->hw_id_pin_count)) {
200 list_move_tail(&ctx->hw_id_link, &pinned);
204 GEM_BUG_ON(!ctx->hw_id); /* perma-pinned kernel context */
205 list_del_init(&ctx->hw_id_link);
211 * Remember how far we got up on the last repossesion scan, so the
212 * list is kept in a "least recently scanned" order.
214 list_splice_tail(&pinned, &i915->contexts.hw_id_list);
218 static int assign_hw_id(struct drm_i915_private *i915, unsigned int *out)
222 lockdep_assert_held(&i915->contexts.mutex);
225 * We prefer to steal/stall ourselves and our users over that of the
226 * entire system. That may be a little unfair to our users, and
227 * even hurt high priority clients. The choice is whether to oomkill
228 * something else, or steal a context id.
230 ret = new_hw_id(i915, GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
231 if (unlikely(ret < 0)) {
232 ret = steal_hw_id(i915);
233 if (ret < 0) /* once again for the correct errno code */
234 ret = new_hw_id(i915, GFP_KERNEL);
243 static void release_hw_id(struct i915_gem_context *ctx)
245 struct drm_i915_private *i915 = ctx->i915;
247 if (list_empty(&ctx->hw_id_link))
250 mutex_lock(&i915->contexts.mutex);
251 if (!list_empty(&ctx->hw_id_link)) {
252 ida_simple_remove(&i915->contexts.hw_ida, ctx->hw_id);
253 list_del_init(&ctx->hw_id_link);
255 mutex_unlock(&i915->contexts.mutex);
258 static void __free_engines(struct i915_gem_engines *e, unsigned int count)
261 if (!e->engines[count])
264 intel_context_put(e->engines[count]);
269 static void free_engines(struct i915_gem_engines *e)
271 __free_engines(e, e->num_engines);
274 static void free_engines_rcu(struct rcu_head *rcu)
276 free_engines(container_of(rcu, struct i915_gem_engines, rcu));
279 static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx)
281 struct intel_engine_cs *engine;
282 struct i915_gem_engines *e;
283 enum intel_engine_id id;
285 e = kzalloc(struct_size(e, engines, I915_NUM_ENGINES), GFP_KERNEL);
287 return ERR_PTR(-ENOMEM);
289 init_rcu_head(&e->rcu);
290 for_each_engine(engine, ctx->i915, id) {
291 struct intel_context *ce;
293 ce = intel_context_create(ctx, engine);
295 __free_engines(e, id);
306 static void i915_gem_context_free(struct i915_gem_context *ctx)
308 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
309 GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
312 i915_ppgtt_put(ctx->ppgtt);
314 free_engines(rcu_access_pointer(ctx->engines));
315 mutex_destroy(&ctx->engines_mutex);
318 i915_timeline_put(ctx->timeline);
323 list_del(&ctx->link);
324 mutex_destroy(&ctx->mutex);
329 static void contexts_free(struct drm_i915_private *i915)
331 struct llist_node *freed = llist_del_all(&i915->contexts.free_list);
332 struct i915_gem_context *ctx, *cn;
334 lockdep_assert_held(&i915->drm.struct_mutex);
336 llist_for_each_entry_safe(ctx, cn, freed, free_link)
337 i915_gem_context_free(ctx);
340 static void contexts_free_first(struct drm_i915_private *i915)
342 struct i915_gem_context *ctx;
343 struct llist_node *freed;
345 lockdep_assert_held(&i915->drm.struct_mutex);
347 freed = llist_del_first(&i915->contexts.free_list);
351 ctx = container_of(freed, typeof(*ctx), free_link);
352 i915_gem_context_free(ctx);
355 static void contexts_free_worker(struct work_struct *work)
357 struct drm_i915_private *i915 =
358 container_of(work, typeof(*i915), contexts.free_work);
360 mutex_lock(&i915->drm.struct_mutex);
362 mutex_unlock(&i915->drm.struct_mutex);
365 void i915_gem_context_release(struct kref *ref)
367 struct i915_gem_context *ctx = container_of(ref, typeof(*ctx), ref);
368 struct drm_i915_private *i915 = ctx->i915;
370 trace_i915_context_free(ctx);
371 if (llist_add(&ctx->free_link, &i915->contexts.free_list))
372 queue_work(i915->wq, &i915->contexts.free_work);
375 static void context_close(struct i915_gem_context *ctx)
377 mutex_lock(&ctx->mutex);
379 i915_gem_context_set_closed(ctx);
380 ctx->file_priv = ERR_PTR(-EBADF);
383 * This context will never again be assinged to HW, so we can
384 * reuse its ID for the next context.
389 * The LUT uses the VMA as a backpointer to unref the object,
390 * so we need to clear the LUT before we close all the VMA (inside
395 mutex_unlock(&ctx->mutex);
396 i915_gem_context_put(ctx);
399 static u32 default_desc_template(const struct drm_i915_private *i915,
400 const struct i915_hw_ppgtt *ppgtt)
405 desc = GEN8_CTX_VALID | GEN8_CTX_PRIVILEGE;
407 address_mode = INTEL_LEGACY_32B_CONTEXT;
408 if (ppgtt && i915_vm_is_4lvl(&ppgtt->vm))
409 address_mode = INTEL_LEGACY_64B_CONTEXT;
410 desc |= address_mode << GEN8_CTX_ADDRESSING_MODE_SHIFT;
413 desc |= GEN8_CTX_L3LLC_COHERENT;
415 /* TODO: WaDisableLiteRestore when we start using semaphore
416 * signalling between Command Streamers
417 * ring->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE;
423 static struct i915_gem_context *
424 __create_context(struct drm_i915_private *dev_priv)
426 struct i915_gem_context *ctx;
427 struct i915_gem_engines *e;
431 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
433 return ERR_PTR(-ENOMEM);
435 kref_init(&ctx->ref);
436 list_add_tail(&ctx->link, &dev_priv->contexts.list);
437 ctx->i915 = dev_priv;
438 ctx->sched.priority = I915_USER_PRIORITY(I915_PRIORITY_NORMAL);
439 mutex_init(&ctx->mutex);
441 mutex_init(&ctx->engines_mutex);
442 e = default_engines(ctx);
447 RCU_INIT_POINTER(ctx->engines, e);
449 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
450 INIT_LIST_HEAD(&ctx->hw_id_link);
452 /* NB: Mark all slices as needing a remap so that when the context first
453 * loads it will restore whatever remap state already exists. If there
454 * is no remap info, it will be a NOP. */
455 ctx->remap_slice = ALL_L3_SLICES(dev_priv);
457 i915_gem_context_set_bannable(ctx);
458 i915_gem_context_set_recoverable(ctx);
460 ctx->ring_size = 4 * PAGE_SIZE;
462 default_desc_template(dev_priv, dev_priv->mm.aliasing_ppgtt);
464 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp); i++)
465 ctx->hang_timestamp[i] = jiffies - CONTEXT_FAST_HANG_JIFFIES;
474 static struct i915_hw_ppgtt *
475 __set_ppgtt(struct i915_gem_context *ctx, struct i915_hw_ppgtt *ppgtt)
477 struct i915_hw_ppgtt *old = ctx->ppgtt;
479 ctx->ppgtt = i915_ppgtt_get(ppgtt);
480 ctx->desc_template = default_desc_template(ctx->i915, ppgtt);
485 static void __assign_ppgtt(struct i915_gem_context *ctx,
486 struct i915_hw_ppgtt *ppgtt)
488 if (ppgtt == ctx->ppgtt)
491 ppgtt = __set_ppgtt(ctx, ppgtt);
493 i915_ppgtt_put(ppgtt);
496 static struct i915_gem_context *
497 i915_gem_create_context(struct drm_i915_private *dev_priv, unsigned int flags)
499 struct i915_gem_context *ctx;
501 lockdep_assert_held(&dev_priv->drm.struct_mutex);
503 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE &&
504 !HAS_EXECLISTS(dev_priv))
505 return ERR_PTR(-EINVAL);
507 /* Reap the most stale context */
508 contexts_free_first(dev_priv);
510 ctx = __create_context(dev_priv);
514 if (HAS_FULL_PPGTT(dev_priv)) {
515 struct i915_hw_ppgtt *ppgtt;
517 ppgtt = i915_ppgtt_create(dev_priv);
519 DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
522 return ERR_CAST(ppgtt);
525 __assign_ppgtt(ctx, ppgtt);
526 i915_ppgtt_put(ppgtt);
529 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE) {
530 struct i915_timeline *timeline;
532 timeline = i915_timeline_create(dev_priv, NULL);
533 if (IS_ERR(timeline)) {
535 return ERR_CAST(timeline);
538 ctx->timeline = timeline;
541 trace_i915_context_create(ctx);
547 * i915_gem_context_create_gvt - create a GVT GEM context
550 * This function is used to create a GVT specific GEM context.
553 * pointer to i915_gem_context on success, error pointer if failed
556 struct i915_gem_context *
557 i915_gem_context_create_gvt(struct drm_device *dev)
559 struct i915_gem_context *ctx;
562 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
563 return ERR_PTR(-ENODEV);
565 ret = i915_mutex_lock_interruptible(dev);
569 ctx = i915_gem_create_context(to_i915(dev), 0);
573 ret = i915_gem_context_pin_hw_id(ctx);
580 ctx->file_priv = ERR_PTR(-EBADF);
581 i915_gem_context_set_closed(ctx); /* not user accessible */
582 i915_gem_context_clear_bannable(ctx);
583 i915_gem_context_set_force_single_submission(ctx);
584 if (!USES_GUC_SUBMISSION(to_i915(dev)))
585 ctx->ring_size = 512 * PAGE_SIZE; /* Max ring buffer size */
587 GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
589 mutex_unlock(&dev->struct_mutex);
594 destroy_kernel_context(struct i915_gem_context **ctxp)
596 struct i915_gem_context *ctx;
598 /* Keep the context ref so that we can free it immediately ourselves */
599 ctx = i915_gem_context_get(fetch_and_zero(ctxp));
600 GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
603 i915_gem_context_free(ctx);
606 struct i915_gem_context *
607 i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio)
609 struct i915_gem_context *ctx;
612 ctx = i915_gem_create_context(i915, 0);
616 err = i915_gem_context_pin_hw_id(ctx);
618 destroy_kernel_context(&ctx);
622 i915_gem_context_clear_bannable(ctx);
623 ctx->sched.priority = I915_USER_PRIORITY(prio);
624 ctx->ring_size = PAGE_SIZE;
626 GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
631 static void init_contexts(struct drm_i915_private *i915)
633 mutex_init(&i915->contexts.mutex);
634 INIT_LIST_HEAD(&i915->contexts.list);
636 /* Using the simple ida interface, the max is limited by sizeof(int) */
637 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > INT_MAX);
638 BUILD_BUG_ON(GEN11_MAX_CONTEXT_HW_ID > INT_MAX);
639 ida_init(&i915->contexts.hw_ida);
640 INIT_LIST_HEAD(&i915->contexts.hw_id_list);
642 INIT_WORK(&i915->contexts.free_work, contexts_free_worker);
643 init_llist_head(&i915->contexts.free_list);
646 static bool needs_preempt_context(struct drm_i915_private *i915)
648 return HAS_EXECLISTS(i915);
651 int i915_gem_contexts_init(struct drm_i915_private *dev_priv)
653 struct i915_gem_context *ctx;
655 /* Reassure ourselves we are only called once */
656 GEM_BUG_ON(dev_priv->kernel_context);
657 GEM_BUG_ON(dev_priv->preempt_context);
659 intel_engine_init_ctx_wa(dev_priv->engine[RCS0]);
660 init_contexts(dev_priv);
662 /* lowest priority; idle task */
663 ctx = i915_gem_context_create_kernel(dev_priv, I915_PRIORITY_MIN);
665 DRM_ERROR("Failed to create default global context\n");
669 * For easy recognisablity, we want the kernel context to be 0 and then
670 * all user contexts will have non-zero hw_id. Kernel contexts are
671 * permanently pinned, so that we never suffer a stall and can
672 * use them from any allocation context (e.g. for evicting other
673 * contexts and from inside the shrinker).
675 GEM_BUG_ON(ctx->hw_id);
676 GEM_BUG_ON(!atomic_read(&ctx->hw_id_pin_count));
677 dev_priv->kernel_context = ctx;
679 /* highest priority; preempting task */
680 if (needs_preempt_context(dev_priv)) {
681 ctx = i915_gem_context_create_kernel(dev_priv, INT_MAX);
683 dev_priv->preempt_context = ctx;
685 DRM_ERROR("Failed to create preempt context; disabling preemption\n");
688 DRM_DEBUG_DRIVER("%s context support initialized\n",
689 DRIVER_CAPS(dev_priv)->has_logical_contexts ?
694 void i915_gem_contexts_lost(struct drm_i915_private *dev_priv)
696 struct intel_engine_cs *engine;
697 enum intel_engine_id id;
699 lockdep_assert_held(&dev_priv->drm.struct_mutex);
701 for_each_engine(engine, dev_priv, id)
702 intel_engine_lost_context(engine);
705 void i915_gem_contexts_fini(struct drm_i915_private *i915)
707 lockdep_assert_held(&i915->drm.struct_mutex);
709 if (i915->preempt_context)
710 destroy_kernel_context(&i915->preempt_context);
711 destroy_kernel_context(&i915->kernel_context);
713 /* Must free all deferred contexts (via flush_workqueue) first */
714 GEM_BUG_ON(!list_empty(&i915->contexts.hw_id_list));
715 ida_destroy(&i915->contexts.hw_ida);
718 static int context_idr_cleanup(int id, void *p, void *data)
724 static int vm_idr_cleanup(int id, void *p, void *data)
730 static int gem_context_register(struct i915_gem_context *ctx,
731 struct drm_i915_file_private *fpriv)
735 ctx->file_priv = fpriv;
737 ctx->ppgtt->vm.file = fpriv;
739 ctx->pid = get_task_pid(current, PIDTYPE_PID);
740 ctx->name = kasprintf(GFP_KERNEL, "%s[%d]",
741 current->comm, pid_nr(ctx->pid));
747 /* And finally expose ourselves to userspace via the idr */
748 mutex_lock(&fpriv->context_idr_lock);
749 ret = idr_alloc(&fpriv->context_idr, ctx, 0, 0, GFP_KERNEL);
750 mutex_unlock(&fpriv->context_idr_lock);
754 kfree(fetch_and_zero(&ctx->name));
756 put_pid(fetch_and_zero(&ctx->pid));
761 int i915_gem_context_open(struct drm_i915_private *i915,
762 struct drm_file *file)
764 struct drm_i915_file_private *file_priv = file->driver_priv;
765 struct i915_gem_context *ctx;
768 mutex_init(&file_priv->context_idr_lock);
769 mutex_init(&file_priv->vm_idr_lock);
771 idr_init(&file_priv->context_idr);
772 idr_init_base(&file_priv->vm_idr, 1);
774 mutex_lock(&i915->drm.struct_mutex);
775 ctx = i915_gem_create_context(i915, 0);
776 mutex_unlock(&i915->drm.struct_mutex);
782 err = gem_context_register(ctx, file_priv);
786 GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
794 idr_destroy(&file_priv->vm_idr);
795 idr_destroy(&file_priv->context_idr);
796 mutex_destroy(&file_priv->vm_idr_lock);
797 mutex_destroy(&file_priv->context_idr_lock);
801 void i915_gem_context_close(struct drm_file *file)
803 struct drm_i915_file_private *file_priv = file->driver_priv;
805 idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
806 idr_destroy(&file_priv->context_idr);
807 mutex_destroy(&file_priv->context_idr_lock);
809 idr_for_each(&file_priv->vm_idr, vm_idr_cleanup, NULL);
810 idr_destroy(&file_priv->vm_idr);
811 mutex_destroy(&file_priv->vm_idr_lock);
814 int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,
815 struct drm_file *file)
817 struct drm_i915_private *i915 = to_i915(dev);
818 struct drm_i915_gem_vm_control *args = data;
819 struct drm_i915_file_private *file_priv = file->driver_priv;
820 struct i915_hw_ppgtt *ppgtt;
823 if (!HAS_FULL_PPGTT(i915))
829 ppgtt = i915_ppgtt_create(i915);
831 return PTR_ERR(ppgtt);
833 ppgtt->vm.file = file_priv;
835 if (args->extensions) {
836 err = i915_user_extensions(u64_to_user_ptr(args->extensions),
843 err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
847 err = idr_alloc(&file_priv->vm_idr, ppgtt, 0, 0, GFP_KERNEL);
851 GEM_BUG_ON(err == 0); /* reserved for invalid/unassigned ppgtt */
853 mutex_unlock(&file_priv->vm_idr_lock);
859 mutex_unlock(&file_priv->vm_idr_lock);
861 i915_ppgtt_put(ppgtt);
865 int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,
866 struct drm_file *file)
868 struct drm_i915_file_private *file_priv = file->driver_priv;
869 struct drm_i915_gem_vm_control *args = data;
870 struct i915_hw_ppgtt *ppgtt;
877 if (args->extensions)
884 err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
888 ppgtt = idr_remove(&file_priv->vm_idr, id);
890 mutex_unlock(&file_priv->vm_idr_lock);
894 i915_ppgtt_put(ppgtt);
898 struct context_barrier_task {
899 struct i915_active base;
900 void (*task)(void *data);
904 static void cb_retire(struct i915_active *base)
906 struct context_barrier_task *cb = container_of(base, typeof(*cb), base);
911 i915_active_fini(&cb->base);
915 I915_SELFTEST_DECLARE(static intel_engine_mask_t context_barrier_inject_fault);
916 static int context_barrier_task(struct i915_gem_context *ctx,
917 intel_engine_mask_t engines,
918 bool (*skip)(struct intel_context *ce, void *data),
919 int (*emit)(struct i915_request *rq, void *data),
920 void (*task)(void *data),
923 struct drm_i915_private *i915 = ctx->i915;
924 struct context_barrier_task *cb;
925 struct i915_gem_engines_iter it;
926 struct intel_context *ce;
929 lockdep_assert_held(&i915->drm.struct_mutex);
932 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
936 i915_active_init(i915, &cb->base, cb_retire);
937 i915_active_acquire(&cb->base);
939 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
940 struct i915_request *rq;
942 if (I915_SELFTEST_ONLY(context_barrier_inject_fault &
948 if (!(ce->engine->mask & engines))
951 if (skip && skip(ce, data))
954 rq = intel_context_create_request(ce);
962 err = emit(rq, data);
964 err = i915_active_ref(&cb->base, rq->fence.context, rq);
966 i915_request_add(rq);
970 i915_gem_context_unlock_engines(ctx);
972 cb->task = err ? NULL : task; /* caller needs to unwind instead */
975 i915_active_release(&cb->base);
980 static int get_ppgtt(struct drm_i915_file_private *file_priv,
981 struct i915_gem_context *ctx,
982 struct drm_i915_gem_context_param *args)
984 struct i915_hw_ppgtt *ppgtt;
990 /* XXX rcu acquire? */
991 ret = mutex_lock_interruptible(&ctx->i915->drm.struct_mutex);
995 ppgtt = i915_ppgtt_get(ctx->ppgtt);
996 mutex_unlock(&ctx->i915->drm.struct_mutex);
998 ret = mutex_lock_interruptible(&file_priv->vm_idr_lock);
1002 ret = idr_alloc(&file_priv->vm_idr, ppgtt, 0, 0, GFP_KERNEL);
1007 i915_ppgtt_get(ppgtt);
1014 mutex_unlock(&file_priv->vm_idr_lock);
1016 i915_ppgtt_put(ppgtt);
1020 static void set_ppgtt_barrier(void *data)
1022 struct i915_hw_ppgtt *old = data;
1024 if (INTEL_GEN(old->vm.i915) < 8)
1025 gen6_ppgtt_unpin_all(old);
1027 i915_ppgtt_put(old);
1030 static int emit_ppgtt_update(struct i915_request *rq, void *data)
1032 struct i915_hw_ppgtt *ppgtt = rq->gem_context->ppgtt;
1033 struct intel_engine_cs *engine = rq->engine;
1034 u32 base = engine->mmio_base;
1038 if (i915_vm_is_4lvl(&ppgtt->vm)) {
1039 const dma_addr_t pd_daddr = px_dma(&ppgtt->pml4);
1041 cs = intel_ring_begin(rq, 6);
1045 *cs++ = MI_LOAD_REGISTER_IMM(2);
1047 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, 0));
1048 *cs++ = upper_32_bits(pd_daddr);
1049 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, 0));
1050 *cs++ = lower_32_bits(pd_daddr);
1053 intel_ring_advance(rq, cs);
1054 } else if (HAS_LOGICAL_RING_CONTEXTS(engine->i915)) {
1055 cs = intel_ring_begin(rq, 4 * GEN8_3LVL_PDPES + 2);
1059 *cs++ = MI_LOAD_REGISTER_IMM(2 * GEN8_3LVL_PDPES);
1060 for (i = GEN8_3LVL_PDPES; i--; ) {
1061 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1063 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, i));
1064 *cs++ = upper_32_bits(pd_daddr);
1065 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, i));
1066 *cs++ = lower_32_bits(pd_daddr);
1069 intel_ring_advance(rq, cs);
1071 /* ppGTT is not part of the legacy context image */
1072 gen6_ppgtt_pin(ppgtt);
1078 static bool skip_ppgtt_update(struct intel_context *ce, void *data)
1080 if (HAS_LOGICAL_RING_CONTEXTS(ce->engine->i915))
1083 return !atomic_read(&ce->pin_count);
1086 static int set_ppgtt(struct drm_i915_file_private *file_priv,
1087 struct i915_gem_context *ctx,
1088 struct drm_i915_gem_context_param *args)
1090 struct i915_hw_ppgtt *ppgtt, *old;
1099 if (upper_32_bits(args->value))
1102 err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
1106 ppgtt = idr_find(&file_priv->vm_idr, args->value);
1108 i915_ppgtt_get(ppgtt);
1109 mutex_unlock(&file_priv->vm_idr_lock);
1113 err = mutex_lock_interruptible(&ctx->i915->drm.struct_mutex);
1117 if (ppgtt == ctx->ppgtt)
1120 /* Teardown the existing obj:vma cache, it will have to be rebuilt. */
1121 mutex_lock(&ctx->mutex);
1123 mutex_unlock(&ctx->mutex);
1125 old = __set_ppgtt(ctx, ppgtt);
1128 * We need to flush any requests using the current ppgtt before
1129 * we release it as the requests do not hold a reference themselves,
1130 * only indirectly through the context.
1132 err = context_barrier_task(ctx, ALL_ENGINES,
1139 ctx->desc_template = default_desc_template(ctx->i915, old);
1140 i915_ppgtt_put(ppgtt);
1144 mutex_unlock(&ctx->i915->drm.struct_mutex);
1147 i915_ppgtt_put(ppgtt);
1151 static int gen8_emit_rpcs_config(struct i915_request *rq,
1152 struct intel_context *ce,
1153 struct intel_sseu sseu)
1158 cs = intel_ring_begin(rq, 4);
1162 offset = i915_ggtt_offset(ce->state) +
1163 LRC_STATE_PN * PAGE_SIZE +
1164 (CTX_R_PWR_CLK_STATE + 1) * 4;
1166 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1167 *cs++ = lower_32_bits(offset);
1168 *cs++ = upper_32_bits(offset);
1169 *cs++ = intel_sseu_make_rpcs(rq->i915, &sseu);
1171 intel_ring_advance(rq, cs);
1177 gen8_modify_rpcs(struct intel_context *ce, struct intel_sseu sseu)
1179 struct i915_request *rq;
1182 lockdep_assert_held(&ce->pin_mutex);
1185 * If the context is not idle, we have to submit an ordered request to
1186 * modify its context image via the kernel context (writing to our own
1187 * image, or into the registers directory, does not stick). Pristine
1188 * and idle contexts will be configured on pinning.
1190 if (!intel_context_is_pinned(ce))
1193 rq = i915_request_create(ce->engine->kernel_context);
1197 /* Queue this switch after all other activity by this context. */
1198 ret = i915_active_request_set(&ce->ring->timeline->last_request, rq);
1202 ret = gen8_emit_rpcs_config(rq, ce, sseu);
1207 * Guarantee context image and the timeline remains pinned until the
1208 * modifying request is retired by setting the ce activity tracker.
1210 * But we only need to take one pin on the account of it. Or in other
1211 * words transfer the pinned ce object to tracked active request.
1213 if (!i915_active_request_isset(&ce->active_tracker))
1214 __intel_context_pin(ce);
1215 __i915_active_request_set(&ce->active_tracker, rq);
1218 i915_request_add(rq);
1223 __intel_context_reconfigure_sseu(struct intel_context *ce,
1224 struct intel_sseu sseu)
1228 GEM_BUG_ON(INTEL_GEN(ce->gem_context->i915) < 8);
1230 ret = intel_context_lock_pinned(ce);
1234 /* Nothing to do if unmodified. */
1235 if (!memcmp(&ce->sseu, &sseu, sizeof(sseu)))
1238 ret = gen8_modify_rpcs(ce, sseu);
1243 intel_context_unlock_pinned(ce);
1248 intel_context_reconfigure_sseu(struct intel_context *ce, struct intel_sseu sseu)
1250 struct drm_i915_private *i915 = ce->gem_context->i915;
1253 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1257 ret = __intel_context_reconfigure_sseu(ce, sseu);
1259 mutex_unlock(&i915->drm.struct_mutex);
1265 user_to_context_sseu(struct drm_i915_private *i915,
1266 const struct drm_i915_gem_context_param_sseu *user,
1267 struct intel_sseu *context)
1269 const struct sseu_dev_info *device = &RUNTIME_INFO(i915)->sseu;
1271 /* No zeros in any field. */
1272 if (!user->slice_mask || !user->subslice_mask ||
1273 !user->min_eus_per_subslice || !user->max_eus_per_subslice)
1277 if (user->max_eus_per_subslice < user->min_eus_per_subslice)
1281 * Some future proofing on the types since the uAPI is wider than the
1282 * current internal implementation.
1284 if (overflows_type(user->slice_mask, context->slice_mask) ||
1285 overflows_type(user->subslice_mask, context->subslice_mask) ||
1286 overflows_type(user->min_eus_per_subslice,
1287 context->min_eus_per_subslice) ||
1288 overflows_type(user->max_eus_per_subslice,
1289 context->max_eus_per_subslice))
1292 /* Check validity against hardware. */
1293 if (user->slice_mask & ~device->slice_mask)
1296 if (user->subslice_mask & ~device->subslice_mask[0])
1299 if (user->max_eus_per_subslice > device->max_eus_per_subslice)
1302 context->slice_mask = user->slice_mask;
1303 context->subslice_mask = user->subslice_mask;
1304 context->min_eus_per_subslice = user->min_eus_per_subslice;
1305 context->max_eus_per_subslice = user->max_eus_per_subslice;
1307 /* Part specific restrictions. */
1308 if (IS_GEN(i915, 11)) {
1309 unsigned int hw_s = hweight8(device->slice_mask);
1310 unsigned int hw_ss_per_s = hweight8(device->subslice_mask[0]);
1311 unsigned int req_s = hweight8(context->slice_mask);
1312 unsigned int req_ss = hweight8(context->subslice_mask);
1315 * Only full subslice enablement is possible if more than one
1316 * slice is turned on.
1318 if (req_s > 1 && req_ss != hw_ss_per_s)
1322 * If more than four (SScount bitfield limit) subslices are
1323 * requested then the number has to be even.
1325 if (req_ss > 4 && (req_ss & 1))
1329 * If only one slice is enabled and subslice count is below the
1330 * device full enablement, it must be at most half of the all
1331 * available subslices.
1333 if (req_s == 1 && req_ss < hw_ss_per_s &&
1334 req_ss > (hw_ss_per_s / 2))
1337 /* ABI restriction - VME use case only. */
1339 /* All slices or one slice only. */
1340 if (req_s != 1 && req_s != hw_s)
1344 * Half subslices or full enablement only when one slice is
1348 (req_ss != hw_ss_per_s && req_ss != (hw_ss_per_s / 2)))
1351 /* No EU configuration changes. */
1352 if ((user->min_eus_per_subslice !=
1353 device->max_eus_per_subslice) ||
1354 (user->max_eus_per_subslice !=
1355 device->max_eus_per_subslice))
1362 static int set_sseu(struct i915_gem_context *ctx,
1363 struct drm_i915_gem_context_param *args)
1365 struct drm_i915_private *i915 = ctx->i915;
1366 struct drm_i915_gem_context_param_sseu user_sseu;
1367 struct intel_context *ce;
1368 struct intel_sseu sseu;
1369 unsigned long lookup;
1372 if (args->size < sizeof(user_sseu))
1375 if (!IS_GEN(i915, 11))
1378 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
1385 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
1389 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
1390 lookup |= LOOKUP_USER_INDEX;
1392 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
1396 /* Only render engine supports RPCS configuration. */
1397 if (ce->engine->class != RENDER_CLASS) {
1402 ret = user_to_context_sseu(i915, &user_sseu, &sseu);
1406 ret = intel_context_reconfigure_sseu(ce, sseu);
1410 args->size = sizeof(user_sseu);
1413 intel_context_put(ce);
1417 struct set_engines {
1418 struct i915_gem_context *ctx;
1419 struct i915_gem_engines *engines;
1423 set_engines__load_balance(struct i915_user_extension __user *base, void *data)
1425 struct i915_context_engines_load_balance __user *ext =
1426 container_of_user(base, typeof(*ext), base);
1427 const struct set_engines *set = data;
1428 struct intel_engine_cs *stack[16];
1429 struct intel_engine_cs **siblings;
1430 struct intel_context *ce;
1431 u16 num_siblings, idx;
1435 if (!HAS_EXECLISTS(set->ctx->i915))
1438 if (USES_GUC_SUBMISSION(set->ctx->i915))
1439 return -ENODEV; /* not implement yet */
1441 if (get_user(idx, &ext->engine_index))
1444 if (idx >= set->engines->num_engines) {
1445 DRM_DEBUG("Invalid placement value, %d >= %d\n",
1446 idx, set->engines->num_engines);
1450 idx = array_index_nospec(idx, set->engines->num_engines);
1451 if (set->engines->engines[idx]) {
1452 DRM_DEBUG("Invalid placement[%d], already occupied\n", idx);
1456 if (get_user(num_siblings, &ext->num_siblings))
1459 err = check_user_mbz(&ext->flags);
1463 err = check_user_mbz(&ext->mbz64);
1468 if (num_siblings > ARRAY_SIZE(stack)) {
1469 siblings = kmalloc_array(num_siblings,
1476 for (n = 0; n < num_siblings; n++) {
1477 struct i915_engine_class_instance ci;
1479 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) {
1484 siblings[n] = intel_engine_lookup_user(set->ctx->i915,
1486 ci.engine_instance);
1488 DRM_DEBUG("Invalid sibling[%d]: { class:%d, inst:%d }\n",
1489 n, ci.engine_class, ci.engine_instance);
1495 ce = intel_execlists_create_virtual(set->ctx, siblings, n);
1501 if (cmpxchg(&set->engines->engines[idx], NULL, ce)) {
1502 intel_context_put(ce);
1508 if (siblings != stack)
1515 set_engines__bond(struct i915_user_extension __user *base, void *data)
1517 struct i915_context_engines_bond __user *ext =
1518 container_of_user(base, typeof(*ext), base);
1519 const struct set_engines *set = data;
1520 struct i915_engine_class_instance ci;
1521 struct intel_engine_cs *virtual;
1522 struct intel_engine_cs *master;
1526 if (get_user(idx, &ext->virtual_index))
1529 if (idx >= set->engines->num_engines) {
1530 DRM_DEBUG("Invalid index for virtual engine: %d >= %d\n",
1531 idx, set->engines->num_engines);
1535 idx = array_index_nospec(idx, set->engines->num_engines);
1536 if (!set->engines->engines[idx]) {
1537 DRM_DEBUG("Invalid engine at %d\n", idx);
1540 virtual = set->engines->engines[idx]->engine;
1542 err = check_user_mbz(&ext->flags);
1546 for (n = 0; n < ARRAY_SIZE(ext->mbz64); n++) {
1547 err = check_user_mbz(&ext->mbz64[n]);
1552 if (copy_from_user(&ci, &ext->master, sizeof(ci)))
1555 master = intel_engine_lookup_user(set->ctx->i915,
1556 ci.engine_class, ci.engine_instance);
1558 DRM_DEBUG("Unrecognised master engine: { class:%u, instance:%u }\n",
1559 ci.engine_class, ci.engine_instance);
1563 if (get_user(num_bonds, &ext->num_bonds))
1566 for (n = 0; n < num_bonds; n++) {
1567 struct intel_engine_cs *bond;
1569 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci)))
1572 bond = intel_engine_lookup_user(set->ctx->i915,
1574 ci.engine_instance);
1576 DRM_DEBUG("Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n",
1577 n, ci.engine_class, ci.engine_instance);
1582 * A non-virtual engine has no siblings to choose between; and
1583 * a submit fence will always be directed to the one engine.
1585 if (intel_engine_is_virtual(virtual)) {
1586 err = intel_virtual_engine_attach_bond(virtual,
1597 static const i915_user_extension_fn set_engines__extensions[] = {
1598 [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE] = set_engines__load_balance,
1599 [I915_CONTEXT_ENGINES_EXT_BOND] = set_engines__bond,
1603 set_engines(struct i915_gem_context *ctx,
1604 const struct drm_i915_gem_context_param *args)
1606 struct i915_context_param_engines __user *user =
1607 u64_to_user_ptr(args->value);
1608 struct set_engines set = { .ctx = ctx };
1609 unsigned int num_engines, n;
1613 if (!args->size) { /* switch back to legacy user_ring_map */
1614 if (!i915_gem_context_user_engines(ctx))
1617 set.engines = default_engines(ctx);
1618 if (IS_ERR(set.engines))
1619 return PTR_ERR(set.engines);
1624 BUILD_BUG_ON(!IS_ALIGNED(sizeof(*user), sizeof(*user->engines)));
1625 if (args->size < sizeof(*user) ||
1626 !IS_ALIGNED(args->size, sizeof(*user->engines))) {
1627 DRM_DEBUG("Invalid size for engine array: %d\n",
1633 * Note that I915_EXEC_RING_MASK limits execbuf to only using the
1634 * first 64 engines defined here.
1636 num_engines = (args->size - sizeof(*user)) / sizeof(*user->engines);
1638 set.engines = kmalloc(struct_size(set.engines, engines, num_engines),
1643 init_rcu_head(&set.engines->rcu);
1644 for (n = 0; n < num_engines; n++) {
1645 struct i915_engine_class_instance ci;
1646 struct intel_engine_cs *engine;
1648 if (copy_from_user(&ci, &user->engines[n], sizeof(ci))) {
1649 __free_engines(set.engines, n);
1653 if (ci.engine_class == (u16)I915_ENGINE_CLASS_INVALID &&
1654 ci.engine_instance == (u16)I915_ENGINE_CLASS_INVALID_NONE) {
1655 set.engines->engines[n] = NULL;
1659 engine = intel_engine_lookup_user(ctx->i915,
1661 ci.engine_instance);
1663 DRM_DEBUG("Invalid engine[%d]: { class:%d, instance:%d }\n",
1664 n, ci.engine_class, ci.engine_instance);
1665 __free_engines(set.engines, n);
1669 set.engines->engines[n] = intel_context_create(ctx, engine);
1670 if (!set.engines->engines[n]) {
1671 __free_engines(set.engines, n);
1675 set.engines->num_engines = num_engines;
1678 if (!get_user(extensions, &user->extensions))
1679 err = i915_user_extensions(u64_to_user_ptr(extensions),
1680 set_engines__extensions,
1681 ARRAY_SIZE(set_engines__extensions),
1684 free_engines(set.engines);
1689 mutex_lock(&ctx->engines_mutex);
1691 i915_gem_context_set_user_engines(ctx);
1693 i915_gem_context_clear_user_engines(ctx);
1694 rcu_swap_protected(ctx->engines, set.engines, 1);
1695 mutex_unlock(&ctx->engines_mutex);
1697 call_rcu(&set.engines->rcu, free_engines_rcu);
1702 static struct i915_gem_engines *
1703 __copy_engines(struct i915_gem_engines *e)
1705 struct i915_gem_engines *copy;
1708 copy = kmalloc(struct_size(e, engines, e->num_engines), GFP_KERNEL);
1710 return ERR_PTR(-ENOMEM);
1712 init_rcu_head(©->rcu);
1713 for (n = 0; n < e->num_engines; n++) {
1715 copy->engines[n] = intel_context_get(e->engines[n]);
1717 copy->engines[n] = NULL;
1719 copy->num_engines = n;
1725 get_engines(struct i915_gem_context *ctx,
1726 struct drm_i915_gem_context_param *args)
1728 struct i915_context_param_engines __user *user;
1729 struct i915_gem_engines *e;
1730 size_t n, count, size;
1733 err = mutex_lock_interruptible(&ctx->engines_mutex);
1738 if (i915_gem_context_user_engines(ctx))
1739 e = __copy_engines(i915_gem_context_engines(ctx));
1740 mutex_unlock(&ctx->engines_mutex);
1741 if (IS_ERR_OR_NULL(e)) {
1743 return PTR_ERR_OR_ZERO(e);
1746 count = e->num_engines;
1748 /* Be paranoid in case we have an impedance mismatch */
1749 if (!check_struct_size(user, engines, count, &size)) {
1753 if (overflows_type(size, args->size)) {
1763 if (args->size < size) {
1768 user = u64_to_user_ptr(args->value);
1769 if (!access_ok(user, size)) {
1774 if (put_user(0, &user->extensions)) {
1779 for (n = 0; n < count; n++) {
1780 struct i915_engine_class_instance ci = {
1781 .engine_class = I915_ENGINE_CLASS_INVALID,
1782 .engine_instance = I915_ENGINE_CLASS_INVALID_NONE,
1785 if (e->engines[n]) {
1786 ci.engine_class = e->engines[n]->engine->uabi_class;
1787 ci.engine_instance = e->engines[n]->engine->instance;
1790 if (copy_to_user(&user->engines[n], &ci, sizeof(ci))) {
1803 static int ctx_setparam(struct drm_i915_file_private *fpriv,
1804 struct i915_gem_context *ctx,
1805 struct drm_i915_gem_context_param *args)
1809 switch (args->param) {
1810 case I915_CONTEXT_PARAM_NO_ZEROMAP:
1813 else if (args->value)
1814 set_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
1816 clear_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
1819 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
1822 else if (args->value)
1823 i915_gem_context_set_no_error_capture(ctx);
1825 i915_gem_context_clear_no_error_capture(ctx);
1828 case I915_CONTEXT_PARAM_BANNABLE:
1831 else if (!capable(CAP_SYS_ADMIN) && !args->value)
1833 else if (args->value)
1834 i915_gem_context_set_bannable(ctx);
1836 i915_gem_context_clear_bannable(ctx);
1839 case I915_CONTEXT_PARAM_RECOVERABLE:
1842 else if (args->value)
1843 i915_gem_context_set_recoverable(ctx);
1845 i915_gem_context_clear_recoverable(ctx);
1848 case I915_CONTEXT_PARAM_PRIORITY:
1850 s64 priority = args->value;
1854 else if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PRIORITY))
1856 else if (priority > I915_CONTEXT_MAX_USER_PRIORITY ||
1857 priority < I915_CONTEXT_MIN_USER_PRIORITY)
1859 else if (priority > I915_CONTEXT_DEFAULT_PRIORITY &&
1860 !capable(CAP_SYS_NICE))
1863 ctx->sched.priority =
1864 I915_USER_PRIORITY(priority);
1868 case I915_CONTEXT_PARAM_SSEU:
1869 ret = set_sseu(ctx, args);
1872 case I915_CONTEXT_PARAM_VM:
1873 ret = set_ppgtt(fpriv, ctx, args);
1876 case I915_CONTEXT_PARAM_ENGINES:
1877 ret = set_engines(ctx, args);
1880 case I915_CONTEXT_PARAM_BAN_PERIOD:
1890 struct i915_gem_context *ctx;
1891 struct drm_i915_file_private *fpriv;
1894 static int create_setparam(struct i915_user_extension __user *ext, void *data)
1896 struct drm_i915_gem_context_create_ext_setparam local;
1897 const struct create_ext *arg = data;
1899 if (copy_from_user(&local, ext, sizeof(local)))
1902 if (local.param.ctx_id)
1905 return ctx_setparam(arg->fpriv, arg->ctx, &local.param);
1908 static int clone_engines(struct i915_gem_context *dst,
1909 struct i915_gem_context *src)
1911 struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1912 struct i915_gem_engines *clone;
1916 clone = kmalloc(struct_size(e, engines, e->num_engines), GFP_KERNEL);
1920 init_rcu_head(&clone->rcu);
1921 for (n = 0; n < e->num_engines; n++) {
1922 struct intel_engine_cs *engine;
1924 if (!e->engines[n]) {
1925 clone->engines[n] = NULL;
1928 engine = e->engines[n]->engine;
1931 * Virtual engines are singletons; they can only exist
1932 * inside a single context, because they embed their
1933 * HW context... As each virtual context implies a single
1934 * timeline (each engine can only dequeue a single request
1935 * at any time), it would be surprising for two contexts
1936 * to use the same engine. So let's create a copy of
1937 * the virtual engine instead.
1939 if (intel_engine_is_virtual(engine))
1941 intel_execlists_clone_virtual(dst, engine);
1943 clone->engines[n] = intel_context_create(dst, engine);
1944 if (IS_ERR_OR_NULL(clone->engines[n])) {
1945 __free_engines(clone, n);
1949 clone->num_engines = n;
1951 user_engines = i915_gem_context_user_engines(src);
1952 i915_gem_context_unlock_engines(src);
1954 free_engines(dst->engines);
1955 RCU_INIT_POINTER(dst->engines, clone);
1957 i915_gem_context_set_user_engines(dst);
1959 i915_gem_context_clear_user_engines(dst);
1963 i915_gem_context_unlock_engines(src);
1967 static int clone_flags(struct i915_gem_context *dst,
1968 struct i915_gem_context *src)
1970 dst->user_flags = src->user_flags;
1974 static int clone_schedattr(struct i915_gem_context *dst,
1975 struct i915_gem_context *src)
1977 dst->sched = src->sched;
1981 static int clone_sseu(struct i915_gem_context *dst,
1982 struct i915_gem_context *src)
1984 struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1985 struct i915_gem_engines *clone;
1989 clone = dst->engines; /* no locking required; sole access */
1990 if (e->num_engines != clone->num_engines) {
1995 for (n = 0; n < e->num_engines; n++) {
1996 struct intel_context *ce = e->engines[n];
1998 if (clone->engines[n]->engine->class != ce->engine->class) {
1999 /* Must have compatible engine maps! */
2004 /* serialises with set_sseu */
2005 err = intel_context_lock_pinned(ce);
2009 clone->engines[n]->sseu = ce->sseu;
2010 intel_context_unlock_pinned(ce);
2015 i915_gem_context_unlock_engines(src);
2019 static int clone_timeline(struct i915_gem_context *dst,
2020 struct i915_gem_context *src)
2022 if (src->timeline) {
2023 GEM_BUG_ON(src->timeline == dst->timeline);
2026 i915_timeline_put(dst->timeline);
2027 dst->timeline = i915_timeline_get(src->timeline);
2033 static int clone_vm(struct i915_gem_context *dst,
2034 struct i915_gem_context *src)
2036 struct i915_hw_ppgtt *ppgtt;
2040 ppgtt = READ_ONCE(src->ppgtt);
2044 if (!kref_get_unless_zero(&ppgtt->ref))
2048 * This ppgtt may have be reallocated between
2049 * the read and the kref, and reassigned to a third
2050 * context. In order to avoid inadvertent sharing
2051 * of this ppgtt with that third context (and not
2052 * src), we have to confirm that we have the same
2053 * ppgtt after passing through the strong memory
2054 * barrier implied by a successful
2055 * kref_get_unless_zero().
2057 * Once we have acquired the current ppgtt of src,
2058 * we no longer care if it is released from src, as
2059 * it cannot be reallocated elsewhere.
2062 if (ppgtt == READ_ONCE(src->ppgtt))
2065 i915_ppgtt_put(ppgtt);
2070 __assign_ppgtt(dst, ppgtt);
2071 i915_ppgtt_put(ppgtt);
2077 static int create_clone(struct i915_user_extension __user *ext, void *data)
2079 static int (* const fn[])(struct i915_gem_context *dst,
2080 struct i915_gem_context *src) = {
2081 #define MAP(x, y) [ilog2(I915_CONTEXT_CLONE_##x)] = y
2082 MAP(ENGINES, clone_engines),
2083 MAP(FLAGS, clone_flags),
2084 MAP(SCHEDATTR, clone_schedattr),
2085 MAP(SSEU, clone_sseu),
2086 MAP(TIMELINE, clone_timeline),
2090 struct drm_i915_gem_context_create_ext_clone local;
2091 const struct create_ext *arg = data;
2092 struct i915_gem_context *dst = arg->ctx;
2093 struct i915_gem_context *src;
2096 if (copy_from_user(&local, ext, sizeof(local)))
2099 BUILD_BUG_ON(GENMASK(BITS_PER_TYPE(local.flags) - 1, ARRAY_SIZE(fn)) !=
2100 I915_CONTEXT_CLONE_UNKNOWN);
2102 if (local.flags & I915_CONTEXT_CLONE_UNKNOWN)
2109 src = __i915_gem_context_lookup_rcu(arg->fpriv, local.clone_id);
2114 GEM_BUG_ON(src == dst);
2116 for (bit = 0; bit < ARRAY_SIZE(fn); bit++) {
2117 if (!(local.flags & BIT(bit)))
2120 err = fn[bit](dst, src);
2128 static const i915_user_extension_fn create_extensions[] = {
2129 [I915_CONTEXT_CREATE_EXT_SETPARAM] = create_setparam,
2130 [I915_CONTEXT_CREATE_EXT_CLONE] = create_clone,
2133 static bool client_is_banned(struct drm_i915_file_private *file_priv)
2135 return atomic_read(&file_priv->ban_score) >= I915_CLIENT_SCORE_BANNED;
2138 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
2139 struct drm_file *file)
2141 struct drm_i915_private *i915 = to_i915(dev);
2142 struct drm_i915_gem_context_create_ext *args = data;
2143 struct create_ext ext_data;
2146 if (!DRIVER_CAPS(i915)->has_logical_contexts)
2149 if (args->flags & I915_CONTEXT_CREATE_FLAGS_UNKNOWN)
2152 ret = i915_terminally_wedged(i915);
2156 ext_data.fpriv = file->driver_priv;
2157 if (client_is_banned(ext_data.fpriv)) {
2158 DRM_DEBUG("client %s[%d] banned from creating ctx\n",
2160 pid_nr(get_task_pid(current, PIDTYPE_PID)));
2164 ret = i915_mutex_lock_interruptible(dev);
2168 ext_data.ctx = i915_gem_create_context(i915, args->flags);
2169 mutex_unlock(&dev->struct_mutex);
2170 if (IS_ERR(ext_data.ctx))
2171 return PTR_ERR(ext_data.ctx);
2173 if (args->flags & I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS) {
2174 ret = i915_user_extensions(u64_to_user_ptr(args->extensions),
2176 ARRAY_SIZE(create_extensions),
2182 ret = gem_context_register(ext_data.ctx, ext_data.fpriv);
2187 DRM_DEBUG("HW context %d created\n", args->ctx_id);
2192 context_close(ext_data.ctx);
2196 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
2197 struct drm_file *file)
2199 struct drm_i915_gem_context_destroy *args = data;
2200 struct drm_i915_file_private *file_priv = file->driver_priv;
2201 struct i915_gem_context *ctx;
2209 if (mutex_lock_interruptible(&file_priv->context_idr_lock))
2212 ctx = idr_remove(&file_priv->context_idr, args->ctx_id);
2213 mutex_unlock(&file_priv->context_idr_lock);
2221 static int get_sseu(struct i915_gem_context *ctx,
2222 struct drm_i915_gem_context_param *args)
2224 struct drm_i915_gem_context_param_sseu user_sseu;
2225 struct intel_context *ce;
2226 unsigned long lookup;
2229 if (args->size == 0)
2231 else if (args->size < sizeof(user_sseu))
2234 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
2241 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
2245 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
2246 lookup |= LOOKUP_USER_INDEX;
2248 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
2252 err = intel_context_lock_pinned(ce); /* serialises with set_sseu */
2254 intel_context_put(ce);
2258 user_sseu.slice_mask = ce->sseu.slice_mask;
2259 user_sseu.subslice_mask = ce->sseu.subslice_mask;
2260 user_sseu.min_eus_per_subslice = ce->sseu.min_eus_per_subslice;
2261 user_sseu.max_eus_per_subslice = ce->sseu.max_eus_per_subslice;
2263 intel_context_unlock_pinned(ce);
2264 intel_context_put(ce);
2266 if (copy_to_user(u64_to_user_ptr(args->value), &user_sseu,
2271 args->size = sizeof(user_sseu);
2276 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
2277 struct drm_file *file)
2279 struct drm_i915_file_private *file_priv = file->driver_priv;
2280 struct drm_i915_gem_context_param *args = data;
2281 struct i915_gem_context *ctx;
2284 ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2288 switch (args->param) {
2289 case I915_CONTEXT_PARAM_NO_ZEROMAP:
2291 args->value = test_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
2294 case I915_CONTEXT_PARAM_GTT_SIZE:
2297 args->value = ctx->ppgtt->vm.total;
2298 else if (to_i915(dev)->mm.aliasing_ppgtt)
2299 args->value = to_i915(dev)->mm.aliasing_ppgtt->vm.total;
2301 args->value = to_i915(dev)->ggtt.vm.total;
2304 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
2306 args->value = i915_gem_context_no_error_capture(ctx);
2309 case I915_CONTEXT_PARAM_BANNABLE:
2311 args->value = i915_gem_context_is_bannable(ctx);
2314 case I915_CONTEXT_PARAM_RECOVERABLE:
2316 args->value = i915_gem_context_is_recoverable(ctx);
2319 case I915_CONTEXT_PARAM_PRIORITY:
2321 args->value = ctx->sched.priority >> I915_USER_PRIORITY_SHIFT;
2324 case I915_CONTEXT_PARAM_SSEU:
2325 ret = get_sseu(ctx, args);
2328 case I915_CONTEXT_PARAM_VM:
2329 ret = get_ppgtt(file_priv, ctx, args);
2332 case I915_CONTEXT_PARAM_ENGINES:
2333 ret = get_engines(ctx, args);
2336 case I915_CONTEXT_PARAM_BAN_PERIOD:
2342 i915_gem_context_put(ctx);
2346 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
2347 struct drm_file *file)
2349 struct drm_i915_file_private *file_priv = file->driver_priv;
2350 struct drm_i915_gem_context_param *args = data;
2351 struct i915_gem_context *ctx;
2354 ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2358 ret = ctx_setparam(file_priv, ctx, args);
2360 i915_gem_context_put(ctx);
2364 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
2365 void *data, struct drm_file *file)
2367 struct drm_i915_private *dev_priv = to_i915(dev);
2368 struct drm_i915_reset_stats *args = data;
2369 struct i915_gem_context *ctx;
2372 if (args->flags || args->pad)
2377 ctx = __i915_gem_context_lookup_rcu(file->driver_priv, args->ctx_id);
2382 * We opt for unserialised reads here. This may result in tearing
2383 * in the extremely unlikely event of a GPU hang on this context
2384 * as we are querying them. If we need that extra layer of protection,
2385 * we should wrap the hangstats with a seqlock.
2388 if (capable(CAP_SYS_ADMIN))
2389 args->reset_count = i915_reset_count(&dev_priv->gpu_error);
2391 args->reset_count = 0;
2393 args->batch_active = atomic_read(&ctx->guilty_count);
2394 args->batch_pending = atomic_read(&ctx->active_count);
2402 int __i915_gem_context_pin_hw_id(struct i915_gem_context *ctx)
2404 struct drm_i915_private *i915 = ctx->i915;
2407 mutex_lock(&i915->contexts.mutex);
2409 GEM_BUG_ON(i915_gem_context_is_closed(ctx));
2411 if (list_empty(&ctx->hw_id_link)) {
2412 GEM_BUG_ON(atomic_read(&ctx->hw_id_pin_count));
2414 err = assign_hw_id(i915, &ctx->hw_id);
2418 list_add_tail(&ctx->hw_id_link, &i915->contexts.hw_id_list);
2421 GEM_BUG_ON(atomic_read(&ctx->hw_id_pin_count) == ~0u);
2422 atomic_inc(&ctx->hw_id_pin_count);
2425 mutex_unlock(&i915->contexts.mutex);
2429 /* GEM context-engines iterator: for_each_gem_engine() */
2430 struct intel_context *
2431 i915_gem_engines_iter_next(struct i915_gem_engines_iter *it)
2433 const struct i915_gem_engines *e = it->engines;
2434 struct intel_context *ctx;
2437 if (it->idx >= e->num_engines)
2440 ctx = e->engines[it->idx++];
2446 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2447 #include "selftests/mock_context.c"
2448 #include "selftests/i915_gem_context.c"
2451 static void i915_global_gem_context_shrink(void)
2453 kmem_cache_shrink(global.slab_luts);
2456 static void i915_global_gem_context_exit(void)
2458 kmem_cache_destroy(global.slab_luts);
2461 static struct i915_global_gem_context global = { {
2462 .shrink = i915_global_gem_context_shrink,
2463 .exit = i915_global_gem_context_exit,
2466 int __init i915_global_gem_context_init(void)
2468 global.slab_luts = KMEM_CACHE(i915_lut_handle, 0);
2469 if (!global.slab_luts)
2472 i915_global_register(&global.base);