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