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