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