]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/i915/gem/i915_gem_context.c
Merge drm/drm-next into drm-intel-next-queued
[linux.git] / drivers / gpu / drm / i915 / gem / i915_gem_context.c
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2011-2012 Intel Corporation
5  */
6
7 /*
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.
14  *
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.
25  *
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.
30  *
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
42  *
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
52  *
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.
59  *
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.
64  *
65  */
66
67 #include <linux/log2.h>
68 #include <linux/nospec.h>
69
70 #include <drm/i915_drm.h>
71
72 #include "gt/intel_lrc_reg.h"
73
74 #include "i915_gem_context.h"
75 #include "i915_globals.h"
76 #include "i915_trace.h"
77 #include "i915_user_extensions.h"
78
79 #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
80
81 static struct i915_global_gem_context {
82         struct i915_global base;
83         struct kmem_cache *slab_luts;
84 } global;
85
86 struct i915_lut_handle *i915_lut_handle_alloc(void)
87 {
88         return kmem_cache_alloc(global.slab_luts, GFP_KERNEL);
89 }
90
91 void i915_lut_handle_free(struct i915_lut_handle *lut)
92 {
93         return kmem_cache_free(global.slab_luts, lut);
94 }
95
96 static void lut_close(struct i915_gem_context *ctx)
97 {
98         struct radix_tree_iter iter;
99         void __rcu **slot;
100
101         lockdep_assert_held(&ctx->mutex);
102
103         rcu_read_lock();
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;
108
109                 if (!kref_get_unless_zero(&obj->base.refcount))
110                         continue;
111
112                 rcu_read_unlock();
113                 i915_gem_object_lock(obj);
114                 list_for_each_entry(lut, &obj->lut_list, obj_link) {
115                         if (lut->ctx != ctx)
116                                 continue;
117
118                         if (lut->handle != iter.index)
119                                 continue;
120
121                         list_del(&lut->obj_link);
122                         break;
123                 }
124                 i915_gem_object_unlock(obj);
125                 rcu_read_lock();
126
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))
132                                 i915_vma_close(vma);
133                         i915_gem_object_put(obj);
134                 }
135
136                 i915_gem_object_put(obj);
137         }
138         rcu_read_unlock();
139 }
140
141 static struct intel_context *
142 lookup_user_engine(struct i915_gem_context *ctx,
143                    unsigned long flags,
144                    const struct i915_engine_class_instance *ci)
145 #define LOOKUP_USER_INDEX BIT(0)
146 {
147         int idx;
148
149         if (!!(flags & LOOKUP_USER_INDEX) != i915_gem_context_user_engines(ctx))
150                 return ERR_PTR(-EINVAL);
151
152         if (!i915_gem_context_user_engines(ctx)) {
153                 struct intel_engine_cs *engine;
154
155                 engine = intel_engine_lookup_user(ctx->i915,
156                                                   ci->engine_class,
157                                                   ci->engine_instance);
158                 if (!engine)
159                         return ERR_PTR(-EINVAL);
160
161                 idx = engine->id;
162         } else {
163                 idx = ci->engine_instance;
164         }
165
166         return i915_gem_context_get_engine(ctx, idx);
167 }
168
169 static inline int new_hw_id(struct drm_i915_private *i915, gfp_t gfp)
170 {
171         unsigned int max;
172
173         lockdep_assert_held(&i915->contexts.mutex);
174
175         if (INTEL_GEN(i915) >= 11)
176                 max = GEN11_MAX_CONTEXT_HW_ID;
177         else if (USES_GUC_SUBMISSION(i915))
178                 /*
179                  * When using GuC in proxy submission, GuC consumes the
180                  * highest bit in the context id to indicate proxy submission.
181                  */
182                 max = MAX_GUC_CONTEXT_HW_ID;
183         else
184                 max = MAX_CONTEXT_HW_ID;
185
186         return ida_simple_get(&i915->contexts.hw_ida, 0, max, gfp);
187 }
188
189 static int steal_hw_id(struct drm_i915_private *i915)
190 {
191         struct i915_gem_context *ctx, *cn;
192         LIST_HEAD(pinned);
193         int id = -ENOSPC;
194
195         lockdep_assert_held(&i915->contexts.mutex);
196
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);
201                         continue;
202                 }
203
204                 GEM_BUG_ON(!ctx->hw_id); /* perma-pinned kernel context */
205                 list_del_init(&ctx->hw_id_link);
206                 id = ctx->hw_id;
207                 break;
208         }
209
210         /*
211          * Remember how far we got up on the last repossesion scan, so the
212          * list is kept in a "least recently scanned" order.
213          */
214         list_splice_tail(&pinned, &i915->contexts.hw_id_list);
215         return id;
216 }
217
218 static int assign_hw_id(struct drm_i915_private *i915, unsigned int *out)
219 {
220         int ret;
221
222         lockdep_assert_held(&i915->contexts.mutex);
223
224         /*
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.
229          */
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);
235                 if (ret < 0)
236                         return ret;
237         }
238
239         *out = ret;
240         return 0;
241 }
242
243 static void release_hw_id(struct i915_gem_context *ctx)
244 {
245         struct drm_i915_private *i915 = ctx->i915;
246
247         if (list_empty(&ctx->hw_id_link))
248                 return;
249
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);
254         }
255         mutex_unlock(&i915->contexts.mutex);
256 }
257
258 static void __free_engines(struct i915_gem_engines *e, unsigned int count)
259 {
260         while (count--) {
261                 if (!e->engines[count])
262                         continue;
263
264                 intel_context_put(e->engines[count]);
265         }
266         kfree(e);
267 }
268
269 static void free_engines(struct i915_gem_engines *e)
270 {
271         __free_engines(e, e->num_engines);
272 }
273
274 static void free_engines_rcu(struct rcu_head *rcu)
275 {
276         free_engines(container_of(rcu, struct i915_gem_engines, rcu));
277 }
278
279 static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx)
280 {
281         struct intel_engine_cs *engine;
282         struct i915_gem_engines *e;
283         enum intel_engine_id id;
284
285         e = kzalloc(struct_size(e, engines, I915_NUM_ENGINES), GFP_KERNEL);
286         if (!e)
287                 return ERR_PTR(-ENOMEM);
288
289         init_rcu_head(&e->rcu);
290         for_each_engine(engine, ctx->i915, id) {
291                 struct intel_context *ce;
292
293                 ce = intel_context_create(ctx, engine);
294                 if (IS_ERR(ce)) {
295                         __free_engines(e, id);
296                         return ERR_CAST(ce);
297                 }
298
299                 e->engines[id] = ce;
300         }
301         e->num_engines = id;
302
303         return e;
304 }
305
306 static void i915_gem_context_free(struct i915_gem_context *ctx)
307 {
308         lockdep_assert_held(&ctx->i915->drm.struct_mutex);
309         GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
310
311         release_hw_id(ctx);
312         if (ctx->vm)
313                 i915_vm_put(ctx->vm);
314
315         free_engines(rcu_access_pointer(ctx->engines));
316         mutex_destroy(&ctx->engines_mutex);
317
318         if (ctx->timeline)
319                 intel_timeline_put(ctx->timeline);
320
321         kfree(ctx->name);
322         put_pid(ctx->pid);
323
324         list_del(&ctx->link);
325         mutex_destroy(&ctx->mutex);
326
327         kfree_rcu(ctx, rcu);
328 }
329
330 static void contexts_free(struct drm_i915_private *i915)
331 {
332         struct llist_node *freed = llist_del_all(&i915->contexts.free_list);
333         struct i915_gem_context *ctx, *cn;
334
335         lockdep_assert_held(&i915->drm.struct_mutex);
336
337         llist_for_each_entry_safe(ctx, cn, freed, free_link)
338                 i915_gem_context_free(ctx);
339 }
340
341 static void contexts_free_first(struct drm_i915_private *i915)
342 {
343         struct i915_gem_context *ctx;
344         struct llist_node *freed;
345
346         lockdep_assert_held(&i915->drm.struct_mutex);
347
348         freed = llist_del_first(&i915->contexts.free_list);
349         if (!freed)
350                 return;
351
352         ctx = container_of(freed, typeof(*ctx), free_link);
353         i915_gem_context_free(ctx);
354 }
355
356 static void contexts_free_worker(struct work_struct *work)
357 {
358         struct drm_i915_private *i915 =
359                 container_of(work, typeof(*i915), contexts.free_work);
360
361         mutex_lock(&i915->drm.struct_mutex);
362         contexts_free(i915);
363         mutex_unlock(&i915->drm.struct_mutex);
364 }
365
366 void i915_gem_context_release(struct kref *ref)
367 {
368         struct i915_gem_context *ctx = container_of(ref, typeof(*ctx), ref);
369         struct drm_i915_private *i915 = ctx->i915;
370
371         trace_i915_context_free(ctx);
372         if (llist_add(&ctx->free_link, &i915->contexts.free_list))
373                 queue_work(i915->wq, &i915->contexts.free_work);
374 }
375
376 static void context_close(struct i915_gem_context *ctx)
377 {
378         mutex_lock(&ctx->mutex);
379
380         i915_gem_context_set_closed(ctx);
381         ctx->file_priv = ERR_PTR(-EBADF);
382
383         /*
384          * This context will never again be assinged to HW, so we can
385          * reuse its ID for the next context.
386          */
387         release_hw_id(ctx);
388
389         /*
390          * The LUT uses the VMA as a backpointer to unref the object,
391          * so we need to clear the LUT before we close all the VMA (inside
392          * the ppgtt).
393          */
394         lut_close(ctx);
395
396         mutex_unlock(&ctx->mutex);
397         i915_gem_context_put(ctx);
398 }
399
400 static u32 default_desc_template(const struct drm_i915_private *i915,
401                                  const struct i915_address_space *vm)
402 {
403         u32 address_mode;
404         u32 desc;
405
406         desc = GEN8_CTX_VALID | GEN8_CTX_PRIVILEGE;
407
408         address_mode = INTEL_LEGACY_32B_CONTEXT;
409         if (vm && i915_vm_is_4lvl(vm))
410                 address_mode = INTEL_LEGACY_64B_CONTEXT;
411         desc |= address_mode << GEN8_CTX_ADDRESSING_MODE_SHIFT;
412
413         if (IS_GEN(i915, 8))
414                 desc |= GEN8_CTX_L3LLC_COHERENT;
415
416         /* TODO: WaDisableLiteRestore when we start using semaphore
417          * signalling between Command Streamers
418          * ring->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE;
419          */
420
421         return desc;
422 }
423
424 static struct i915_gem_context *
425 __create_context(struct drm_i915_private *i915)
426 {
427         struct i915_gem_context *ctx;
428         struct i915_gem_engines *e;
429         int err;
430         int i;
431
432         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
433         if (!ctx)
434                 return ERR_PTR(-ENOMEM);
435
436         kref_init(&ctx->ref);
437         list_add_tail(&ctx->link, &i915->contexts.list);
438         ctx->i915 = i915;
439         ctx->sched.priority = I915_USER_PRIORITY(I915_PRIORITY_NORMAL);
440         mutex_init(&ctx->mutex);
441
442         mutex_init(&ctx->engines_mutex);
443         e = default_engines(ctx);
444         if (IS_ERR(e)) {
445                 err = PTR_ERR(e);
446                 goto err_free;
447         }
448         RCU_INIT_POINTER(ctx->engines, e);
449
450         INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
451         INIT_LIST_HEAD(&ctx->hw_id_link);
452
453         /* NB: Mark all slices as needing a remap so that when the context first
454          * loads it will restore whatever remap state already exists. If there
455          * is no remap info, it will be a NOP. */
456         ctx->remap_slice = ALL_L3_SLICES(i915);
457
458         i915_gem_context_set_bannable(ctx);
459         i915_gem_context_set_recoverable(ctx);
460
461         ctx->ring_size = 4 * PAGE_SIZE;
462         ctx->desc_template =
463                 default_desc_template(i915, &i915->mm.aliasing_ppgtt->vm);
464
465         for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp); i++)
466                 ctx->hang_timestamp[i] = jiffies - CONTEXT_FAST_HANG_JIFFIES;
467
468         return ctx;
469
470 err_free:
471         kfree(ctx);
472         return ERR_PTR(err);
473 }
474
475 static struct i915_address_space *
476 __set_ppgtt(struct i915_gem_context *ctx, struct i915_address_space *vm)
477 {
478         struct i915_address_space *old = ctx->vm;
479
480         ctx->vm = i915_vm_get(vm);
481         ctx->desc_template = default_desc_template(ctx->i915, vm);
482
483         return old;
484 }
485
486 static void __assign_ppgtt(struct i915_gem_context *ctx,
487                            struct i915_address_space *vm)
488 {
489         if (vm == ctx->vm)
490                 return;
491
492         vm = __set_ppgtt(ctx, vm);
493         if (vm)
494                 i915_vm_put(vm);
495 }
496
497 static struct i915_gem_context *
498 i915_gem_create_context(struct drm_i915_private *dev_priv, unsigned int flags)
499 {
500         struct i915_gem_context *ctx;
501
502         lockdep_assert_held(&dev_priv->drm.struct_mutex);
503
504         if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE &&
505             !HAS_EXECLISTS(dev_priv))
506                 return ERR_PTR(-EINVAL);
507
508         /* Reap the most stale context */
509         contexts_free_first(dev_priv);
510
511         ctx = __create_context(dev_priv);
512         if (IS_ERR(ctx))
513                 return ctx;
514
515         if (HAS_FULL_PPGTT(dev_priv)) {
516                 struct i915_ppgtt *ppgtt;
517
518                 ppgtt = i915_ppgtt_create(dev_priv);
519                 if (IS_ERR(ppgtt)) {
520                         DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
521                                          PTR_ERR(ppgtt));
522                         context_close(ctx);
523                         return ERR_CAST(ppgtt);
524                 }
525
526                 __assign_ppgtt(ctx, &ppgtt->vm);
527                 i915_vm_put(&ppgtt->vm);
528         }
529
530         if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE) {
531                 struct intel_timeline *timeline;
532
533                 timeline = intel_timeline_create(&dev_priv->gt, NULL);
534                 if (IS_ERR(timeline)) {
535                         context_close(ctx);
536                         return ERR_CAST(timeline);
537                 }
538
539                 ctx->timeline = timeline;
540         }
541
542         trace_i915_context_create(ctx);
543
544         return ctx;
545 }
546
547 /**
548  * i915_gem_context_create_gvt - create a GVT GEM context
549  * @dev: drm device *
550  *
551  * This function is used to create a GVT specific GEM context.
552  *
553  * Returns:
554  * pointer to i915_gem_context on success, error pointer if failed
555  *
556  */
557 struct i915_gem_context *
558 i915_gem_context_create_gvt(struct drm_device *dev)
559 {
560         struct i915_gem_context *ctx;
561         int ret;
562
563         if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
564                 return ERR_PTR(-ENODEV);
565
566         ret = i915_mutex_lock_interruptible(dev);
567         if (ret)
568                 return ERR_PTR(ret);
569
570         ctx = i915_gem_create_context(to_i915(dev), 0);
571         if (IS_ERR(ctx))
572                 goto out;
573
574         ret = i915_gem_context_pin_hw_id(ctx);
575         if (ret) {
576                 context_close(ctx);
577                 ctx = ERR_PTR(ret);
578                 goto out;
579         }
580
581         ctx->file_priv = ERR_PTR(-EBADF);
582         i915_gem_context_set_closed(ctx); /* not user accessible */
583         i915_gem_context_clear_bannable(ctx);
584         i915_gem_context_set_force_single_submission(ctx);
585         if (!USES_GUC_SUBMISSION(to_i915(dev)))
586                 ctx->ring_size = 512 * PAGE_SIZE; /* Max ring buffer size */
587
588         GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
589 out:
590         mutex_unlock(&dev->struct_mutex);
591         return ctx;
592 }
593
594 static void
595 destroy_kernel_context(struct i915_gem_context **ctxp)
596 {
597         struct i915_gem_context *ctx;
598
599         /* Keep the context ref so that we can free it immediately ourselves */
600         ctx = i915_gem_context_get(fetch_and_zero(ctxp));
601         GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
602
603         context_close(ctx);
604         i915_gem_context_free(ctx);
605 }
606
607 struct i915_gem_context *
608 i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio)
609 {
610         struct i915_gem_context *ctx;
611         int err;
612
613         ctx = i915_gem_create_context(i915, 0);
614         if (IS_ERR(ctx))
615                 return ctx;
616
617         err = i915_gem_context_pin_hw_id(ctx);
618         if (err) {
619                 destroy_kernel_context(&ctx);
620                 return ERR_PTR(err);
621         }
622
623         i915_gem_context_clear_bannable(ctx);
624         ctx->sched.priority = I915_USER_PRIORITY(prio);
625         ctx->ring_size = PAGE_SIZE;
626
627         GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
628
629         return ctx;
630 }
631
632 static void init_contexts(struct drm_i915_private *i915)
633 {
634         mutex_init(&i915->contexts.mutex);
635         INIT_LIST_HEAD(&i915->contexts.list);
636
637         /* Using the simple ida interface, the max is limited by sizeof(int) */
638         BUILD_BUG_ON(MAX_CONTEXT_HW_ID > INT_MAX);
639         BUILD_BUG_ON(GEN11_MAX_CONTEXT_HW_ID > INT_MAX);
640         ida_init(&i915->contexts.hw_ida);
641         INIT_LIST_HEAD(&i915->contexts.hw_id_list);
642
643         INIT_WORK(&i915->contexts.free_work, contexts_free_worker);
644         init_llist_head(&i915->contexts.free_list);
645 }
646
647 int i915_gem_contexts_init(struct drm_i915_private *dev_priv)
648 {
649         struct i915_gem_context *ctx;
650
651         /* Reassure ourselves we are only called once */
652         GEM_BUG_ON(dev_priv->kernel_context);
653
654         init_contexts(dev_priv);
655
656         /* lowest priority; idle task */
657         ctx = i915_gem_context_create_kernel(dev_priv, I915_PRIORITY_MIN);
658         if (IS_ERR(ctx)) {
659                 DRM_ERROR("Failed to create default global context\n");
660                 return PTR_ERR(ctx);
661         }
662         /*
663          * For easy recognisablity, we want the kernel context to be 0 and then
664          * all user contexts will have non-zero hw_id. Kernel contexts are
665          * permanently pinned, so that we never suffer a stall and can
666          * use them from any allocation context (e.g. for evicting other
667          * contexts and from inside the shrinker).
668          */
669         GEM_BUG_ON(ctx->hw_id);
670         GEM_BUG_ON(!atomic_read(&ctx->hw_id_pin_count));
671         dev_priv->kernel_context = ctx;
672
673         DRM_DEBUG_DRIVER("%s context support initialized\n",
674                          DRIVER_CAPS(dev_priv)->has_logical_contexts ?
675                          "logical" : "fake");
676         return 0;
677 }
678
679 void i915_gem_contexts_fini(struct drm_i915_private *i915)
680 {
681         lockdep_assert_held(&i915->drm.struct_mutex);
682
683         destroy_kernel_context(&i915->kernel_context);
684
685         /* Must free all deferred contexts (via flush_workqueue) first */
686         GEM_BUG_ON(!list_empty(&i915->contexts.hw_id_list));
687         ida_destroy(&i915->contexts.hw_ida);
688 }
689
690 static int context_idr_cleanup(int id, void *p, void *data)
691 {
692         context_close(p);
693         return 0;
694 }
695
696 static int vm_idr_cleanup(int id, void *p, void *data)
697 {
698         i915_vm_put(p);
699         return 0;
700 }
701
702 static int gem_context_register(struct i915_gem_context *ctx,
703                                 struct drm_i915_file_private *fpriv)
704 {
705         int ret;
706
707         ctx->file_priv = fpriv;
708         if (ctx->vm)
709                 ctx->vm->file = fpriv;
710
711         ctx->pid = get_task_pid(current, PIDTYPE_PID);
712         ctx->name = kasprintf(GFP_KERNEL, "%s[%d]",
713                               current->comm, pid_nr(ctx->pid));
714         if (!ctx->name) {
715                 ret = -ENOMEM;
716                 goto err_pid;
717         }
718
719         /* And finally expose ourselves to userspace via the idr */
720         mutex_lock(&fpriv->context_idr_lock);
721         ret = idr_alloc(&fpriv->context_idr, ctx, 0, 0, GFP_KERNEL);
722         mutex_unlock(&fpriv->context_idr_lock);
723         if (ret >= 0)
724                 goto out;
725
726         kfree(fetch_and_zero(&ctx->name));
727 err_pid:
728         put_pid(fetch_and_zero(&ctx->pid));
729 out:
730         return ret;
731 }
732
733 int i915_gem_context_open(struct drm_i915_private *i915,
734                           struct drm_file *file)
735 {
736         struct drm_i915_file_private *file_priv = file->driver_priv;
737         struct i915_gem_context *ctx;
738         int err;
739
740         mutex_init(&file_priv->context_idr_lock);
741         mutex_init(&file_priv->vm_idr_lock);
742
743         idr_init(&file_priv->context_idr);
744         idr_init_base(&file_priv->vm_idr, 1);
745
746         mutex_lock(&i915->drm.struct_mutex);
747         ctx = i915_gem_create_context(i915, 0);
748         mutex_unlock(&i915->drm.struct_mutex);
749         if (IS_ERR(ctx)) {
750                 err = PTR_ERR(ctx);
751                 goto err;
752         }
753
754         err = gem_context_register(ctx, file_priv);
755         if (err < 0)
756                 goto err_ctx;
757
758         GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
759         GEM_BUG_ON(err > 0);
760
761         return 0;
762
763 err_ctx:
764         context_close(ctx);
765 err:
766         idr_destroy(&file_priv->vm_idr);
767         idr_destroy(&file_priv->context_idr);
768         mutex_destroy(&file_priv->vm_idr_lock);
769         mutex_destroy(&file_priv->context_idr_lock);
770         return err;
771 }
772
773 void i915_gem_context_close(struct drm_file *file)
774 {
775         struct drm_i915_file_private *file_priv = file->driver_priv;
776
777         idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
778         idr_destroy(&file_priv->context_idr);
779         mutex_destroy(&file_priv->context_idr_lock);
780
781         idr_for_each(&file_priv->vm_idr, vm_idr_cleanup, NULL);
782         idr_destroy(&file_priv->vm_idr);
783         mutex_destroy(&file_priv->vm_idr_lock);
784 }
785
786 int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,
787                              struct drm_file *file)
788 {
789         struct drm_i915_private *i915 = to_i915(dev);
790         struct drm_i915_gem_vm_control *args = data;
791         struct drm_i915_file_private *file_priv = file->driver_priv;
792         struct i915_ppgtt *ppgtt;
793         int err;
794
795         if (!HAS_FULL_PPGTT(i915))
796                 return -ENODEV;
797
798         if (args->flags)
799                 return -EINVAL;
800
801         ppgtt = i915_ppgtt_create(i915);
802         if (IS_ERR(ppgtt))
803                 return PTR_ERR(ppgtt);
804
805         ppgtt->vm.file = file_priv;
806
807         if (args->extensions) {
808                 err = i915_user_extensions(u64_to_user_ptr(args->extensions),
809                                            NULL, 0,
810                                            ppgtt);
811                 if (err)
812                         goto err_put;
813         }
814
815         err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
816         if (err)
817                 goto err_put;
818
819         err = idr_alloc(&file_priv->vm_idr, &ppgtt->vm, 0, 0, GFP_KERNEL);
820         if (err < 0)
821                 goto err_unlock;
822
823         GEM_BUG_ON(err == 0); /* reserved for invalid/unassigned ppgtt */
824
825         mutex_unlock(&file_priv->vm_idr_lock);
826
827         args->vm_id = err;
828         return 0;
829
830 err_unlock:
831         mutex_unlock(&file_priv->vm_idr_lock);
832 err_put:
833         i915_vm_put(&ppgtt->vm);
834         return err;
835 }
836
837 int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,
838                               struct drm_file *file)
839 {
840         struct drm_i915_file_private *file_priv = file->driver_priv;
841         struct drm_i915_gem_vm_control *args = data;
842         struct i915_address_space *vm;
843         int err;
844         u32 id;
845
846         if (args->flags)
847                 return -EINVAL;
848
849         if (args->extensions)
850                 return -EINVAL;
851
852         id = args->vm_id;
853         if (!id)
854                 return -ENOENT;
855
856         err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
857         if (err)
858                 return err;
859
860         vm = idr_remove(&file_priv->vm_idr, id);
861
862         mutex_unlock(&file_priv->vm_idr_lock);
863         if (!vm)
864                 return -ENOENT;
865
866         i915_vm_put(vm);
867         return 0;
868 }
869
870 struct context_barrier_task {
871         struct i915_active base;
872         void (*task)(void *data);
873         void *data;
874 };
875
876 static void cb_retire(struct i915_active *base)
877 {
878         struct context_barrier_task *cb = container_of(base, typeof(*cb), base);
879
880         if (cb->task)
881                 cb->task(cb->data);
882
883         i915_active_fini(&cb->base);
884         kfree(cb);
885 }
886
887 I915_SELFTEST_DECLARE(static intel_engine_mask_t context_barrier_inject_fault);
888 static int context_barrier_task(struct i915_gem_context *ctx,
889                                 intel_engine_mask_t engines,
890                                 bool (*skip)(struct intel_context *ce, void *data),
891                                 int (*emit)(struct i915_request *rq, void *data),
892                                 void (*task)(void *data),
893                                 void *data)
894 {
895         struct drm_i915_private *i915 = ctx->i915;
896         struct context_barrier_task *cb;
897         struct i915_gem_engines_iter it;
898         struct intel_context *ce;
899         int err = 0;
900
901         lockdep_assert_held(&i915->drm.struct_mutex);
902         GEM_BUG_ON(!task);
903
904         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
905         if (!cb)
906                 return -ENOMEM;
907
908         i915_active_init(i915, &cb->base, NULL, cb_retire);
909         err = i915_active_acquire(&cb->base);
910         if (err) {
911                 kfree(cb);
912                 return err;
913         }
914
915         for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
916                 struct i915_request *rq;
917
918                 if (I915_SELFTEST_ONLY(context_barrier_inject_fault &
919                                        ce->engine->mask)) {
920                         err = -ENXIO;
921                         break;
922                 }
923
924                 if (!(ce->engine->mask & engines))
925                         continue;
926
927                 if (skip && skip(ce, data))
928                         continue;
929
930                 rq = intel_context_create_request(ce);
931                 if (IS_ERR(rq)) {
932                         err = PTR_ERR(rq);
933                         break;
934                 }
935
936                 err = 0;
937                 if (emit)
938                         err = emit(rq, data);
939                 if (err == 0)
940                         err = i915_active_ref(&cb->base, rq->fence.context, rq);
941
942                 i915_request_add(rq);
943                 if (err)
944                         break;
945         }
946         i915_gem_context_unlock_engines(ctx);
947
948         cb->task = err ? NULL : task; /* caller needs to unwind instead */
949         cb->data = data;
950
951         i915_active_release(&cb->base);
952
953         return err;
954 }
955
956 static int get_ppgtt(struct drm_i915_file_private *file_priv,
957                      struct i915_gem_context *ctx,
958                      struct drm_i915_gem_context_param *args)
959 {
960         struct i915_address_space *vm;
961         int ret;
962
963         if (!ctx->vm)
964                 return -ENODEV;
965
966         /* XXX rcu acquire? */
967         ret = mutex_lock_interruptible(&ctx->i915->drm.struct_mutex);
968         if (ret)
969                 return ret;
970
971         vm = i915_vm_get(ctx->vm);
972         mutex_unlock(&ctx->i915->drm.struct_mutex);
973
974         ret = mutex_lock_interruptible(&file_priv->vm_idr_lock);
975         if (ret)
976                 goto err_put;
977
978         ret = idr_alloc(&file_priv->vm_idr, vm, 0, 0, GFP_KERNEL);
979         GEM_BUG_ON(!ret);
980         if (ret < 0)
981                 goto err_unlock;
982
983         i915_vm_get(vm);
984
985         args->size = 0;
986         args->value = ret;
987
988         ret = 0;
989 err_unlock:
990         mutex_unlock(&file_priv->vm_idr_lock);
991 err_put:
992         i915_vm_put(vm);
993         return ret;
994 }
995
996 static void set_ppgtt_barrier(void *data)
997 {
998         struct i915_address_space *old = data;
999
1000         if (INTEL_GEN(old->i915) < 8)
1001                 gen6_ppgtt_unpin_all(i915_vm_to_ppgtt(old));
1002
1003         i915_vm_put(old);
1004 }
1005
1006 static int emit_ppgtt_update(struct i915_request *rq, void *data)
1007 {
1008         struct i915_address_space *vm = rq->gem_context->vm;
1009         struct intel_engine_cs *engine = rq->engine;
1010         u32 base = engine->mmio_base;
1011         u32 *cs;
1012         int i;
1013
1014         if (i915_vm_is_4lvl(vm)) {
1015                 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1016                 const dma_addr_t pd_daddr = px_dma(ppgtt->pd);
1017
1018                 cs = intel_ring_begin(rq, 6);
1019                 if (IS_ERR(cs))
1020                         return PTR_ERR(cs);
1021
1022                 *cs++ = MI_LOAD_REGISTER_IMM(2);
1023
1024                 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, 0));
1025                 *cs++ = upper_32_bits(pd_daddr);
1026                 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, 0));
1027                 *cs++ = lower_32_bits(pd_daddr);
1028
1029                 *cs++ = MI_NOOP;
1030                 intel_ring_advance(rq, cs);
1031         } else if (HAS_LOGICAL_RING_CONTEXTS(engine->i915)) {
1032                 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1033
1034                 cs = intel_ring_begin(rq, 4 * GEN8_3LVL_PDPES + 2);
1035                 if (IS_ERR(cs))
1036                         return PTR_ERR(cs);
1037
1038                 *cs++ = MI_LOAD_REGISTER_IMM(2 * GEN8_3LVL_PDPES);
1039                 for (i = GEN8_3LVL_PDPES; i--; ) {
1040                         const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1041
1042                         *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, i));
1043                         *cs++ = upper_32_bits(pd_daddr);
1044                         *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, i));
1045                         *cs++ = lower_32_bits(pd_daddr);
1046                 }
1047                 *cs++ = MI_NOOP;
1048                 intel_ring_advance(rq, cs);
1049         } else {
1050                 /* ppGTT is not part of the legacy context image */
1051                 gen6_ppgtt_pin(i915_vm_to_ppgtt(vm));
1052         }
1053
1054         return 0;
1055 }
1056
1057 static bool skip_ppgtt_update(struct intel_context *ce, void *data)
1058 {
1059         if (HAS_LOGICAL_RING_CONTEXTS(ce->engine->i915))
1060                 return !ce->state;
1061         else
1062                 return !atomic_read(&ce->pin_count);
1063 }
1064
1065 static int set_ppgtt(struct drm_i915_file_private *file_priv,
1066                      struct i915_gem_context *ctx,
1067                      struct drm_i915_gem_context_param *args)
1068 {
1069         struct i915_address_space *vm, *old;
1070         int err;
1071
1072         if (args->size)
1073                 return -EINVAL;
1074
1075         if (!ctx->vm)
1076                 return -ENODEV;
1077
1078         if (upper_32_bits(args->value))
1079                 return -ENOENT;
1080
1081         err = mutex_lock_interruptible(&file_priv->vm_idr_lock);
1082         if (err)
1083                 return err;
1084
1085         vm = idr_find(&file_priv->vm_idr, args->value);
1086         if (vm)
1087                 i915_vm_get(vm);
1088         mutex_unlock(&file_priv->vm_idr_lock);
1089         if (!vm)
1090                 return -ENOENT;
1091
1092         err = mutex_lock_interruptible(&ctx->i915->drm.struct_mutex);
1093         if (err)
1094                 goto out;
1095
1096         if (vm == ctx->vm)
1097                 goto unlock;
1098
1099         /* Teardown the existing obj:vma cache, it will have to be rebuilt. */
1100         mutex_lock(&ctx->mutex);
1101         lut_close(ctx);
1102         mutex_unlock(&ctx->mutex);
1103
1104         old = __set_ppgtt(ctx, vm);
1105
1106         /*
1107          * We need to flush any requests using the current ppgtt before
1108          * we release it as the requests do not hold a reference themselves,
1109          * only indirectly through the context.
1110          */
1111         err = context_barrier_task(ctx, ALL_ENGINES,
1112                                    skip_ppgtt_update,
1113                                    emit_ppgtt_update,
1114                                    set_ppgtt_barrier,
1115                                    old);
1116         if (err) {
1117                 ctx->vm = old;
1118                 ctx->desc_template = default_desc_template(ctx->i915, old);
1119                 i915_vm_put(vm);
1120         }
1121
1122 unlock:
1123         mutex_unlock(&ctx->i915->drm.struct_mutex);
1124
1125 out:
1126         i915_vm_put(vm);
1127         return err;
1128 }
1129
1130 static int gen8_emit_rpcs_config(struct i915_request *rq,
1131                                  struct intel_context *ce,
1132                                  struct intel_sseu sseu)
1133 {
1134         u64 offset;
1135         u32 *cs;
1136
1137         cs = intel_ring_begin(rq, 4);
1138         if (IS_ERR(cs))
1139                 return PTR_ERR(cs);
1140
1141         offset = i915_ggtt_offset(ce->state) +
1142                  LRC_STATE_PN * PAGE_SIZE +
1143                  (CTX_R_PWR_CLK_STATE + 1) * 4;
1144
1145         *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1146         *cs++ = lower_32_bits(offset);
1147         *cs++ = upper_32_bits(offset);
1148         *cs++ = intel_sseu_make_rpcs(rq->i915, &sseu);
1149
1150         intel_ring_advance(rq, cs);
1151
1152         return 0;
1153 }
1154
1155 static int
1156 gen8_modify_rpcs(struct intel_context *ce, struct intel_sseu sseu)
1157 {
1158         struct i915_request *rq;
1159         int ret;
1160
1161         lockdep_assert_held(&ce->pin_mutex);
1162
1163         /*
1164          * If the context is not idle, we have to submit an ordered request to
1165          * modify its context image via the kernel context (writing to our own
1166          * image, or into the registers directory, does not stick). Pristine
1167          * and idle contexts will be configured on pinning.
1168          */
1169         if (!intel_context_is_pinned(ce))
1170                 return 0;
1171
1172         rq = i915_request_create(ce->engine->kernel_context);
1173         if (IS_ERR(rq))
1174                 return PTR_ERR(rq);
1175
1176         /* Serialise with the remote context */
1177         ret = intel_context_prepare_remote_request(ce, rq);
1178         if (ret == 0)
1179                 ret = gen8_emit_rpcs_config(rq, ce, sseu);
1180
1181         i915_request_add(rq);
1182         return ret;
1183 }
1184
1185 static int
1186 __intel_context_reconfigure_sseu(struct intel_context *ce,
1187                                  struct intel_sseu sseu)
1188 {
1189         int ret;
1190
1191         GEM_BUG_ON(INTEL_GEN(ce->gem_context->i915) < 8);
1192
1193         ret = intel_context_lock_pinned(ce);
1194         if (ret)
1195                 return ret;
1196
1197         /* Nothing to do if unmodified. */
1198         if (!memcmp(&ce->sseu, &sseu, sizeof(sseu)))
1199                 goto unlock;
1200
1201         ret = gen8_modify_rpcs(ce, sseu);
1202         if (!ret)
1203                 ce->sseu = sseu;
1204
1205 unlock:
1206         intel_context_unlock_pinned(ce);
1207         return ret;
1208 }
1209
1210 static int
1211 intel_context_reconfigure_sseu(struct intel_context *ce, struct intel_sseu sseu)
1212 {
1213         struct drm_i915_private *i915 = ce->gem_context->i915;
1214         int ret;
1215
1216         ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1217         if (ret)
1218                 return ret;
1219
1220         ret = __intel_context_reconfigure_sseu(ce, sseu);
1221
1222         mutex_unlock(&i915->drm.struct_mutex);
1223
1224         return ret;
1225 }
1226
1227 static int
1228 user_to_context_sseu(struct drm_i915_private *i915,
1229                      const struct drm_i915_gem_context_param_sseu *user,
1230                      struct intel_sseu *context)
1231 {
1232         const struct sseu_dev_info *device = &RUNTIME_INFO(i915)->sseu;
1233
1234         /* No zeros in any field. */
1235         if (!user->slice_mask || !user->subslice_mask ||
1236             !user->min_eus_per_subslice || !user->max_eus_per_subslice)
1237                 return -EINVAL;
1238
1239         /* Max > min. */
1240         if (user->max_eus_per_subslice < user->min_eus_per_subslice)
1241                 return -EINVAL;
1242
1243         /*
1244          * Some future proofing on the types since the uAPI is wider than the
1245          * current internal implementation.
1246          */
1247         if (overflows_type(user->slice_mask, context->slice_mask) ||
1248             overflows_type(user->subslice_mask, context->subslice_mask) ||
1249             overflows_type(user->min_eus_per_subslice,
1250                            context->min_eus_per_subslice) ||
1251             overflows_type(user->max_eus_per_subslice,
1252                            context->max_eus_per_subslice))
1253                 return -EINVAL;
1254
1255         /* Check validity against hardware. */
1256         if (user->slice_mask & ~device->slice_mask)
1257                 return -EINVAL;
1258
1259         if (user->subslice_mask & ~device->subslice_mask[0])
1260                 return -EINVAL;
1261
1262         if (user->max_eus_per_subslice > device->max_eus_per_subslice)
1263                 return -EINVAL;
1264
1265         context->slice_mask = user->slice_mask;
1266         context->subslice_mask = user->subslice_mask;
1267         context->min_eus_per_subslice = user->min_eus_per_subslice;
1268         context->max_eus_per_subslice = user->max_eus_per_subslice;
1269
1270         /* Part specific restrictions. */
1271         if (IS_GEN(i915, 11)) {
1272                 unsigned int hw_s = hweight8(device->slice_mask);
1273                 unsigned int hw_ss_per_s = hweight8(device->subslice_mask[0]);
1274                 unsigned int req_s = hweight8(context->slice_mask);
1275                 unsigned int req_ss = hweight8(context->subslice_mask);
1276
1277                 /*
1278                  * Only full subslice enablement is possible if more than one
1279                  * slice is turned on.
1280                  */
1281                 if (req_s > 1 && req_ss != hw_ss_per_s)
1282                         return -EINVAL;
1283
1284                 /*
1285                  * If more than four (SScount bitfield limit) subslices are
1286                  * requested then the number has to be even.
1287                  */
1288                 if (req_ss > 4 && (req_ss & 1))
1289                         return -EINVAL;
1290
1291                 /*
1292                  * If only one slice is enabled and subslice count is below the
1293                  * device full enablement, it must be at most half of the all
1294                  * available subslices.
1295                  */
1296                 if (req_s == 1 && req_ss < hw_ss_per_s &&
1297                     req_ss > (hw_ss_per_s / 2))
1298                         return -EINVAL;
1299
1300                 /* ABI restriction - VME use case only. */
1301
1302                 /* All slices or one slice only. */
1303                 if (req_s != 1 && req_s != hw_s)
1304                         return -EINVAL;
1305
1306                 /*
1307                  * Half subslices or full enablement only when one slice is
1308                  * enabled.
1309                  */
1310                 if (req_s == 1 &&
1311                     (req_ss != hw_ss_per_s && req_ss != (hw_ss_per_s / 2)))
1312                         return -EINVAL;
1313
1314                 /* No EU configuration changes. */
1315                 if ((user->min_eus_per_subslice !=
1316                      device->max_eus_per_subslice) ||
1317                     (user->max_eus_per_subslice !=
1318                      device->max_eus_per_subslice))
1319                         return -EINVAL;
1320         }
1321
1322         return 0;
1323 }
1324
1325 static int set_sseu(struct i915_gem_context *ctx,
1326                     struct drm_i915_gem_context_param *args)
1327 {
1328         struct drm_i915_private *i915 = ctx->i915;
1329         struct drm_i915_gem_context_param_sseu user_sseu;
1330         struct intel_context *ce;
1331         struct intel_sseu sseu;
1332         unsigned long lookup;
1333         int ret;
1334
1335         if (args->size < sizeof(user_sseu))
1336                 return -EINVAL;
1337
1338         if (!IS_GEN(i915, 11))
1339                 return -ENODEV;
1340
1341         if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
1342                            sizeof(user_sseu)))
1343                 return -EFAULT;
1344
1345         if (user_sseu.rsvd)
1346                 return -EINVAL;
1347
1348         if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
1349                 return -EINVAL;
1350
1351         lookup = 0;
1352         if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
1353                 lookup |= LOOKUP_USER_INDEX;
1354
1355         ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
1356         if (IS_ERR(ce))
1357                 return PTR_ERR(ce);
1358
1359         /* Only render engine supports RPCS configuration. */
1360         if (ce->engine->class != RENDER_CLASS) {
1361                 ret = -ENODEV;
1362                 goto out_ce;
1363         }
1364
1365         ret = user_to_context_sseu(i915, &user_sseu, &sseu);
1366         if (ret)
1367                 goto out_ce;
1368
1369         ret = intel_context_reconfigure_sseu(ce, sseu);
1370         if (ret)
1371                 goto out_ce;
1372
1373         args->size = sizeof(user_sseu);
1374
1375 out_ce:
1376         intel_context_put(ce);
1377         return ret;
1378 }
1379
1380 struct set_engines {
1381         struct i915_gem_context *ctx;
1382         struct i915_gem_engines *engines;
1383 };
1384
1385 static int
1386 set_engines__load_balance(struct i915_user_extension __user *base, void *data)
1387 {
1388         struct i915_context_engines_load_balance __user *ext =
1389                 container_of_user(base, typeof(*ext), base);
1390         const struct set_engines *set = data;
1391         struct intel_engine_cs *stack[16];
1392         struct intel_engine_cs **siblings;
1393         struct intel_context *ce;
1394         u16 num_siblings, idx;
1395         unsigned int n;
1396         int err;
1397
1398         if (!HAS_EXECLISTS(set->ctx->i915))
1399                 return -ENODEV;
1400
1401         if (USES_GUC_SUBMISSION(set->ctx->i915))
1402                 return -ENODEV; /* not implement yet */
1403
1404         if (get_user(idx, &ext->engine_index))
1405                 return -EFAULT;
1406
1407         if (idx >= set->engines->num_engines) {
1408                 DRM_DEBUG("Invalid placement value, %d >= %d\n",
1409                           idx, set->engines->num_engines);
1410                 return -EINVAL;
1411         }
1412
1413         idx = array_index_nospec(idx, set->engines->num_engines);
1414         if (set->engines->engines[idx]) {
1415                 DRM_DEBUG("Invalid placement[%d], already occupied\n", idx);
1416                 return -EEXIST;
1417         }
1418
1419         if (get_user(num_siblings, &ext->num_siblings))
1420                 return -EFAULT;
1421
1422         err = check_user_mbz(&ext->flags);
1423         if (err)
1424                 return err;
1425
1426         err = check_user_mbz(&ext->mbz64);
1427         if (err)
1428                 return err;
1429
1430         siblings = stack;
1431         if (num_siblings > ARRAY_SIZE(stack)) {
1432                 siblings = kmalloc_array(num_siblings,
1433                                          sizeof(*siblings),
1434                                          GFP_KERNEL);
1435                 if (!siblings)
1436                         return -ENOMEM;
1437         }
1438
1439         for (n = 0; n < num_siblings; n++) {
1440                 struct i915_engine_class_instance ci;
1441
1442                 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) {
1443                         err = -EFAULT;
1444                         goto out_siblings;
1445                 }
1446
1447                 siblings[n] = intel_engine_lookup_user(set->ctx->i915,
1448                                                        ci.engine_class,
1449                                                        ci.engine_instance);
1450                 if (!siblings[n]) {
1451                         DRM_DEBUG("Invalid sibling[%d]: { class:%d, inst:%d }\n",
1452                                   n, ci.engine_class, ci.engine_instance);
1453                         err = -EINVAL;
1454                         goto out_siblings;
1455                 }
1456         }
1457
1458         ce = intel_execlists_create_virtual(set->ctx, siblings, n);
1459         if (IS_ERR(ce)) {
1460                 err = PTR_ERR(ce);
1461                 goto out_siblings;
1462         }
1463
1464         if (cmpxchg(&set->engines->engines[idx], NULL, ce)) {
1465                 intel_context_put(ce);
1466                 err = -EEXIST;
1467                 goto out_siblings;
1468         }
1469
1470 out_siblings:
1471         if (siblings != stack)
1472                 kfree(siblings);
1473
1474         return err;
1475 }
1476
1477 static int
1478 set_engines__bond(struct i915_user_extension __user *base, void *data)
1479 {
1480         struct i915_context_engines_bond __user *ext =
1481                 container_of_user(base, typeof(*ext), base);
1482         const struct set_engines *set = data;
1483         struct i915_engine_class_instance ci;
1484         struct intel_engine_cs *virtual;
1485         struct intel_engine_cs *master;
1486         u16 idx, num_bonds;
1487         int err, n;
1488
1489         if (get_user(idx, &ext->virtual_index))
1490                 return -EFAULT;
1491
1492         if (idx >= set->engines->num_engines) {
1493                 DRM_DEBUG("Invalid index for virtual engine: %d >= %d\n",
1494                           idx, set->engines->num_engines);
1495                 return -EINVAL;
1496         }
1497
1498         idx = array_index_nospec(idx, set->engines->num_engines);
1499         if (!set->engines->engines[idx]) {
1500                 DRM_DEBUG("Invalid engine at %d\n", idx);
1501                 return -EINVAL;
1502         }
1503         virtual = set->engines->engines[idx]->engine;
1504
1505         err = check_user_mbz(&ext->flags);
1506         if (err)
1507                 return err;
1508
1509         for (n = 0; n < ARRAY_SIZE(ext->mbz64); n++) {
1510                 err = check_user_mbz(&ext->mbz64[n]);
1511                 if (err)
1512                         return err;
1513         }
1514
1515         if (copy_from_user(&ci, &ext->master, sizeof(ci)))
1516                 return -EFAULT;
1517
1518         master = intel_engine_lookup_user(set->ctx->i915,
1519                                           ci.engine_class, ci.engine_instance);
1520         if (!master) {
1521                 DRM_DEBUG("Unrecognised master engine: { class:%u, instance:%u }\n",
1522                           ci.engine_class, ci.engine_instance);
1523                 return -EINVAL;
1524         }
1525
1526         if (get_user(num_bonds, &ext->num_bonds))
1527                 return -EFAULT;
1528
1529         for (n = 0; n < num_bonds; n++) {
1530                 struct intel_engine_cs *bond;
1531
1532                 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci)))
1533                         return -EFAULT;
1534
1535                 bond = intel_engine_lookup_user(set->ctx->i915,
1536                                                 ci.engine_class,
1537                                                 ci.engine_instance);
1538                 if (!bond) {
1539                         DRM_DEBUG("Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n",
1540                                   n, ci.engine_class, ci.engine_instance);
1541                         return -EINVAL;
1542                 }
1543
1544                 /*
1545                  * A non-virtual engine has no siblings to choose between; and
1546                  * a submit fence will always be directed to the one engine.
1547                  */
1548                 if (intel_engine_is_virtual(virtual)) {
1549                         err = intel_virtual_engine_attach_bond(virtual,
1550                                                                master,
1551                                                                bond);
1552                         if (err)
1553                                 return err;
1554                 }
1555         }
1556
1557         return 0;
1558 }
1559
1560 static const i915_user_extension_fn set_engines__extensions[] = {
1561         [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE] = set_engines__load_balance,
1562         [I915_CONTEXT_ENGINES_EXT_BOND] = set_engines__bond,
1563 };
1564
1565 static int
1566 set_engines(struct i915_gem_context *ctx,
1567             const struct drm_i915_gem_context_param *args)
1568 {
1569         struct i915_context_param_engines __user *user =
1570                 u64_to_user_ptr(args->value);
1571         struct set_engines set = { .ctx = ctx };
1572         unsigned int num_engines, n;
1573         u64 extensions;
1574         int err;
1575
1576         if (!args->size) { /* switch back to legacy user_ring_map */
1577                 if (!i915_gem_context_user_engines(ctx))
1578                         return 0;
1579
1580                 set.engines = default_engines(ctx);
1581                 if (IS_ERR(set.engines))
1582                         return PTR_ERR(set.engines);
1583
1584                 goto replace;
1585         }
1586
1587         BUILD_BUG_ON(!IS_ALIGNED(sizeof(*user), sizeof(*user->engines)));
1588         if (args->size < sizeof(*user) ||
1589             !IS_ALIGNED(args->size, sizeof(*user->engines))) {
1590                 DRM_DEBUG("Invalid size for engine array: %d\n",
1591                           args->size);
1592                 return -EINVAL;
1593         }
1594
1595         /*
1596          * Note that I915_EXEC_RING_MASK limits execbuf to only using the
1597          * first 64 engines defined here.
1598          */
1599         num_engines = (args->size - sizeof(*user)) / sizeof(*user->engines);
1600
1601         set.engines = kmalloc(struct_size(set.engines, engines, num_engines),
1602                               GFP_KERNEL);
1603         if (!set.engines)
1604                 return -ENOMEM;
1605
1606         init_rcu_head(&set.engines->rcu);
1607         for (n = 0; n < num_engines; n++) {
1608                 struct i915_engine_class_instance ci;
1609                 struct intel_engine_cs *engine;
1610
1611                 if (copy_from_user(&ci, &user->engines[n], sizeof(ci))) {
1612                         __free_engines(set.engines, n);
1613                         return -EFAULT;
1614                 }
1615
1616                 if (ci.engine_class == (u16)I915_ENGINE_CLASS_INVALID &&
1617                     ci.engine_instance == (u16)I915_ENGINE_CLASS_INVALID_NONE) {
1618                         set.engines->engines[n] = NULL;
1619                         continue;
1620                 }
1621
1622                 engine = intel_engine_lookup_user(ctx->i915,
1623                                                   ci.engine_class,
1624                                                   ci.engine_instance);
1625                 if (!engine) {
1626                         DRM_DEBUG("Invalid engine[%d]: { class:%d, instance:%d }\n",
1627                                   n, ci.engine_class, ci.engine_instance);
1628                         __free_engines(set.engines, n);
1629                         return -ENOENT;
1630                 }
1631
1632                 set.engines->engines[n] = intel_context_create(ctx, engine);
1633                 if (!set.engines->engines[n]) {
1634                         __free_engines(set.engines, n);
1635                         return -ENOMEM;
1636                 }
1637         }
1638         set.engines->num_engines = num_engines;
1639
1640         err = -EFAULT;
1641         if (!get_user(extensions, &user->extensions))
1642                 err = i915_user_extensions(u64_to_user_ptr(extensions),
1643                                            set_engines__extensions,
1644                                            ARRAY_SIZE(set_engines__extensions),
1645                                            &set);
1646         if (err) {
1647                 free_engines(set.engines);
1648                 return err;
1649         }
1650
1651 replace:
1652         mutex_lock(&ctx->engines_mutex);
1653         if (args->size)
1654                 i915_gem_context_set_user_engines(ctx);
1655         else
1656                 i915_gem_context_clear_user_engines(ctx);
1657         rcu_swap_protected(ctx->engines, set.engines, 1);
1658         mutex_unlock(&ctx->engines_mutex);
1659
1660         call_rcu(&set.engines->rcu, free_engines_rcu);
1661
1662         return 0;
1663 }
1664
1665 static struct i915_gem_engines *
1666 __copy_engines(struct i915_gem_engines *e)
1667 {
1668         struct i915_gem_engines *copy;
1669         unsigned int n;
1670
1671         copy = kmalloc(struct_size(e, engines, e->num_engines), GFP_KERNEL);
1672         if (!copy)
1673                 return ERR_PTR(-ENOMEM);
1674
1675         init_rcu_head(&copy->rcu);
1676         for (n = 0; n < e->num_engines; n++) {
1677                 if (e->engines[n])
1678                         copy->engines[n] = intel_context_get(e->engines[n]);
1679                 else
1680                         copy->engines[n] = NULL;
1681         }
1682         copy->num_engines = n;
1683
1684         return copy;
1685 }
1686
1687 static int
1688 get_engines(struct i915_gem_context *ctx,
1689             struct drm_i915_gem_context_param *args)
1690 {
1691         struct i915_context_param_engines __user *user;
1692         struct i915_gem_engines *e;
1693         size_t n, count, size;
1694         int err = 0;
1695
1696         err = mutex_lock_interruptible(&ctx->engines_mutex);
1697         if (err)
1698                 return err;
1699
1700         e = NULL;
1701         if (i915_gem_context_user_engines(ctx))
1702                 e = __copy_engines(i915_gem_context_engines(ctx));
1703         mutex_unlock(&ctx->engines_mutex);
1704         if (IS_ERR_OR_NULL(e)) {
1705                 args->size = 0;
1706                 return PTR_ERR_OR_ZERO(e);
1707         }
1708
1709         count = e->num_engines;
1710
1711         /* Be paranoid in case we have an impedance mismatch */
1712         if (!check_struct_size(user, engines, count, &size)) {
1713                 err = -EINVAL;
1714                 goto err_free;
1715         }
1716         if (overflows_type(size, args->size)) {
1717                 err = -EINVAL;
1718                 goto err_free;
1719         }
1720
1721         if (!args->size) {
1722                 args->size = size;
1723                 goto err_free;
1724         }
1725
1726         if (args->size < size) {
1727                 err = -EINVAL;
1728                 goto err_free;
1729         }
1730
1731         user = u64_to_user_ptr(args->value);
1732         if (!access_ok(user, size)) {
1733                 err = -EFAULT;
1734                 goto err_free;
1735         }
1736
1737         if (put_user(0, &user->extensions)) {
1738                 err = -EFAULT;
1739                 goto err_free;
1740         }
1741
1742         for (n = 0; n < count; n++) {
1743                 struct i915_engine_class_instance ci = {
1744                         .engine_class = I915_ENGINE_CLASS_INVALID,
1745                         .engine_instance = I915_ENGINE_CLASS_INVALID_NONE,
1746                 };
1747
1748                 if (e->engines[n]) {
1749                         ci.engine_class = e->engines[n]->engine->uabi_class;
1750                         ci.engine_instance = e->engines[n]->engine->instance;
1751                 }
1752
1753                 if (copy_to_user(&user->engines[n], &ci, sizeof(ci))) {
1754                         err = -EFAULT;
1755                         goto err_free;
1756                 }
1757         }
1758
1759         args->size = size;
1760
1761 err_free:
1762         free_engines(e);
1763         return err;
1764 }
1765
1766 static int ctx_setparam(struct drm_i915_file_private *fpriv,
1767                         struct i915_gem_context *ctx,
1768                         struct drm_i915_gem_context_param *args)
1769 {
1770         int ret = 0;
1771
1772         switch (args->param) {
1773         case I915_CONTEXT_PARAM_NO_ZEROMAP:
1774                 if (args->size)
1775                         ret = -EINVAL;
1776                 else if (args->value)
1777                         set_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
1778                 else
1779                         clear_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
1780                 break;
1781
1782         case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
1783                 if (args->size)
1784                         ret = -EINVAL;
1785                 else if (args->value)
1786                         i915_gem_context_set_no_error_capture(ctx);
1787                 else
1788                         i915_gem_context_clear_no_error_capture(ctx);
1789                 break;
1790
1791         case I915_CONTEXT_PARAM_BANNABLE:
1792                 if (args->size)
1793                         ret = -EINVAL;
1794                 else if (!capable(CAP_SYS_ADMIN) && !args->value)
1795                         ret = -EPERM;
1796                 else if (args->value)
1797                         i915_gem_context_set_bannable(ctx);
1798                 else
1799                         i915_gem_context_clear_bannable(ctx);
1800                 break;
1801
1802         case I915_CONTEXT_PARAM_RECOVERABLE:
1803                 if (args->size)
1804                         ret = -EINVAL;
1805                 else if (args->value)
1806                         i915_gem_context_set_recoverable(ctx);
1807                 else
1808                         i915_gem_context_clear_recoverable(ctx);
1809                 break;
1810
1811         case I915_CONTEXT_PARAM_PRIORITY:
1812                 {
1813                         s64 priority = args->value;
1814
1815                         if (args->size)
1816                                 ret = -EINVAL;
1817                         else if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PRIORITY))
1818                                 ret = -ENODEV;
1819                         else if (priority > I915_CONTEXT_MAX_USER_PRIORITY ||
1820                                  priority < I915_CONTEXT_MIN_USER_PRIORITY)
1821                                 ret = -EINVAL;
1822                         else if (priority > I915_CONTEXT_DEFAULT_PRIORITY &&
1823                                  !capable(CAP_SYS_NICE))
1824                                 ret = -EPERM;
1825                         else
1826                                 ctx->sched.priority =
1827                                         I915_USER_PRIORITY(priority);
1828                 }
1829                 break;
1830
1831         case I915_CONTEXT_PARAM_SSEU:
1832                 ret = set_sseu(ctx, args);
1833                 break;
1834
1835         case I915_CONTEXT_PARAM_VM:
1836                 ret = set_ppgtt(fpriv, ctx, args);
1837                 break;
1838
1839         case I915_CONTEXT_PARAM_ENGINES:
1840                 ret = set_engines(ctx, args);
1841                 break;
1842
1843         case I915_CONTEXT_PARAM_BAN_PERIOD:
1844         default:
1845                 ret = -EINVAL;
1846                 break;
1847         }
1848
1849         return ret;
1850 }
1851
1852 struct create_ext {
1853         struct i915_gem_context *ctx;
1854         struct drm_i915_file_private *fpriv;
1855 };
1856
1857 static int create_setparam(struct i915_user_extension __user *ext, void *data)
1858 {
1859         struct drm_i915_gem_context_create_ext_setparam local;
1860         const struct create_ext *arg = data;
1861
1862         if (copy_from_user(&local, ext, sizeof(local)))
1863                 return -EFAULT;
1864
1865         if (local.param.ctx_id)
1866                 return -EINVAL;
1867
1868         return ctx_setparam(arg->fpriv, arg->ctx, &local.param);
1869 }
1870
1871 static int clone_engines(struct i915_gem_context *dst,
1872                          struct i915_gem_context *src)
1873 {
1874         struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1875         struct i915_gem_engines *clone;
1876         bool user_engines;
1877         unsigned long n;
1878
1879         clone = kmalloc(struct_size(e, engines, e->num_engines), GFP_KERNEL);
1880         if (!clone)
1881                 goto err_unlock;
1882
1883         init_rcu_head(&clone->rcu);
1884         for (n = 0; n < e->num_engines; n++) {
1885                 struct intel_engine_cs *engine;
1886
1887                 if (!e->engines[n]) {
1888                         clone->engines[n] = NULL;
1889                         continue;
1890                 }
1891                 engine = e->engines[n]->engine;
1892
1893                 /*
1894                  * Virtual engines are singletons; they can only exist
1895                  * inside a single context, because they embed their
1896                  * HW context... As each virtual context implies a single
1897                  * timeline (each engine can only dequeue a single request
1898                  * at any time), it would be surprising for two contexts
1899                  * to use the same engine. So let's create a copy of
1900                  * the virtual engine instead.
1901                  */
1902                 if (intel_engine_is_virtual(engine))
1903                         clone->engines[n] =
1904                                 intel_execlists_clone_virtual(dst, engine);
1905                 else
1906                         clone->engines[n] = intel_context_create(dst, engine);
1907                 if (IS_ERR_OR_NULL(clone->engines[n])) {
1908                         __free_engines(clone, n);
1909                         goto err_unlock;
1910                 }
1911         }
1912         clone->num_engines = n;
1913
1914         user_engines = i915_gem_context_user_engines(src);
1915         i915_gem_context_unlock_engines(src);
1916
1917         free_engines(dst->engines);
1918         RCU_INIT_POINTER(dst->engines, clone);
1919         if (user_engines)
1920                 i915_gem_context_set_user_engines(dst);
1921         else
1922                 i915_gem_context_clear_user_engines(dst);
1923         return 0;
1924
1925 err_unlock:
1926         i915_gem_context_unlock_engines(src);
1927         return -ENOMEM;
1928 }
1929
1930 static int clone_flags(struct i915_gem_context *dst,
1931                        struct i915_gem_context *src)
1932 {
1933         dst->user_flags = src->user_flags;
1934         return 0;
1935 }
1936
1937 static int clone_schedattr(struct i915_gem_context *dst,
1938                            struct i915_gem_context *src)
1939 {
1940         dst->sched = src->sched;
1941         return 0;
1942 }
1943
1944 static int clone_sseu(struct i915_gem_context *dst,
1945                       struct i915_gem_context *src)
1946 {
1947         struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1948         struct i915_gem_engines *clone;
1949         unsigned long n;
1950         int err;
1951
1952         clone = dst->engines; /* no locking required; sole access */
1953         if (e->num_engines != clone->num_engines) {
1954                 err = -EINVAL;
1955                 goto unlock;
1956         }
1957
1958         for (n = 0; n < e->num_engines; n++) {
1959                 struct intel_context *ce = e->engines[n];
1960
1961                 if (clone->engines[n]->engine->class != ce->engine->class) {
1962                         /* Must have compatible engine maps! */
1963                         err = -EINVAL;
1964                         goto unlock;
1965                 }
1966
1967                 /* serialises with set_sseu */
1968                 err = intel_context_lock_pinned(ce);
1969                 if (err)
1970                         goto unlock;
1971
1972                 clone->engines[n]->sseu = ce->sseu;
1973                 intel_context_unlock_pinned(ce);
1974         }
1975
1976         err = 0;
1977 unlock:
1978         i915_gem_context_unlock_engines(src);
1979         return err;
1980 }
1981
1982 static int clone_timeline(struct i915_gem_context *dst,
1983                           struct i915_gem_context *src)
1984 {
1985         if (src->timeline) {
1986                 GEM_BUG_ON(src->timeline == dst->timeline);
1987
1988                 if (dst->timeline)
1989                         intel_timeline_put(dst->timeline);
1990                 dst->timeline = intel_timeline_get(src->timeline);
1991         }
1992
1993         return 0;
1994 }
1995
1996 static int clone_vm(struct i915_gem_context *dst,
1997                     struct i915_gem_context *src)
1998 {
1999         struct i915_address_space *vm;
2000
2001         rcu_read_lock();
2002         do {
2003                 vm = READ_ONCE(src->vm);
2004                 if (!vm)
2005                         break;
2006
2007                 if (!kref_get_unless_zero(&vm->ref))
2008                         continue;
2009
2010                 /*
2011                  * This ppgtt may have be reallocated between
2012                  * the read and the kref, and reassigned to a third
2013                  * context. In order to avoid inadvertent sharing
2014                  * of this ppgtt with that third context (and not
2015                  * src), we have to confirm that we have the same
2016                  * ppgtt after passing through the strong memory
2017                  * barrier implied by a successful
2018                  * kref_get_unless_zero().
2019                  *
2020                  * Once we have acquired the current ppgtt of src,
2021                  * we no longer care if it is released from src, as
2022                  * it cannot be reallocated elsewhere.
2023                  */
2024
2025                 if (vm == READ_ONCE(src->vm))
2026                         break;
2027
2028                 i915_vm_put(vm);
2029         } while (1);
2030         rcu_read_unlock();
2031
2032         if (vm) {
2033                 __assign_ppgtt(dst, vm);
2034                 i915_vm_put(vm);
2035         }
2036
2037         return 0;
2038 }
2039
2040 static int create_clone(struct i915_user_extension __user *ext, void *data)
2041 {
2042         static int (* const fn[])(struct i915_gem_context *dst,
2043                                   struct i915_gem_context *src) = {
2044 #define MAP(x, y) [ilog2(I915_CONTEXT_CLONE_##x)] = y
2045                 MAP(ENGINES, clone_engines),
2046                 MAP(FLAGS, clone_flags),
2047                 MAP(SCHEDATTR, clone_schedattr),
2048                 MAP(SSEU, clone_sseu),
2049                 MAP(TIMELINE, clone_timeline),
2050                 MAP(VM, clone_vm),
2051 #undef MAP
2052         };
2053         struct drm_i915_gem_context_create_ext_clone local;
2054         const struct create_ext *arg = data;
2055         struct i915_gem_context *dst = arg->ctx;
2056         struct i915_gem_context *src;
2057         int err, bit;
2058
2059         if (copy_from_user(&local, ext, sizeof(local)))
2060                 return -EFAULT;
2061
2062         BUILD_BUG_ON(GENMASK(BITS_PER_TYPE(local.flags) - 1, ARRAY_SIZE(fn)) !=
2063                      I915_CONTEXT_CLONE_UNKNOWN);
2064
2065         if (local.flags & I915_CONTEXT_CLONE_UNKNOWN)
2066                 return -EINVAL;
2067
2068         if (local.rsvd)
2069                 return -EINVAL;
2070
2071         rcu_read_lock();
2072         src = __i915_gem_context_lookup_rcu(arg->fpriv, local.clone_id);
2073         rcu_read_unlock();
2074         if (!src)
2075                 return -ENOENT;
2076
2077         GEM_BUG_ON(src == dst);
2078
2079         for (bit = 0; bit < ARRAY_SIZE(fn); bit++) {
2080                 if (!(local.flags & BIT(bit)))
2081                         continue;
2082
2083                 err = fn[bit](dst, src);
2084                 if (err)
2085                         return err;
2086         }
2087
2088         return 0;
2089 }
2090
2091 static const i915_user_extension_fn create_extensions[] = {
2092         [I915_CONTEXT_CREATE_EXT_SETPARAM] = create_setparam,
2093         [I915_CONTEXT_CREATE_EXT_CLONE] = create_clone,
2094 };
2095
2096 static bool client_is_banned(struct drm_i915_file_private *file_priv)
2097 {
2098         return atomic_read(&file_priv->ban_score) >= I915_CLIENT_SCORE_BANNED;
2099 }
2100
2101 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
2102                                   struct drm_file *file)
2103 {
2104         struct drm_i915_private *i915 = to_i915(dev);
2105         struct drm_i915_gem_context_create_ext *args = data;
2106         struct create_ext ext_data;
2107         int ret;
2108
2109         if (!DRIVER_CAPS(i915)->has_logical_contexts)
2110                 return -ENODEV;
2111
2112         if (args->flags & I915_CONTEXT_CREATE_FLAGS_UNKNOWN)
2113                 return -EINVAL;
2114
2115         ret = intel_gt_terminally_wedged(&i915->gt);
2116         if (ret)
2117                 return ret;
2118
2119         ext_data.fpriv = file->driver_priv;
2120         if (client_is_banned(ext_data.fpriv)) {
2121                 DRM_DEBUG("client %s[%d] banned from creating ctx\n",
2122                           current->comm,
2123                           pid_nr(get_task_pid(current, PIDTYPE_PID)));
2124                 return -EIO;
2125         }
2126
2127         ret = i915_mutex_lock_interruptible(dev);
2128         if (ret)
2129                 return ret;
2130
2131         ext_data.ctx = i915_gem_create_context(i915, args->flags);
2132         mutex_unlock(&dev->struct_mutex);
2133         if (IS_ERR(ext_data.ctx))
2134                 return PTR_ERR(ext_data.ctx);
2135
2136         if (args->flags & I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS) {
2137                 ret = i915_user_extensions(u64_to_user_ptr(args->extensions),
2138                                            create_extensions,
2139                                            ARRAY_SIZE(create_extensions),
2140                                            &ext_data);
2141                 if (ret)
2142                         goto err_ctx;
2143         }
2144
2145         ret = gem_context_register(ext_data.ctx, ext_data.fpriv);
2146         if (ret < 0)
2147                 goto err_ctx;
2148
2149         args->ctx_id = ret;
2150         DRM_DEBUG("HW context %d created\n", args->ctx_id);
2151
2152         return 0;
2153
2154 err_ctx:
2155         context_close(ext_data.ctx);
2156         return ret;
2157 }
2158
2159 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
2160                                    struct drm_file *file)
2161 {
2162         struct drm_i915_gem_context_destroy *args = data;
2163         struct drm_i915_file_private *file_priv = file->driver_priv;
2164         struct i915_gem_context *ctx;
2165
2166         if (args->pad != 0)
2167                 return -EINVAL;
2168
2169         if (!args->ctx_id)
2170                 return -ENOENT;
2171
2172         if (mutex_lock_interruptible(&file_priv->context_idr_lock))
2173                 return -EINTR;
2174
2175         ctx = idr_remove(&file_priv->context_idr, args->ctx_id);
2176         mutex_unlock(&file_priv->context_idr_lock);
2177         if (!ctx)
2178                 return -ENOENT;
2179
2180         context_close(ctx);
2181         return 0;
2182 }
2183
2184 static int get_sseu(struct i915_gem_context *ctx,
2185                     struct drm_i915_gem_context_param *args)
2186 {
2187         struct drm_i915_gem_context_param_sseu user_sseu;
2188         struct intel_context *ce;
2189         unsigned long lookup;
2190         int err;
2191
2192         if (args->size == 0)
2193                 goto out;
2194         else if (args->size < sizeof(user_sseu))
2195                 return -EINVAL;
2196
2197         if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
2198                            sizeof(user_sseu)))
2199                 return -EFAULT;
2200
2201         if (user_sseu.rsvd)
2202                 return -EINVAL;
2203
2204         if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
2205                 return -EINVAL;
2206
2207         lookup = 0;
2208         if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
2209                 lookup |= LOOKUP_USER_INDEX;
2210
2211         ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
2212         if (IS_ERR(ce))
2213                 return PTR_ERR(ce);
2214
2215         err = intel_context_lock_pinned(ce); /* serialises with set_sseu */
2216         if (err) {
2217                 intel_context_put(ce);
2218                 return err;
2219         }
2220
2221         user_sseu.slice_mask = ce->sseu.slice_mask;
2222         user_sseu.subslice_mask = ce->sseu.subslice_mask;
2223         user_sseu.min_eus_per_subslice = ce->sseu.min_eus_per_subslice;
2224         user_sseu.max_eus_per_subslice = ce->sseu.max_eus_per_subslice;
2225
2226         intel_context_unlock_pinned(ce);
2227         intel_context_put(ce);
2228
2229         if (copy_to_user(u64_to_user_ptr(args->value), &user_sseu,
2230                          sizeof(user_sseu)))
2231                 return -EFAULT;
2232
2233 out:
2234         args->size = sizeof(user_sseu);
2235
2236         return 0;
2237 }
2238
2239 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
2240                                     struct drm_file *file)
2241 {
2242         struct drm_i915_file_private *file_priv = file->driver_priv;
2243         struct drm_i915_gem_context_param *args = data;
2244         struct i915_gem_context *ctx;
2245         int ret = 0;
2246
2247         ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2248         if (!ctx)
2249                 return -ENOENT;
2250
2251         switch (args->param) {
2252         case I915_CONTEXT_PARAM_NO_ZEROMAP:
2253                 args->size = 0;
2254                 args->value = test_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags);
2255                 break;
2256
2257         case I915_CONTEXT_PARAM_GTT_SIZE:
2258                 args->size = 0;
2259                 if (ctx->vm)
2260                         args->value = ctx->vm->total;
2261                 else if (to_i915(dev)->mm.aliasing_ppgtt)
2262                         args->value = to_i915(dev)->mm.aliasing_ppgtt->vm.total;
2263                 else
2264                         args->value = to_i915(dev)->ggtt.vm.total;
2265                 break;
2266
2267         case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
2268                 args->size = 0;
2269                 args->value = i915_gem_context_no_error_capture(ctx);
2270                 break;
2271
2272         case I915_CONTEXT_PARAM_BANNABLE:
2273                 args->size = 0;
2274                 args->value = i915_gem_context_is_bannable(ctx);
2275                 break;
2276
2277         case I915_CONTEXT_PARAM_RECOVERABLE:
2278                 args->size = 0;
2279                 args->value = i915_gem_context_is_recoverable(ctx);
2280                 break;
2281
2282         case I915_CONTEXT_PARAM_PRIORITY:
2283                 args->size = 0;
2284                 args->value = ctx->sched.priority >> I915_USER_PRIORITY_SHIFT;
2285                 break;
2286
2287         case I915_CONTEXT_PARAM_SSEU:
2288                 ret = get_sseu(ctx, args);
2289                 break;
2290
2291         case I915_CONTEXT_PARAM_VM:
2292                 ret = get_ppgtt(file_priv, ctx, args);
2293                 break;
2294
2295         case I915_CONTEXT_PARAM_ENGINES:
2296                 ret = get_engines(ctx, args);
2297                 break;
2298
2299         case I915_CONTEXT_PARAM_BAN_PERIOD:
2300         default:
2301                 ret = -EINVAL;
2302                 break;
2303         }
2304
2305         i915_gem_context_put(ctx);
2306         return ret;
2307 }
2308
2309 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
2310                                     struct drm_file *file)
2311 {
2312         struct drm_i915_file_private *file_priv = file->driver_priv;
2313         struct drm_i915_gem_context_param *args = data;
2314         struct i915_gem_context *ctx;
2315         int ret;
2316
2317         ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
2318         if (!ctx)
2319                 return -ENOENT;
2320
2321         ret = ctx_setparam(file_priv, ctx, args);
2322
2323         i915_gem_context_put(ctx);
2324         return ret;
2325 }
2326
2327 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
2328                                        void *data, struct drm_file *file)
2329 {
2330         struct drm_i915_private *dev_priv = to_i915(dev);
2331         struct drm_i915_reset_stats *args = data;
2332         struct i915_gem_context *ctx;
2333         int ret;
2334
2335         if (args->flags || args->pad)
2336                 return -EINVAL;
2337
2338         ret = -ENOENT;
2339         rcu_read_lock();
2340         ctx = __i915_gem_context_lookup_rcu(file->driver_priv, args->ctx_id);
2341         if (!ctx)
2342                 goto out;
2343
2344         /*
2345          * We opt for unserialised reads here. This may result in tearing
2346          * in the extremely unlikely event of a GPU hang on this context
2347          * as we are querying them. If we need that extra layer of protection,
2348          * we should wrap the hangstats with a seqlock.
2349          */
2350
2351         if (capable(CAP_SYS_ADMIN))
2352                 args->reset_count = i915_reset_count(&dev_priv->gpu_error);
2353         else
2354                 args->reset_count = 0;
2355
2356         args->batch_active = atomic_read(&ctx->guilty_count);
2357         args->batch_pending = atomic_read(&ctx->active_count);
2358
2359         ret = 0;
2360 out:
2361         rcu_read_unlock();
2362         return ret;
2363 }
2364
2365 int __i915_gem_context_pin_hw_id(struct i915_gem_context *ctx)
2366 {
2367         struct drm_i915_private *i915 = ctx->i915;
2368         int err = 0;
2369
2370         mutex_lock(&i915->contexts.mutex);
2371
2372         GEM_BUG_ON(i915_gem_context_is_closed(ctx));
2373
2374         if (list_empty(&ctx->hw_id_link)) {
2375                 GEM_BUG_ON(atomic_read(&ctx->hw_id_pin_count));
2376
2377                 err = assign_hw_id(i915, &ctx->hw_id);
2378                 if (err)
2379                         goto out_unlock;
2380
2381                 list_add_tail(&ctx->hw_id_link, &i915->contexts.hw_id_list);
2382         }
2383
2384         GEM_BUG_ON(atomic_read(&ctx->hw_id_pin_count) == ~0u);
2385         atomic_inc(&ctx->hw_id_pin_count);
2386
2387 out_unlock:
2388         mutex_unlock(&i915->contexts.mutex);
2389         return err;
2390 }
2391
2392 /* GEM context-engines iterator: for_each_gem_engine() */
2393 struct intel_context *
2394 i915_gem_engines_iter_next(struct i915_gem_engines_iter *it)
2395 {
2396         const struct i915_gem_engines *e = it->engines;
2397         struct intel_context *ctx;
2398
2399         do {
2400                 if (it->idx >= e->num_engines)
2401                         return NULL;
2402
2403                 ctx = e->engines[it->idx++];
2404         } while (!ctx);
2405
2406         return ctx;
2407 }
2408
2409 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2410 #include "selftests/mock_context.c"
2411 #include "selftests/i915_gem_context.c"
2412 #endif
2413
2414 static void i915_global_gem_context_shrink(void)
2415 {
2416         kmem_cache_shrink(global.slab_luts);
2417 }
2418
2419 static void i915_global_gem_context_exit(void)
2420 {
2421         kmem_cache_destroy(global.slab_luts);
2422 }
2423
2424 static struct i915_global_gem_context global = { {
2425         .shrink = i915_global_gem_context_shrink,
2426         .exit = i915_global_gem_context_exit,
2427 } };
2428
2429 int __init i915_global_gem_context_init(void)
2430 {
2431         global.slab_luts = KMEM_CACHE(i915_lut_handle, 0);
2432         if (!global.slab_luts)
2433                 return -ENOMEM;
2434
2435         i915_global_register(&global.base);
2436         return 0;
2437 }