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