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