xref: /linux/drivers/gpu/drm/xe/xe_exec_queue.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include "xe_exec_queue.h"
7 
8 #include <linux/nospec.h>
9 
10 #include <drm/drm_device.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_file.h>
13 #include <drm/drm_syncobj.h>
14 #include <uapi/drm/xe_drm.h>
15 
16 #include "xe_bo.h"
17 #include "xe_dep_scheduler.h"
18 #include "xe_device.h"
19 #include "xe_gt.h"
20 #include "xe_gt_sriov_pf.h"
21 #include "xe_gt_sriov_vf.h"
22 #include "xe_hw_engine_class_sysfs.h"
23 #include "xe_hw_engine_group.h"
24 #include "xe_irq.h"
25 #include "xe_lrc.h"
26 #include "xe_macros.h"
27 #include "xe_migrate.h"
28 #include "xe_pm.h"
29 #include "xe_trace.h"
30 #include "xe_vm.h"
31 #include "xe_pxp.h"
32 
33 /**
34  * DOC: Execution Queue
35  *
36  * An Execution queue is an interface for the HW context of execution.
37  * The user creates an execution queue, submits the GPU jobs through those
38  * queues and in the end destroys them.
39  *
40  * Execution queues can also be created by XeKMD itself for driver internal
41  * operations like object migration etc.
42  *
43  * An execution queue is associated with a specified HW engine or a group of
44  * engines (belonging to the same tile and engine class) and any GPU job
45  * submitted on the queue will be run on one of these engines.
46  *
47  * An execution queue is tied to an address space (VM). It holds a reference
48  * of the associated VM and the underlying Logical Ring Context/s (LRC/s)
49  * until the queue is destroyed.
50  *
51  * The execution queue sits on top of the submission backend. It opaquely
52  * handles the GuC and Execlist backends whichever the platform uses, and
53  * the ring operations the different engine classes support.
54  */
55 
56 /**
57  * DOC: Multi Queue Group
58  *
59  * Multi Queue Group is another mode of execution supported by the compute
60  * and blitter copy command streamers (CCS and BCS, respectively). It is
61  * an enhancement of the existing hardware architecture and leverages the
62  * same submission model. It enables support for efficient, parallel
63  * execution of multiple queues within a single shared context. The multi
64  * queue group functionality is only supported with GuC submission backend.
65  * All the queues of a group must use the same address space (VM).
66  *
67  * The DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE execution queue property
68  * supports creating a multi queue group and adding queues to a queue group.
69  *
70  * The XE_EXEC_QUEUE_CREATE ioctl call with above property with value field
71  * set to DRM_XE_MULTI_GROUP_CREATE, will create a new multi queue group with
72  * the queue being created as the primary queue (aka q0) of the group. To add
73  * secondary queues to the group, they need to be created with the above
74  * property with id of the primary queue as the value. The properties of
75  * the primary queue (like priority, time slice) applies to the whole group.
76  * So, these properties can't be set for secondary queues of a group.
77  *
78  * The hardware does not support removing a queue from a multi-queue group.
79  * However, queues can be dynamically added to the group. A group can have
80  * up to 64 queues. To support this, XeKMD holds references to LRCs of the
81  * queues even after the queues are destroyed by the user until the whole
82  * group is destroyed. The secondary queues hold a reference to the primary
83  * queue thus preventing the group from being destroyed when user destroys
84  * the primary queue. Once the primary queue is destroyed, secondary queues
85  * can't be added to the queue group and new job submissions on existing
86  * secondary queues are not allowed.
87  *
88  * The queues of a multi queue group can set their priority within the group
89  * through the DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY property.
90  * This multi queue priority can also be set dynamically through the
91  * XE_EXEC_QUEUE_SET_PROPERTY ioctl. This is the only other property
92  * supported by the secondary queues of a multi queue group, other than
93  * DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE.
94  *
95  * When GuC reports an error on any of the queues of a multi queue group,
96  * the queue cleanup mechanism is invoked for all the queues of the group
97  * as hardware cannot make progress on the multi queue context.
98  *
99  * Refer :ref:`multi-queue-group-guc-interface` for multi queue group GuC
100  * interface.
101  */
102 
103 enum xe_exec_queue_sched_prop {
104 	XE_EXEC_QUEUE_JOB_TIMEOUT = 0,
105 	XE_EXEC_QUEUE_TIMESLICE = 1,
106 	XE_EXEC_QUEUE_PREEMPT_TIMEOUT = 2,
107 	XE_EXEC_QUEUE_SCHED_PROP_MAX = 3,
108 };
109 
110 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q,
111 				      u64 extensions);
112 
113 static void xe_exec_queue_group_cleanup(struct xe_exec_queue *q)
114 {
115 	struct xe_exec_queue_group *group = q->multi_queue.group;
116 	struct xe_lrc *lrc;
117 	unsigned long idx;
118 
119 	if (xe_exec_queue_is_multi_queue_secondary(q)) {
120 		/*
121 		 * Put pairs with get from xe_exec_queue_lookup() call
122 		 * in xe_exec_queue_group_validate().
123 		 */
124 		xe_exec_queue_put(xe_exec_queue_multi_queue_primary(q));
125 		return;
126 	}
127 
128 	if (!group)
129 		return;
130 
131 	/* Primary queue cleanup */
132 	xa_for_each(&group->xa, idx, lrc)
133 		xe_lrc_put(lrc);
134 
135 	xa_destroy(&group->xa);
136 	mutex_destroy(&group->list_lock);
137 	xe_bo_unpin_map_no_vm(group->cgp_bo);
138 	kfree(group);
139 }
140 
141 static void __xe_exec_queue_free(struct xe_exec_queue *q)
142 {
143 	int i;
144 
145 	for (i = 0; i < XE_EXEC_QUEUE_TLB_INVAL_COUNT; ++i)
146 		if (q->tlb_inval[i].dep_scheduler)
147 			xe_dep_scheduler_fini(q->tlb_inval[i].dep_scheduler);
148 
149 	if (xe_exec_queue_uses_pxp(q))
150 		xe_pxp_exec_queue_remove(gt_to_xe(q->gt)->pxp, q);
151 
152 	if (xe_exec_queue_is_multi_queue(q))
153 		xe_exec_queue_group_cleanup(q);
154 
155 	if (q->vm) {
156 		xe_vm_remove_exec_queue(q->vm, q);
157 		xe_vm_put(q->vm);
158 	}
159 
160 	if (q->xef)
161 		xe_file_put(q->xef);
162 
163 	kvfree(q->replay_state);
164 	kfree(q);
165 }
166 
167 static int alloc_dep_schedulers(struct xe_device *xe, struct xe_exec_queue *q)
168 {
169 	struct xe_tile *tile = gt_to_tile(q->gt);
170 	int i;
171 
172 	for (i = 0; i < XE_EXEC_QUEUE_TLB_INVAL_COUNT; ++i) {
173 		struct xe_dep_scheduler *dep_scheduler;
174 		struct xe_gt *gt;
175 		struct workqueue_struct *wq;
176 
177 		if (i == XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT)
178 			gt = tile->primary_gt;
179 		else
180 			gt = tile->media_gt;
181 
182 		if (!gt)
183 			continue;
184 
185 		wq = gt->tlb_inval.job_wq;
186 
187 #define MAX_TLB_INVAL_JOBS	16	/* Picking a reasonable value */
188 		dep_scheduler = xe_dep_scheduler_create(xe, wq, q->name,
189 							MAX_TLB_INVAL_JOBS);
190 		if (IS_ERR(dep_scheduler))
191 			return PTR_ERR(dep_scheduler);
192 
193 		q->tlb_inval[i].dep_scheduler = dep_scheduler;
194 	}
195 #undef MAX_TLB_INVAL_JOBS
196 
197 	return 0;
198 }
199 
200 static struct xe_exec_queue *__xe_exec_queue_alloc(struct xe_device *xe,
201 						   struct xe_vm *vm,
202 						   u32 logical_mask,
203 						   u16 width, struct xe_hw_engine *hwe,
204 						   u32 flags, u64 extensions)
205 {
206 	struct xe_exec_queue *q;
207 	struct xe_gt *gt = hwe->gt;
208 	int err;
209 
210 	q = kzalloc_flex(*q, lrc, width);
211 	if (!q)
212 		return ERR_PTR(-ENOMEM);
213 
214 	kref_init(&q->refcount);
215 	q->flags = flags;
216 	q->hwe = hwe;
217 	q->gt = gt;
218 	q->class = hwe->class;
219 	q->width = width;
220 	q->msix_vec = XE_IRQ_DEFAULT_MSIX;
221 	q->logical_mask = logical_mask;
222 	q->fence_irq = &gt->fence_irq[hwe->class];
223 	q->ring_ops = gt->ring_ops[hwe->class];
224 	q->ops = gt->exec_queue_ops;
225 	INIT_LIST_HEAD(&q->lr.link);
226 	INIT_LIST_HEAD(&q->vm_exec_queue_link);
227 	INIT_LIST_HEAD(&q->multi_gt_link);
228 	INIT_LIST_HEAD(&q->hw_engine_group_link);
229 	INIT_LIST_HEAD(&q->pxp.link);
230 	spin_lock_init(&q->multi_queue.lock);
231 	spin_lock_init(&q->lrc_lookup_lock);
232 	q->multi_queue.priority = XE_MULTI_QUEUE_PRIORITY_NORMAL;
233 
234 	q->sched_props.timeslice_us = hwe->eclass->sched_props.timeslice_us;
235 	q->sched_props.preempt_timeout_us =
236 				hwe->eclass->sched_props.preempt_timeout_us;
237 	q->sched_props.job_timeout_ms =
238 				hwe->eclass->sched_props.job_timeout_ms;
239 	if (q->flags & EXEC_QUEUE_FLAG_KERNEL &&
240 	    q->flags & EXEC_QUEUE_FLAG_HIGH_PRIORITY)
241 		q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_KERNEL;
242 	else
243 		q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_NORMAL;
244 
245 	if (q->flags & (EXEC_QUEUE_FLAG_MIGRATE | EXEC_QUEUE_FLAG_VM)) {
246 		err = alloc_dep_schedulers(xe, q);
247 		if (err) {
248 			__xe_exec_queue_free(q);
249 			return ERR_PTR(err);
250 		}
251 	}
252 
253 	if (vm)
254 		q->vm = xe_vm_get(vm);
255 
256 	if (extensions) {
257 		/*
258 		 * may set q->usm, must come before xe_lrc_create(),
259 		 * may overwrite q->sched_props, must come before q->ops->init()
260 		 */
261 		err = exec_queue_user_extensions(xe, q, extensions);
262 		if (err) {
263 			__xe_exec_queue_free(q);
264 			return ERR_PTR(err);
265 		}
266 	}
267 
268 	return q;
269 }
270 
271 static void xe_exec_queue_set_lrc(struct xe_exec_queue *q, struct xe_lrc *lrc, u16 idx)
272 {
273 	xe_assert(gt_to_xe(q->gt), idx < q->width);
274 
275 	scoped_guard(spinlock, &q->lrc_lookup_lock) {
276 		q->lrc[idx] = lrc;
277 		if (xe_exec_queue_is_multi_queue(q))
278 			q->lrc[idx]->multi_queue.primary_lrc =
279 				q->multi_queue.group->primary->lrc[0];
280 	}
281 }
282 
283 /**
284  * xe_exec_queue_get_lrc() - Get the LRC from exec queue.
285  * @q: The exec queue instance.
286  * @idx: Index within multi-LRC array.
287  *
288  * Retrieves LRC of given index for the exec queue under lock
289  * and takes reference.
290  *
291  * Return: Pointer to LRC on success, error on failure, NULL on
292  * lookup failure.
293  */
294 struct xe_lrc *xe_exec_queue_get_lrc(struct xe_exec_queue *q, u16 idx)
295 {
296 	struct xe_lrc *lrc;
297 
298 	xe_assert(gt_to_xe(q->gt), idx < q->width);
299 
300 	scoped_guard(spinlock, &q->lrc_lookup_lock) {
301 		lrc = q->lrc[idx];
302 		if (lrc)
303 			xe_lrc_get(lrc);
304 	}
305 
306 	return lrc;
307 }
308 
309 /**
310  * xe_exec_queue_lrc() - Get the LRC from exec queue.
311  * @q: The exec queue instance.
312  *
313  * Retrieves the primary LRC for the exec queue. Note that this function
314  * returns only the first LRC instance, even when multiple parallel LRCs
315  * are configured. This function does not increment reference count,
316  * so the reference can be just forgotten after use.
317  *
318  * Return: Pointer to LRC on success, error on failure
319  */
320 struct xe_lrc *xe_exec_queue_lrc(struct xe_exec_queue *q)
321 {
322 	return q->lrc[0];
323 }
324 
325 /*
326  * Publish the queue back-pointer in the LRC BOs.
327  *
328  * The BO holds no reference on the queue; the queue owns the LRCs, and
329  * therefore the BOs, instead. The back-pointer is made safe by two rules:
330  *
331  *  - It is published only once the queue is fully constructed and can no
332  *    longer be destroyed by an error path that bypasses the kref (see
333  *    xe_exec_queue_create()), so a reader that successfully takes a
334  *    reference can never be handed a queue that is freed without going
335  *    through xe_exec_queue_destroy().
336  *
337  *  - It is written and cleared under the BO dma_resv. Readers must hold
338  *    the same lock across both the read and
339  *    xe_exec_queue_get_unless_zero(), which serializes them against
340  *    xe_exec_queue_clear_lrc_bo_backpointer() below.
341  *
342  * For a multi-queue group the LRC BOs point at the primary queue, which is
343  * kept alive by the reference every secondary holds on it.
344  */
345 static void xe_exec_queue_set_lrc_bo_backpointer(struct xe_exec_queue *q)
346 {
347 	struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
348 	int i;
349 
350 	for (i = 0; i < q->width; ++i) {
351 		struct xe_bo *bo = q->lrc[i]->bo;
352 
353 		xe_bo_lock(bo, false);
354 		bo->q = primary;
355 		xe_bo_unlock(bo);
356 	}
357 }
358 
359 /*
360  * Drop the queue back-pointer before anything belonging to the queue is
361  * torn down. This must happen before q->ops->fini(), otherwise a reader
362  * could take a reference and then operate on an already destroyed backend.
363  */
364 static void xe_exec_queue_clear_lrc_bo_backpointer(struct xe_exec_queue *q)
365 {
366 	int i;
367 
368 	for (i = 0; i < q->width; ++i) {
369 		struct xe_bo *bo = q->lrc[i] ? q->lrc[i]->bo : NULL;
370 
371 		if (!bo)
372 			continue;
373 
374 		xe_bo_lock(bo, false);
375 		bo->q = NULL;
376 		xe_bo_unlock(bo);
377 	}
378 }
379 
380 static void __xe_exec_queue_fini(struct xe_exec_queue *q)
381 {
382 	int i;
383 
384 	xe_exec_queue_clear_lrc_bo_backpointer(q);
385 	q->ops->fini(q);
386 
387 	for (i = 0; i < q->width; ++i)
388 		xe_lrc_put(q->lrc[i]);
389 }
390 
391 static int __xe_exec_queue_init(struct xe_exec_queue *q, u32 exec_queue_flags)
392 {
393 	int i, err;
394 	u32 flags = 0;
395 
396 	/*
397 	 * PXP workloads executing on RCS or CCS must run in isolation (i.e. no
398 	 * other workload can use the EUs at the same time). On MTL this is done
399 	 * by setting the RUNALONE bit in the LRC, while starting on Xe2 there
400 	 * is a dedicated bit for it.
401 	 */
402 	if (xe_exec_queue_uses_pxp(q) &&
403 	    (q->class == XE_ENGINE_CLASS_RENDER || q->class == XE_ENGINE_CLASS_COMPUTE)) {
404 		if (GRAPHICS_VER(gt_to_xe(q->gt)) >= 20)
405 			flags |= XE_LRC_CREATE_PXP;
406 		else
407 			flags |= XE_LRC_CREATE_RUNALONE;
408 	}
409 
410 	if (!(exec_queue_flags & EXEC_QUEUE_FLAG_KERNEL))
411 		flags |= XE_LRC_CREATE_USER_CTX;
412 
413 	if (q->flags & EXEC_QUEUE_FLAG_DISABLE_STATE_CACHE_PERF_FIX)
414 		flags |= XE_LRC_DISABLE_STATE_CACHE_PERF_FIX;
415 
416 	err = q->ops->init(q);
417 	if (err)
418 		return err;
419 
420 	/*
421 	 * This must occur after q->ops->init to avoid race conditions during VF
422 	 * post-migration recovery, as the fixups for the LRC GGTT addresses
423 	 * depend on the queue being present in the backend tracking structure.
424 	 *
425 	 * In addition to above, we must wait on inflight GGTT changes to avoid
426 	 * writing out stale values here. Such wait provides a solid solution
427 	 * (without a race) only if the function can detect migration instantly
428 	 * from the moment vCPU resumes execution.
429 	 */
430 	for (i = 0; i < q->width; ++i) {
431 		struct xe_lrc *__lrc = NULL;
432 		int marker;
433 
434 		do {
435 			struct xe_lrc *lrc;
436 
437 			marker = xe_gt_sriov_vf_wait_valid_ggtt(q->gt);
438 
439 			lrc = xe_lrc_create(q->hwe, q->vm, q->replay_state,
440 					    xe_lrc_ring_size(), q->msix_vec, flags);
441 			if (IS_ERR(lrc)) {
442 				err = PTR_ERR(lrc);
443 				goto err_lrc;
444 			}
445 
446 			xe_exec_queue_set_lrc(q, lrc, i);
447 
448 			if (__lrc)
449 				xe_lrc_put(__lrc);
450 			__lrc = lrc;
451 
452 		} while (marker != xe_vf_migration_fixups_complete_count(q->gt));
453 	}
454 
455 	return 0;
456 
457 err_lrc:
458 	__xe_exec_queue_fini(q);
459 	return err;
460 }
461 
462 /**
463  * xe_exec_queue_create() - Create an exec queue
464  * @xe: Xe device
465  * @vm: VM for the exec queue
466  * @logical_mask: Logical mask of HW engines
467  * @width: Width of the exec queue (number of LRCs)
468  * @hwe: Hardware engine
469  * @flags: Exec queue creation flags
470  * @extensions: Extensions for exec queue creation
471  *
472  * Create an exec queue (allocate and initialize) with the specified parameters
473  *
474  * Return: Pointer to the created exec queue on success, ERR_PTR on failure
475  */
476 struct xe_exec_queue *xe_exec_queue_create(struct xe_device *xe, struct xe_vm *vm,
477 					   u32 logical_mask, u16 width,
478 					   struct xe_hw_engine *hwe, u32 flags,
479 					   u64 extensions)
480 {
481 	struct xe_exec_queue *q;
482 	int err;
483 
484 	/* VMs for GSCCS queues (and only those) must have the XE_VM_FLAG_GSC flag */
485 	xe_assert(xe, !vm || (!!(vm->flags & XE_VM_FLAG_GSC) == !!(hwe->engine_id == XE_HW_ENGINE_GSCCS0)));
486 
487 	q = __xe_exec_queue_alloc(xe, vm, logical_mask, width, hwe, flags,
488 				  extensions);
489 	if (IS_ERR(q))
490 		return q;
491 
492 	err = __xe_exec_queue_init(q, flags);
493 	if (err)
494 		goto err_post_alloc;
495 
496 	/*
497 	 * We can only add the queue to the PXP list after the init is complete,
498 	 * because the PXP termination can call exec_queue_kill and that will
499 	 * go bad if the queue is only half-initialized. This means that we
500 	 * can't do it when we handle the PXP extension in __xe_exec_queue_alloc
501 	 * and we need to do it here instead.
502 	 */
503 	if (xe_exec_queue_uses_pxp(q)) {
504 		err = xe_pxp_exec_queue_add(xe->pxp, q);
505 		if (err)
506 			goto err_post_init;
507 	}
508 
509 	/*
510 	 * Publish the LRC BO back-pointers last: past this point the queue can
511 	 * only be destroyed through xe_exec_queue_destroy(), so a concurrent
512 	 * reader that takes a reference via bo->q cannot race with the
513 	 * kref-bypassing error paths below.
514 	 */
515 	xe_exec_queue_set_lrc_bo_backpointer(q);
516 
517 	return q;
518 
519 err_post_init:
520 	__xe_exec_queue_fini(q);
521 err_post_alloc:
522 	__xe_exec_queue_free(q);
523 	return ERR_PTR(err);
524 }
525 ALLOW_ERROR_INJECTION(xe_exec_queue_create, ERRNO);
526 
527 /**
528  * xe_exec_queue_create_class() - Create an exec queue for a specific engine class
529  * @xe: Xe device
530  * @gt: GT for the exec queue
531  * @vm: VM for the exec queue
532  * @class: Engine class
533  * @flags: Exec queue creation flags
534  * @extensions: Extensions for exec queue creation
535  *
536  * Create an exec queue for the specified engine class.
537  *
538  * Return: Pointer to the created exec queue on success, ERR_PTR on failure
539  */
540 struct xe_exec_queue *xe_exec_queue_create_class(struct xe_device *xe, struct xe_gt *gt,
541 						 struct xe_vm *vm,
542 						 enum xe_engine_class class,
543 						 u32 flags, u64 extensions)
544 {
545 	struct xe_hw_engine *hwe, *hwe0 = NULL;
546 	enum xe_hw_engine_id id;
547 	u32 logical_mask = 0;
548 
549 	for_each_hw_engine(hwe, gt, id) {
550 		if (xe_hw_engine_is_reserved(hwe))
551 			continue;
552 
553 		if (hwe->class == class) {
554 			logical_mask |= BIT(hwe->logical_instance);
555 			if (!hwe0)
556 				hwe0 = hwe;
557 		}
558 	}
559 
560 	if (!logical_mask)
561 		return ERR_PTR(-ENODEV);
562 
563 	return xe_exec_queue_create(xe, vm, logical_mask, 1, hwe0, flags, extensions);
564 }
565 
566 /**
567  * xe_exec_queue_create_bind() - Create bind exec queue.
568  * @xe: Xe device.
569  * @tile: tile which bind exec queue belongs to.
570  * @flags: exec queue creation flags
571  * @user_vm: The user VM which this exec queue belongs to
572  * @extensions: exec queue creation extensions
573  *
574  * Normalize bind exec queue creation. Bind exec queue is tied to migration VM
575  * for access to physical memory required for page table programming. On a
576  * faulting devices the reserved copy engine instance must be used to avoid
577  * deadlocking (user binds cannot get stuck behind faults as kernel binds which
578  * resolve faults depend on user binds). On non-faulting devices any copy engine
579  * can be used.
580  *
581  * Returns exec queue on success, ERR_PTR on failure
582  */
583 struct xe_exec_queue *xe_exec_queue_create_bind(struct xe_device *xe,
584 						struct xe_tile *tile,
585 						struct xe_vm *user_vm,
586 						u32 flags, u64 extensions)
587 {
588 	struct xe_gt *gt = tile->primary_gt;
589 	struct xe_exec_queue *q;
590 	struct xe_vm *migrate_vm;
591 
592 	migrate_vm = xe_migrate_get_vm(tile->migrate);
593 	if (xe->info.has_usm) {
594 		struct xe_hw_engine *hwe = gt->usm.paging_hwe0;
595 
596 		if (!hwe) {
597 			xe_vm_put(migrate_vm);
598 			return ERR_PTR(-EINVAL);
599 		}
600 
601 		q = xe_exec_queue_create(xe, migrate_vm,
602 					 BIT(hwe->logical_instance), 1, hwe,
603 					 flags, extensions);
604 	} else {
605 		q = xe_exec_queue_create_class(xe, gt, migrate_vm,
606 					       XE_ENGINE_CLASS_COPY, flags,
607 					       extensions);
608 	}
609 	xe_vm_put(migrate_vm);
610 
611 	if (!IS_ERR(q)) {
612 		int err = drm_syncobj_create(&q->ufence_syncobj,
613 					     DRM_SYNCOBJ_CREATE_SIGNALED,
614 					     NULL);
615 		if (err) {
616 			xe_exec_queue_put(q);
617 			return ERR_PTR(err);
618 		}
619 
620 		if (user_vm)
621 			q->user_vm = xe_vm_get(user_vm);
622 	}
623 
624 	return q;
625 }
626 ALLOW_ERROR_INJECTION(xe_exec_queue_create_bind, ERRNO);
627 
628 /**
629  * xe_exec_queue_destroy() - Destroy an exec queue
630  * @ref: Reference count of the exec queue
631  *
632  * Called when the last reference to the exec queue is dropped.
633  * Cleans up all resources associated with the exec queue.
634  * This function should not be called directly; use xe_exec_queue_put() instead.
635  */
636 void xe_exec_queue_destroy(struct kref *ref)
637 {
638 	struct xe_exec_queue *q = container_of(ref, struct xe_exec_queue, refcount);
639 	struct xe_exec_queue *eq, *next;
640 	int i;
641 
642 	xe_assert(gt_to_xe(q->gt), atomic_read(&q->job_cnt) == 0);
643 
644 	if (q->ufence_syncobj)
645 		drm_syncobj_put(q->ufence_syncobj);
646 
647 	if (xe_exec_queue_uses_pxp(q))
648 		xe_pxp_exec_queue_remove(gt_to_xe(q->gt)->pxp, q);
649 
650 	xe_exec_queue_last_fence_put_unlocked(q);
651 	for_each_tlb_inval(i)
652 		xe_exec_queue_tlb_inval_last_fence_put_unlocked(q, i);
653 
654 	if (!(q->flags & EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD)) {
655 		list_for_each_entry_safe(eq, next, &q->multi_gt_list,
656 					 multi_gt_link)
657 			xe_exec_queue_put(eq);
658 	}
659 
660 	if (q->user_vm) {
661 		xe_vm_put(q->user_vm);
662 		q->user_vm = NULL;
663 	}
664 
665 	q->ops->destroy(q);
666 }
667 
668 /**
669  * xe_exec_queue_fini() - Finalize an exec queue
670  * @q: The exec queue
671  *
672  * Finalizes the exec queue by updating run ticks, releasing LRC references,
673  * and freeing the queue structure. This is called after the queue has been
674  * destroyed and all references have been dropped.
675  */
676 void xe_exec_queue_fini(struct xe_exec_queue *q)
677 {
678 	/*
679 	 * Before releasing our ref to lrc and xef, accumulate our run ticks
680 	 * and wakeup any waiters.
681 	 */
682 	xe_exec_queue_update_run_ticks(q);
683 	if (q->xef && atomic_dec_and_test(&q->xef->exec_queue.pending_removal))
684 		wake_up_var(&q->xef->exec_queue.pending_removal);
685 
686 	__xe_exec_queue_fini(q);
687 	__xe_exec_queue_free(q);
688 }
689 
690 /**
691  * xe_exec_queue_assign_name() - Assign a name to an exec queue
692  * @q: The exec queue
693  * @instance: Instance number for the engine
694  *
695  * Assigns a human-readable name to the exec queue based on its engine class
696  * and instance number (e.g., "rcs0", "vcs1", "bcs2").
697  */
698 void xe_exec_queue_assign_name(struct xe_exec_queue *q, u32 instance)
699 {
700 	switch (q->class) {
701 	case XE_ENGINE_CLASS_RENDER:
702 		snprintf(q->name, sizeof(q->name), "rcs%d", instance);
703 		break;
704 	case XE_ENGINE_CLASS_VIDEO_DECODE:
705 		snprintf(q->name, sizeof(q->name), "vcs%d", instance);
706 		break;
707 	case XE_ENGINE_CLASS_VIDEO_ENHANCE:
708 		snprintf(q->name, sizeof(q->name), "vecs%d", instance);
709 		break;
710 	case XE_ENGINE_CLASS_COPY:
711 		snprintf(q->name, sizeof(q->name), "bcs%d", instance);
712 		break;
713 	case XE_ENGINE_CLASS_COMPUTE:
714 		snprintf(q->name, sizeof(q->name), "ccs%d", instance);
715 		break;
716 	case XE_ENGINE_CLASS_OTHER:
717 		snprintf(q->name, sizeof(q->name), "gsccs%d", instance);
718 		break;
719 	default:
720 		XE_WARN_ON(q->class);
721 	}
722 }
723 
724 /**
725  * xe_exec_queue_lookup() - Look up an exec queue by ID
726  * @xef: Xe file private data
727  * @id: Exec queue ID
728  *
729  * Looks up an exec queue by its ID and increments its reference count.
730  *
731  * Return: Pointer to the exec queue if found, NULL otherwise
732  */
733 struct xe_exec_queue *xe_exec_queue_lookup(struct xe_file *xef, u32 id)
734 {
735 	struct xe_exec_queue *q;
736 
737 	mutex_lock(&xef->exec_queue.lock);
738 	q = xa_load(&xef->exec_queue.xa, id);
739 	if (q)
740 		xe_exec_queue_get(q);
741 	mutex_unlock(&xef->exec_queue.lock);
742 
743 	return q;
744 }
745 
746 /**
747  * xe_exec_queue_device_get_max_priority() - Get maximum priority for an exec queues
748  * @xe: Xe device
749  *
750  * Returns the maximum priority level that can be assigned to an exec queues.
751  *
752  * Return: Maximum priority level (HIGH if CAP_SYS_NICE, NORMAL otherwise)
753  */
754 enum xe_exec_queue_priority
755 xe_exec_queue_device_get_max_priority(struct xe_device *xe)
756 {
757 	return capable(CAP_SYS_NICE) ? XE_EXEC_QUEUE_PRIORITY_HIGH :
758 				       XE_EXEC_QUEUE_PRIORITY_NORMAL;
759 }
760 
761 static int exec_queue_set_priority(struct xe_device *xe, struct xe_exec_queue *q,
762 				   u64 value)
763 {
764 	if (XE_IOCTL_DBG(xe, value > XE_EXEC_QUEUE_PRIORITY_HIGH))
765 		return -EINVAL;
766 
767 	if (XE_IOCTL_DBG(xe, value > xe_exec_queue_device_get_max_priority(xe)))
768 		return -EPERM;
769 
770 	q->sched_props.priority = value;
771 	return 0;
772 }
773 
774 static bool xe_exec_queue_enforce_schedule_limit(void)
775 {
776 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT)
777 	return true;
778 #else
779 	return !capable(CAP_SYS_NICE);
780 #endif
781 }
782 
783 static void
784 xe_exec_queue_get_prop_minmax(struct xe_hw_engine_class_intf *eclass,
785 			      enum xe_exec_queue_sched_prop prop,
786 			      u32 *min, u32 *max)
787 {
788 	switch (prop) {
789 	case XE_EXEC_QUEUE_JOB_TIMEOUT:
790 		*min = eclass->sched_props.job_timeout_min;
791 		*max = eclass->sched_props.job_timeout_max;
792 		break;
793 	case XE_EXEC_QUEUE_TIMESLICE:
794 		*min = eclass->sched_props.timeslice_min;
795 		*max = eclass->sched_props.timeslice_max;
796 		break;
797 	case XE_EXEC_QUEUE_PREEMPT_TIMEOUT:
798 		*min = eclass->sched_props.preempt_timeout_min;
799 		*max = eclass->sched_props.preempt_timeout_max;
800 		break;
801 	default:
802 		break;
803 	}
804 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT)
805 	if (capable(CAP_SYS_NICE)) {
806 		switch (prop) {
807 		case XE_EXEC_QUEUE_JOB_TIMEOUT:
808 			*min = XE_HW_ENGINE_JOB_TIMEOUT_MIN;
809 			*max = XE_HW_ENGINE_JOB_TIMEOUT_MAX;
810 			break;
811 		case XE_EXEC_QUEUE_TIMESLICE:
812 			*min = XE_HW_ENGINE_TIMESLICE_MIN;
813 			*max = XE_HW_ENGINE_TIMESLICE_MAX;
814 			break;
815 		case XE_EXEC_QUEUE_PREEMPT_TIMEOUT:
816 			*min = XE_HW_ENGINE_PREEMPT_TIMEOUT_MIN;
817 			*max = XE_HW_ENGINE_PREEMPT_TIMEOUT_MAX;
818 			break;
819 		default:
820 			break;
821 		}
822 	}
823 #endif
824 }
825 
826 static int exec_queue_set_timeslice(struct xe_device *xe, struct xe_exec_queue *q,
827 				    u64 value)
828 {
829 	u32 min = 0, max = 0;
830 
831 	xe_exec_queue_get_prop_minmax(q->hwe->eclass,
832 				      XE_EXEC_QUEUE_TIMESLICE, &min, &max);
833 
834 	if (xe_exec_queue_enforce_schedule_limit() &&
835 	    !xe_hw_engine_timeout_in_range(value, min, max))
836 		return -EINVAL;
837 
838 	q->sched_props.timeslice_us = value;
839 	return 0;
840 }
841 
842 static int
843 exec_queue_set_pxp_type(struct xe_device *xe, struct xe_exec_queue *q, u64 value)
844 {
845 	if (value == DRM_XE_PXP_TYPE_NONE)
846 		return 0;
847 
848 	/* we only support HWDRM sessions right now */
849 	if (XE_IOCTL_DBG(xe, value != DRM_XE_PXP_TYPE_HWDRM))
850 		return -EINVAL;
851 
852 	if (!xe_pxp_is_enabled(xe->pxp))
853 		return -ENODEV;
854 
855 	return xe_pxp_exec_queue_set_type(xe->pxp, q, DRM_XE_PXP_TYPE_HWDRM);
856 }
857 
858 static int exec_queue_set_hang_replay_state(struct xe_device *xe,
859 					    struct xe_exec_queue *q,
860 					    u64 value)
861 {
862 	size_t size = xe_gt_lrc_hang_replay_size(q->gt, q->class);
863 	u64 __user *address = u64_to_user_ptr(value);
864 	void *ptr;
865 
866 	if (q->replay_state)
867 		return -EINVAL;
868 
869 	ptr = vmemdup_user(address, size);
870 	if (XE_IOCTL_DBG(xe, IS_ERR(ptr)))
871 		return PTR_ERR(ptr);
872 
873 	q->replay_state = ptr;
874 
875 	return 0;
876 }
877 
878 static int xe_exec_queue_group_init(struct xe_device *xe, struct xe_exec_queue *q)
879 {
880 	struct xe_tile *tile = gt_to_tile(q->gt);
881 	struct xe_exec_queue_group *group;
882 	struct xe_bo *bo;
883 
884 	group = kzalloc_obj(*group);
885 	if (!group)
886 		return -ENOMEM;
887 
888 	bo = xe_bo_create_pin_map_novm(xe, tile, SZ_4K, ttm_bo_type_kernel,
889 				       XE_BO_FLAG_VRAM_IF_DGFX(tile) |
890 				       XE_BO_FLAG_PINNED_LATE_RESTORE |
891 				       XE_BO_FLAG_FORCE_USER_VRAM |
892 				       XE_BO_FLAG_GGTT_INVALIDATE |
893 				       XE_BO_FLAG_GGTT, false);
894 	if (IS_ERR(bo)) {
895 		drm_err(&xe->drm, "CGP bo allocation for queue group failed: %ld\n",
896 			PTR_ERR(bo));
897 		kfree(group);
898 		return PTR_ERR(bo);
899 	}
900 
901 	xe_map_memset(xe, &bo->vmap, 0, 0, SZ_4K);
902 
903 	group->primary = q;
904 	group->cgp_bo = bo;
905 	INIT_LIST_HEAD(&group->list);
906 	spin_lock_init(&group->suspend_lock);
907 	xa_init_flags(&group->xa, XA_FLAGS_ALLOC1);
908 	mutex_init(&group->list_lock);
909 	q->multi_queue.group = group;
910 
911 	/* group->list_lock is used in submission backend */
912 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
913 		fs_reclaim_acquire(GFP_KERNEL);
914 		might_lock(&group->list_lock);
915 		fs_reclaim_release(GFP_KERNEL);
916 	}
917 
918 	return 0;
919 }
920 
921 static int xe_exec_queue_group_validate(struct xe_device *xe, struct xe_exec_queue *q,
922 					u32 primary_id)
923 {
924 	struct xe_exec_queue_group *group;
925 	struct xe_exec_queue *primary;
926 	int ret;
927 
928 	/*
929 	 * Get from below xe_exec_queue_lookup() pairs with put
930 	 * in xe_exec_queue_group_cleanup().
931 	 */
932 	primary = xe_exec_queue_lookup(q->vm->xef, primary_id);
933 	if (XE_IOCTL_DBG(xe, !primary))
934 		return -ENOENT;
935 
936 	if (XE_IOCTL_DBG(xe, !xe_exec_queue_is_multi_queue_primary(primary)) ||
937 	    XE_IOCTL_DBG(xe, q->vm != primary->vm) ||
938 	    XE_IOCTL_DBG(xe, q->logical_mask != primary->logical_mask)) {
939 		ret = -EINVAL;
940 		goto put_primary;
941 	}
942 
943 	group = primary->multi_queue.group;
944 	q->multi_queue.valid = true;
945 	q->multi_queue.group = group;
946 
947 	return 0;
948 put_primary:
949 	xe_exec_queue_put(primary);
950 	return ret;
951 }
952 
953 #define XE_MAX_GROUP_SIZE	64
954 static int xe_exec_queue_group_add(struct xe_device *xe, struct xe_exec_queue *q)
955 {
956 	struct xe_exec_queue_group *group = q->multi_queue.group;
957 	u32 pos;
958 	int err;
959 
960 	xe_assert(xe, xe_exec_queue_is_multi_queue_secondary(q));
961 
962 	/* Primary queue holds a reference to LRCs of all secondary queues */
963 	err = xa_alloc(&group->xa, &pos, xe_lrc_get(q->lrc[0]),
964 		       XA_LIMIT(1, XE_MAX_GROUP_SIZE - 1), GFP_KERNEL);
965 	if (XE_IOCTL_DBG(xe, err)) {
966 		xe_lrc_put(q->lrc[0]);
967 
968 		/* It is invalid if queue group limit is exceeded */
969 		if (err == -EBUSY)
970 			err = -EINVAL;
971 
972 		return err;
973 	}
974 
975 	q->multi_queue.pos = pos;
976 	q->lrc[0]->multi_queue.pos = pos;
977 
978 	return 0;
979 }
980 
981 static void xe_exec_queue_group_delete(struct xe_device *xe, struct xe_exec_queue *q)
982 {
983 	struct xe_exec_queue_group *group = q->multi_queue.group;
984 	struct xe_lrc *lrc;
985 
986 	xe_assert(xe, xe_exec_queue_is_multi_queue_secondary(q));
987 
988 	lrc = xa_erase(&group->xa, q->multi_queue.pos);
989 	xe_assert(xe, lrc);
990 	xe_lrc_put(lrc);
991 }
992 
993 static int exec_queue_set_multi_group(struct xe_device *xe, struct xe_exec_queue *q,
994 				      u64 value)
995 {
996 	if (XE_IOCTL_DBG(xe, !xe_gt_supports_multi_queue(q->gt, q->class)))
997 		return -ENODEV;
998 
999 	if (XE_IOCTL_DBG(xe, !xe_device_uc_enabled(xe)))
1000 		return -EOPNOTSUPP;
1001 
1002 	if (XE_IOCTL_DBG(xe, !q->vm->xef))
1003 		return -EINVAL;
1004 
1005 	if (XE_IOCTL_DBG(xe, xe_exec_queue_is_parallel(q)))
1006 		return -EINVAL;
1007 
1008 	if (XE_IOCTL_DBG(xe, xe_exec_queue_is_multi_queue(q)))
1009 		return -EINVAL;
1010 
1011 	if (value & DRM_XE_MULTI_GROUP_CREATE) {
1012 		if (XE_IOCTL_DBG(xe, value & ~DRM_XE_MULTI_GROUP_CREATE))
1013 			return -EINVAL;
1014 
1015 		q->multi_queue.valid = true;
1016 		q->multi_queue.is_primary = true;
1017 		q->multi_queue.pos = 0;
1018 		return 0;
1019 	}
1020 
1021 	/* While adding secondary queues, the upper 32 bits must be 0 */
1022 	if (XE_IOCTL_DBG(xe, value & (~0ull << 32)))
1023 		return -EINVAL;
1024 
1025 	return xe_exec_queue_group_validate(xe, q, value);
1026 }
1027 
1028 static int exec_queue_set_multi_queue_priority(struct xe_device *xe, struct xe_exec_queue *q,
1029 					       u64 value)
1030 {
1031 	if (XE_IOCTL_DBG(xe, value > XE_MULTI_QUEUE_PRIORITY_HIGH))
1032 		return -EINVAL;
1033 
1034 	/* For queue creation time (!q->xef) setting, just store the priority value */
1035 	if (!q->xef) {
1036 		q->multi_queue.priority = value;
1037 		return 0;
1038 	}
1039 
1040 	if (!xe_exec_queue_is_multi_queue(q))
1041 		return -EINVAL;
1042 
1043 	return q->ops->set_multi_queue_priority(q, value);
1044 }
1045 
1046 static int exec_queue_set_state_cache_perf_fix(struct xe_device *xe, struct xe_exec_queue *q,
1047 					       u64 value)
1048 {
1049 	if (XE_IOCTL_DBG(xe, q->class != XE_ENGINE_CLASS_RENDER))
1050 		return -EOPNOTSUPP;
1051 
1052 	q->flags |= value != 0 ? EXEC_QUEUE_FLAG_DISABLE_STATE_CACHE_PERF_FIX : 0;
1053 
1054 	return 0;
1055 }
1056 
1057 typedef int (*xe_exec_queue_set_property_fn)(struct xe_device *xe,
1058 					     struct xe_exec_queue *q,
1059 					     u64 value);
1060 
1061 static const xe_exec_queue_set_property_fn exec_queue_set_property_funcs[] = {
1062 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY] = exec_queue_set_priority,
1063 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE] = exec_queue_set_timeslice,
1064 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE] = exec_queue_set_pxp_type,
1065 	[DRM_XE_EXEC_QUEUE_SET_HANG_REPLAY_STATE] = exec_queue_set_hang_replay_state,
1066 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP] = exec_queue_set_multi_group,
1067 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY] =
1068 							exec_queue_set_multi_queue_priority,
1069 	[DRM_XE_EXEC_QUEUE_SET_DISABLE_STATE_CACHE_PERF_FIX] =
1070 							exec_queue_set_state_cache_perf_fix,
1071 };
1072 
1073 /**
1074  * xe_exec_queue_set_property_ioctl() - Set a property on an exec queue
1075  * @dev: DRM device
1076  * @data: IOCTL data
1077  * @file: DRM file
1078  *
1079  * Allows setting properties on an existing exec queue. Currently only
1080  * supports setting multi-queue priority.
1081  *
1082  * Return: 0 on success, negative error code on failure
1083  */
1084 int xe_exec_queue_set_property_ioctl(struct drm_device *dev, void *data,
1085 				     struct drm_file *file)
1086 {
1087 	struct xe_device *xe = to_xe_device(dev);
1088 	struct xe_file *xef = to_xe_file(file);
1089 	struct drm_xe_exec_queue_set_property *args = data;
1090 	struct xe_exec_queue *q;
1091 	int ret;
1092 	u32 idx;
1093 
1094 	if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1095 		return -EINVAL;
1096 
1097 	if (XE_IOCTL_DBG(xe, args->property !=
1098 			 DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY))
1099 		return -EINVAL;
1100 
1101 	q = xe_exec_queue_lookup(xef, args->exec_queue_id);
1102 	if (XE_IOCTL_DBG(xe, !q))
1103 		return -ENOENT;
1104 
1105 	idx = array_index_nospec(args->property,
1106 				 ARRAY_SIZE(exec_queue_set_property_funcs));
1107 	ret = exec_queue_set_property_funcs[idx](xe, q, args->value);
1108 	if (XE_IOCTL_DBG(xe, ret))
1109 		goto err_post_lookup;
1110 
1111 	xe_exec_queue_put(q);
1112 	return 0;
1113 
1114  err_post_lookup:
1115 	xe_exec_queue_put(q);
1116 	return ret;
1117 }
1118 
1119 static int exec_queue_user_ext_check(struct xe_exec_queue *q, u64 properties)
1120 {
1121 	struct xe_device *xe = gt_to_xe(q->gt);
1122 	u64 secondary_queue_valid_props = BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP) |
1123 				  BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY);
1124 
1125 	/*
1126 	 * Only MULTI_QUEUE_PRIORITY property is valid for secondary queues of a
1127 	 * multi-queue group.
1128 	 */
1129 	if (xe_exec_queue_is_multi_queue_secondary(q) &&
1130 	    properties & ~secondary_queue_valid_props)
1131 		return -EINVAL;
1132 
1133 	/*
1134 	 * HWDRM is the only supported PXP type today. It is display related and
1135 	 * hence can't work with multi-queue. Reject the combination. The secondary
1136 	 * queue path above already rejects any PXP property, so this also covers
1137 	 * the multi-queue primary which would otherwise allow it.
1138 	 */
1139 	if (XE_IOCTL_DBG(xe, (properties & BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP)) &&
1140 			 (properties & BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE))))
1141 		return -EINVAL;
1142 
1143 	return 0;
1144 }
1145 
1146 static int exec_queue_user_ext_check_final(struct xe_exec_queue *q, u64 properties)
1147 {
1148 	/* MULTI_QUEUE_PRIORITY only applies to multi-queue group queues */
1149 	if ((properties & BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY)) &&
1150 	    !(properties & BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP)))
1151 		return -EINVAL;
1152 
1153 	return 0;
1154 }
1155 
1156 static int exec_queue_user_ext_set_property(struct xe_device *xe,
1157 					    struct xe_exec_queue *q,
1158 					    u64 extension, u64 *properties)
1159 {
1160 	u64 __user *address = u64_to_user_ptr(extension);
1161 	struct drm_xe_ext_set_property ext;
1162 	int err;
1163 	u32 idx;
1164 
1165 	err = copy_from_user(&ext, address, sizeof(ext));
1166 	if (XE_IOCTL_DBG(xe, err))
1167 		return -EFAULT;
1168 
1169 	if (XE_IOCTL_DBG(xe, ext.property >=
1170 			 ARRAY_SIZE(exec_queue_set_property_funcs)) ||
1171 	    XE_IOCTL_DBG(xe, ext.pad) ||
1172 	    XE_IOCTL_DBG(xe, ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY &&
1173 			 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE &&
1174 			 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE &&
1175 			 ext.property != DRM_XE_EXEC_QUEUE_SET_HANG_REPLAY_STATE &&
1176 			 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP &&
1177 			 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY &&
1178 			 ext.property != DRM_XE_EXEC_QUEUE_SET_DISABLE_STATE_CACHE_PERF_FIX))
1179 		return -EINVAL;
1180 
1181 	idx = array_index_nospec(ext.property, ARRAY_SIZE(exec_queue_set_property_funcs));
1182 	if (!exec_queue_set_property_funcs[idx])
1183 		return -EINVAL;
1184 
1185 	if (XE_IOCTL_DBG(xe, *properties & BIT_ULL(idx)))
1186 		return -EINVAL;
1187 
1188 	*properties |= BIT_ULL(idx);
1189 	err = exec_queue_user_ext_check(q, *properties);
1190 	if (XE_IOCTL_DBG(xe, err))
1191 		return err;
1192 
1193 	return exec_queue_set_property_funcs[idx](xe, q, ext.value);
1194 }
1195 
1196 typedef int (*xe_exec_queue_user_extension_fn)(struct xe_device *xe,
1197 					       struct xe_exec_queue *q,
1198 					       u64 extension, u64 *properties);
1199 
1200 static const xe_exec_queue_user_extension_fn exec_queue_user_extension_funcs[] = {
1201 	[DRM_XE_EXEC_QUEUE_EXTENSION_SET_PROPERTY] = exec_queue_user_ext_set_property,
1202 };
1203 
1204 #define MAX_USER_EXTENSIONS	16
1205 static int __exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q,
1206 					u64 extensions, int ext_number, u64 *properties)
1207 {
1208 	u64 __user *address = u64_to_user_ptr(extensions);
1209 	struct drm_xe_user_extension ext;
1210 	int err;
1211 	u32 idx;
1212 
1213 	if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS))
1214 		return -E2BIG;
1215 
1216 	err = copy_from_user(&ext, address, sizeof(ext));
1217 	if (XE_IOCTL_DBG(xe, err))
1218 		return -EFAULT;
1219 
1220 	if (XE_IOCTL_DBG(xe, ext.pad) ||
1221 	    XE_IOCTL_DBG(xe, ext.name >=
1222 			 ARRAY_SIZE(exec_queue_user_extension_funcs)))
1223 		return -EINVAL;
1224 
1225 	idx = array_index_nospec(ext.name,
1226 				 ARRAY_SIZE(exec_queue_user_extension_funcs));
1227 	err = exec_queue_user_extension_funcs[idx](xe, q, extensions, properties);
1228 	if (XE_IOCTL_DBG(xe, err))
1229 		return err;
1230 
1231 	if (ext.next_extension)
1232 		return __exec_queue_user_extensions(xe, q, ext.next_extension,
1233 						    ++ext_number, properties);
1234 
1235 	return 0;
1236 }
1237 
1238 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q,
1239 				      u64 extensions)
1240 {
1241 	u64 properties = 0;
1242 	int err;
1243 
1244 	err = __exec_queue_user_extensions(xe, q, extensions, 0, &properties);
1245 	if (XE_IOCTL_DBG(xe, err))
1246 		return err;
1247 
1248 	err = exec_queue_user_ext_check_final(q, properties);
1249 	if (XE_IOCTL_DBG(xe, err))
1250 		return err;
1251 
1252 	if (xe_exec_queue_is_multi_queue_primary(q)) {
1253 		err = xe_exec_queue_group_init(xe, q);
1254 		if (XE_IOCTL_DBG(xe, err))
1255 			return err;
1256 	}
1257 
1258 	return 0;
1259 }
1260 
1261 static u32 calc_validate_logical_mask(struct xe_device *xe,
1262 				      struct drm_xe_engine_class_instance *eci,
1263 				      u16 width, u16 num_placements)
1264 {
1265 	int len = width * num_placements;
1266 	int i, j, n;
1267 	u16 class;
1268 	u16 gt_id;
1269 	u32 return_mask = 0, prev_mask;
1270 
1271 	if (XE_IOCTL_DBG(xe, !xe_device_uc_enabled(xe) &&
1272 			 len > 1))
1273 		return 0;
1274 
1275 	for (i = 0; i < width; ++i) {
1276 		u32 current_mask = 0;
1277 
1278 		for (j = 0; j < num_placements; ++j) {
1279 			struct xe_hw_engine *hwe;
1280 
1281 			n = j * width + i;
1282 
1283 			hwe = xe_hw_engine_lookup(xe, eci[n]);
1284 			if (XE_IOCTL_DBG(xe, !hwe))
1285 				return 0;
1286 
1287 			if (XE_IOCTL_DBG(xe, xe_hw_engine_is_reserved(hwe)))
1288 				return 0;
1289 
1290 			if (XE_IOCTL_DBG(xe, n && eci[n].gt_id != gt_id) ||
1291 			    XE_IOCTL_DBG(xe, n && eci[n].engine_class != class))
1292 				return 0;
1293 
1294 			class = eci[n].engine_class;
1295 			gt_id = eci[n].gt_id;
1296 
1297 			if (width == 1 || !i)
1298 				return_mask |= BIT(eci[n].engine_instance);
1299 			current_mask |= BIT(eci[n].engine_instance);
1300 		}
1301 
1302 		/* Parallel submissions must be logically contiguous */
1303 		if (i && XE_IOCTL_DBG(xe, current_mask != prev_mask << 1))
1304 			return 0;
1305 
1306 		prev_mask = current_mask;
1307 	}
1308 
1309 	return return_mask;
1310 }
1311 
1312 static bool has_sched_groups(struct xe_gt *gt)
1313 {
1314 	if (IS_SRIOV_PF(gt_to_xe(gt)) && xe_gt_sriov_pf_sched_groups_enabled(gt))
1315 		return true;
1316 
1317 	if (IS_SRIOV_VF(gt_to_xe(gt)) && xe_gt_sriov_vf_sched_groups_enabled(gt))
1318 		return true;
1319 
1320 	return false;
1321 }
1322 
1323 /**
1324  * xe_exec_queue_create_ioctl() - Create an exec queue via IOCTL
1325  * @dev: DRM device
1326  * @data: IOCTL data
1327  * @file: DRM file
1328  *
1329  * Creates a new exec queue based on user-provided parameters. Supports
1330  * creating VM bind queues, regular exec queues, multi-lrc exec queues
1331  * and multi-queue groups.
1332  *
1333  * Return: 0 on success with exec_queue_id filled in, negative error code on failure
1334  */
1335 int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
1336 			       struct drm_file *file)
1337 {
1338 	struct xe_device *xe = to_xe_device(dev);
1339 	struct xe_file *xef = to_xe_file(file);
1340 	struct drm_xe_exec_queue_create *args = data;
1341 	struct drm_xe_engine_class_instance eci[XE_HW_ENGINE_MAX_INSTANCE];
1342 	struct drm_xe_engine_class_instance __user *user_eci =
1343 		u64_to_user_ptr(args->instances);
1344 	struct xe_hw_engine *hwe;
1345 	struct xe_vm *vm;
1346 	struct xe_tile *tile;
1347 	struct xe_exec_queue *q = NULL;
1348 	u32 logical_mask;
1349 	u32 flags = 0;
1350 	u32 id;
1351 	u32 len;
1352 	int err;
1353 
1354 	if (XE_IOCTL_DBG(xe, args->flags & ~DRM_XE_EXEC_QUEUE_LOW_LATENCY_HINT) ||
1355 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1356 		return -EINVAL;
1357 
1358 	len = args->width * args->num_placements;
1359 	if (XE_IOCTL_DBG(xe, !len || len > XE_HW_ENGINE_MAX_INSTANCE))
1360 		return -EINVAL;
1361 
1362 	err = copy_from_user(eci, user_eci,
1363 			     sizeof(struct drm_xe_engine_class_instance) * len);
1364 	if (XE_IOCTL_DBG(xe, err))
1365 		return -EFAULT;
1366 
1367 	if (XE_IOCTL_DBG(xe, !xe_device_get_gt(xe, eci[0].gt_id)))
1368 		return -EINVAL;
1369 
1370 	if (args->flags & DRM_XE_EXEC_QUEUE_LOW_LATENCY_HINT)
1371 		flags |= EXEC_QUEUE_FLAG_LOW_LATENCY;
1372 
1373 	if (eci[0].engine_class == DRM_XE_ENGINE_CLASS_VM_BIND) {
1374 		if (XE_IOCTL_DBG(xe, args->width != 1) ||
1375 		    XE_IOCTL_DBG(xe, args->num_placements != 1) ||
1376 		    XE_IOCTL_DBG(xe, eci[0].engine_instance != 0))
1377 			return -EINVAL;
1378 
1379 		vm = xe_vm_lookup(xef, args->vm_id);
1380 		if (XE_IOCTL_DBG(xe, !vm))
1381 			return -ENOENT;
1382 
1383 		err = down_read_interruptible(&vm->lock);
1384 		if (err) {
1385 			xe_vm_put(vm);
1386 			return err;
1387 		}
1388 
1389 		if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
1390 			up_read(&vm->lock);
1391 			xe_vm_put(vm);
1392 			return -ENOENT;
1393 		}
1394 
1395 		for_each_tile(tile, xe, id) {
1396 			struct xe_exec_queue *new;
1397 
1398 			flags |= EXEC_QUEUE_FLAG_VM;
1399 			if (id)
1400 				flags |= EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD;
1401 
1402 			new = xe_exec_queue_create_bind(xe, tile, vm, flags,
1403 							args->extensions);
1404 			if (IS_ERR(new)) {
1405 				up_read(&vm->lock);
1406 				xe_vm_put(vm);
1407 				err = PTR_ERR(new);
1408 				if (q)
1409 					goto put_exec_queue;
1410 				return err;
1411 			}
1412 			if (id == 0)
1413 				q = new;
1414 			else
1415 				list_add_tail(&new->multi_gt_list,
1416 					      &q->multi_gt_link);
1417 		}
1418 		up_read(&vm->lock);
1419 		xe_vm_put(vm);
1420 	} else {
1421 		logical_mask = calc_validate_logical_mask(xe, eci,
1422 							  args->width,
1423 							  args->num_placements);
1424 		if (XE_IOCTL_DBG(xe, !logical_mask))
1425 			return -EINVAL;
1426 
1427 		hwe = xe_hw_engine_lookup(xe, eci[0]);
1428 		if (XE_IOCTL_DBG(xe, !hwe))
1429 			return -EINVAL;
1430 
1431 		/* multi-lrc is only supported on select engine classes */
1432 		if (XE_IOCTL_DBG(xe, args->width > 1 &&
1433 				 !(xe->info.multi_lrc_mask & BIT(hwe->class))))
1434 			return -EOPNOTSUPP;
1435 
1436 		vm = xe_vm_lookup(xef, args->vm_id);
1437 		if (XE_IOCTL_DBG(xe, !vm))
1438 			return -ENOENT;
1439 
1440 		err = down_read_interruptible(&vm->lock);
1441 		if (err) {
1442 			xe_vm_put(vm);
1443 			return err;
1444 		}
1445 
1446 		if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
1447 			up_read(&vm->lock);
1448 			xe_vm_put(vm);
1449 			return -ENOENT;
1450 		}
1451 
1452 		/* SRIOV sched groups are not compatible with multi-lrc */
1453 		if (XE_IOCTL_DBG(xe, args->width > 1 && has_sched_groups(hwe->gt))) {
1454 			up_read(&vm->lock);
1455 			xe_vm_put(vm);
1456 			return -EINVAL;
1457 		}
1458 
1459 		q = xe_exec_queue_create(xe, vm, logical_mask,
1460 					 args->width, hwe, flags,
1461 					 args->extensions);
1462 		up_read(&vm->lock);
1463 		xe_vm_put(vm);
1464 		if (IS_ERR(q))
1465 			return PTR_ERR(q);
1466 
1467 		if (xe_exec_queue_is_multi_queue_secondary(q)) {
1468 			err = xe_exec_queue_group_add(xe, q);
1469 			if (XE_IOCTL_DBG(xe, err))
1470 				goto put_exec_queue;
1471 		}
1472 
1473 		if (xe_vm_in_preempt_fence_mode(vm)) {
1474 			q->lr.context = dma_fence_context_alloc(1);
1475 
1476 			err = xe_vm_add_compute_exec_queue(vm, q);
1477 			if (XE_IOCTL_DBG(xe, err))
1478 				goto delete_queue_group;
1479 		}
1480 
1481 		if (q->vm && q->hwe->hw_engine_group) {
1482 			err = xe_hw_engine_group_add_exec_queue(q->hwe->hw_engine_group, q);
1483 			if (err)
1484 				goto kill_exec_queue;
1485 		}
1486 	}
1487 
1488 	q->xef = xe_file_get(xef);
1489 	if (eci[0].engine_class != DRM_XE_ENGINE_CLASS_VM_BIND)
1490 		xe_vm_add_exec_queue(vm, q);
1491 
1492 	/* user id alloc must always be last in ioctl to prevent UAF */
1493 	err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
1494 	if (err)
1495 		goto del_hw_engine_group;
1496 
1497 	args->exec_queue_id = id;
1498 
1499 	return 0;
1500 
1501 del_hw_engine_group:
1502 	if (q->vm && q->hwe && q->hwe->hw_engine_group)
1503 		xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
1504 kill_exec_queue:
1505 	xe_exec_queue_kill(q);
1506 delete_queue_group:
1507 	if (xe_exec_queue_is_multi_queue_secondary(q))
1508 		xe_exec_queue_group_delete(xe, q);
1509 put_exec_queue:
1510 	xe_exec_queue_put(q);
1511 	return err;
1512 }
1513 
1514 /**
1515  * xe_exec_queue_get_property_ioctl() - Get a property from an exec queue
1516  * @dev: DRM device
1517  * @data: IOCTL data
1518  * @file: DRM file
1519  *
1520  * Retrieves property values from an existing exec queue. Currently supports
1521  * getting the ban/reset status.
1522  *
1523  * Return: 0 on success with value filled in, negative error code on failure
1524  */
1525 int xe_exec_queue_get_property_ioctl(struct drm_device *dev, void *data,
1526 				     struct drm_file *file)
1527 {
1528 	struct xe_device *xe = to_xe_device(dev);
1529 	struct xe_file *xef = to_xe_file(file);
1530 	struct drm_xe_exec_queue_get_property *args = data;
1531 	struct xe_exec_queue *q;
1532 	int ret;
1533 
1534 	if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1535 		return -EINVAL;
1536 
1537 	q = xe_exec_queue_lookup(xef, args->exec_queue_id);
1538 	if (XE_IOCTL_DBG(xe, !q))
1539 		return -ENOENT;
1540 
1541 	switch (args->property) {
1542 	case DRM_XE_EXEC_QUEUE_GET_PROPERTY_BAN:
1543 		args->value = q->ops->reset_status(q);
1544 		ret = 0;
1545 		break;
1546 	default:
1547 		ret = -EINVAL;
1548 	}
1549 
1550 	xe_exec_queue_put(q);
1551 
1552 	return ret;
1553 }
1554 
1555 /**
1556  * xe_exec_queue_is_lr() - Whether an exec_queue is long-running
1557  * @q: The exec_queue
1558  *
1559  * Return: True if the exec_queue is long-running, false otherwise.
1560  */
1561 bool xe_exec_queue_is_lr(struct xe_exec_queue *q)
1562 {
1563 	return q->vm && xe_vm_in_lr_mode(q->vm) &&
1564 		!(q->flags & EXEC_QUEUE_FLAG_VM);
1565 }
1566 
1567 /**
1568  * xe_exec_queue_is_idle() - Whether an exec_queue is idle.
1569  * @q: The exec_queue
1570  *
1571  * FIXME: Need to determine what to use as the short-lived
1572  * timeline lock for the exec_queues, so that the return value
1573  * of this function becomes more than just an advisory
1574  * snapshot in time. The timeline lock must protect the
1575  * seqno from racing submissions on the same exec_queue.
1576  * Typically vm->resv, but user-created timeline locks use the migrate vm
1577  * and never grabs the migrate vm->resv so we have a race there.
1578  *
1579  * Return: True if the exec_queue is idle, false otherwise.
1580  */
1581 bool xe_exec_queue_is_idle(struct xe_exec_queue *q)
1582 {
1583 	if (xe_exec_queue_is_parallel(q)) {
1584 		int i;
1585 
1586 		for (i = 0; i < q->width; ++i) {
1587 			if (xe_lrc_seqno(q->lrc[i]) !=
1588 			    q->lrc[i]->fence_ctx.next_seqno - 1)
1589 				return false;
1590 		}
1591 
1592 		return true;
1593 	}
1594 
1595 	return xe_lrc_seqno(q->lrc[0]) ==
1596 		q->lrc[0]->fence_ctx.next_seqno - 1;
1597 }
1598 
1599 /**
1600  * xe_exec_queue_update_run_ticks() - Update run time in ticks for this exec queue
1601  * from hw
1602  * @q: The exec queue
1603  *
1604  * Update the timestamp saved by HW for this exec queue and save run ticks
1605  * calculated by using the delta from last update.
1606  */
1607 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
1608 {
1609 	struct xe_device *xe = gt_to_xe(q->gt);
1610 	struct xe_lrc *lrc;
1611 	u64 old_ts, new_ts;
1612 	int idx;
1613 
1614 	/*
1615 	 * Jobs that are executed by kernel doesn't have a corresponding xe_file
1616 	 * and thus are not accounted.
1617 	 */
1618 	if (!q->xef)
1619 		return;
1620 
1621 	/* Synchronize with unbind while holding the xe file open */
1622 	if (!drm_dev_enter(&xe->drm, &idx))
1623 		return;
1624 	/*
1625 	 * Only sample the first LRC. For parallel submission, all of them are
1626 	 * scheduled together and we compensate that below by multiplying by
1627 	 * width - this may introduce errors if that premise is not true and
1628 	 * they don't exit 100% aligned. On the other hand, looping through
1629 	 * the LRCs and reading them in different time could also introduce
1630 	 * errors.
1631 	 */
1632 	lrc = q->lrc[0];
1633 	xe_bo_lock(lrc->bo, false);
1634 	if (!xe_bo_is_purged(lrc->bo)) {
1635 		new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
1636 		q->xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
1637 	}
1638 	xe_bo_unlock(lrc->bo);
1639 
1640 	drm_dev_exit(idx);
1641 }
1642 
1643 /**
1644  * xe_exec_queue_kill - permanently stop all execution from an exec queue
1645  * @q: The exec queue
1646  *
1647  * This function permanently stops all activity on an exec queue. If the queue
1648  * is actively executing on the HW, it will be kicked off the engine; any
1649  * pending jobs are discarded and all future submissions are rejected.
1650  * This function is safe to call multiple times.
1651  */
1652 void xe_exec_queue_kill(struct xe_exec_queue *q)
1653 {
1654 	struct xe_exec_queue *eq = q, *next;
1655 
1656 	list_for_each_entry_safe(eq, next, &eq->multi_gt_list,
1657 				 multi_gt_link) {
1658 		q->ops->kill(eq);
1659 		xe_vm_remove_compute_exec_queue(q->vm, eq);
1660 	}
1661 
1662 	q->ops->kill(q);
1663 	xe_vm_remove_compute_exec_queue(q->vm, q);
1664 }
1665 
1666 /**
1667  * xe_exec_queue_destroy_ioctl() - Destroy an exec queue via IOCTL
1668  * @dev: DRM device
1669  * @data: IOCTL data
1670  * @file: DRM file
1671  *
1672  * Destroys an existing exec queue and releases its reference.
1673  *
1674  * Return: 0 on success, negative error code on failure
1675  */
1676 int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data,
1677 				struct drm_file *file)
1678 {
1679 	struct xe_device *xe = to_xe_device(dev);
1680 	struct xe_file *xef = to_xe_file(file);
1681 	struct drm_xe_exec_queue_destroy *args = data;
1682 	struct xe_exec_queue *q;
1683 
1684 	if (XE_IOCTL_DBG(xe, args->pad) ||
1685 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1686 		return -EINVAL;
1687 
1688 	mutex_lock(&xef->exec_queue.lock);
1689 	q = xa_erase(&xef->exec_queue.xa, args->exec_queue_id);
1690 	if (q)
1691 		atomic_inc(&xef->exec_queue.pending_removal);
1692 	mutex_unlock(&xef->exec_queue.lock);
1693 
1694 	if (XE_IOCTL_DBG(xe, !q))
1695 		return -ENOENT;
1696 
1697 	if (q->vm && q->hwe->hw_engine_group)
1698 		xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
1699 
1700 	xe_exec_queue_kill(q);
1701 
1702 	trace_xe_exec_queue_close(q);
1703 	xe_exec_queue_put(q);
1704 
1705 	return 0;
1706 }
1707 
1708 static void xe_exec_queue_last_fence_lockdep_assert(struct xe_exec_queue *q,
1709 						    struct xe_vm *vm)
1710 {
1711 	if (q->flags & EXEC_QUEUE_FLAG_MIGRATE) {
1712 		xe_migrate_job_lock_assert(q);
1713 	} else if (q->flags & EXEC_QUEUE_FLAG_VM) {
1714 		lockdep_assert_held(&vm->lock);
1715 	} else {
1716 		xe_vm_assert_held(vm);
1717 		lockdep_assert_held(&q->hwe->hw_engine_group->mode_sem);
1718 	}
1719 }
1720 
1721 /**
1722  * xe_exec_queue_last_fence_put() - Drop ref to last fence
1723  * @q: The exec queue
1724  * @vm: The VM the engine does a bind or exec for
1725  */
1726 void xe_exec_queue_last_fence_put(struct xe_exec_queue *q, struct xe_vm *vm)
1727 {
1728 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1729 
1730 	xe_exec_queue_last_fence_put_unlocked(q);
1731 }
1732 
1733 /**
1734  * xe_exec_queue_last_fence_put_unlocked() - Drop ref to last fence unlocked
1735  * @q: The exec queue
1736  *
1737  * Only safe to be called from xe_exec_queue_destroy().
1738  */
1739 void xe_exec_queue_last_fence_put_unlocked(struct xe_exec_queue *q)
1740 {
1741 	if (q->last_fence) {
1742 		dma_fence_put(q->last_fence);
1743 		q->last_fence = NULL;
1744 	}
1745 }
1746 
1747 /**
1748  * xe_exec_queue_last_fence_get() - Get last fence
1749  * @q: The exec queue
1750  * @vm: The VM the engine does a bind or exec for
1751  *
1752  * Get last fence, takes a ref
1753  *
1754  * Returns: last fence if not signaled, dma fence stub if signaled
1755  */
1756 struct dma_fence *xe_exec_queue_last_fence_get(struct xe_exec_queue *q,
1757 					       struct xe_vm *vm)
1758 {
1759 	struct dma_fence *fence;
1760 
1761 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1762 
1763 	if (q->last_fence &&
1764 	    test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
1765 		xe_exec_queue_last_fence_put(q, vm);
1766 
1767 	fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
1768 	dma_fence_get(fence);
1769 	return fence;
1770 }
1771 
1772 /**
1773  * xe_exec_queue_last_fence_get_for_resume() - Get last fence
1774  * @q: The exec queue
1775  * @vm: The VM the engine does a bind or exec for
1776  *
1777  * Get last fence, takes a ref. Only safe to be called in the context of
1778  * resuming the hw engine group's long-running exec queue, when the group
1779  * semaphore is held.
1780  *
1781  * Returns: last fence if not signaled, dma fence stub if signaled
1782  */
1783 struct dma_fence *xe_exec_queue_last_fence_get_for_resume(struct xe_exec_queue *q,
1784 							  struct xe_vm *vm)
1785 {
1786 	struct dma_fence *fence;
1787 
1788 	lockdep_assert_held_write(&q->hwe->hw_engine_group->mode_sem);
1789 
1790 	if (q->last_fence &&
1791 	    test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
1792 		xe_exec_queue_last_fence_put_unlocked(q);
1793 
1794 	fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
1795 	dma_fence_get(fence);
1796 	return fence;
1797 }
1798 
1799 /**
1800  * xe_exec_queue_last_fence_set() - Set last fence
1801  * @q: The exec queue
1802  * @vm: The VM the engine does a bind or exec for
1803  * @fence: The fence
1804  *
1805  * Set the last fence for the engine. Increases reference count for fence, when
1806  * closing engine xe_exec_queue_last_fence_put should be called.
1807  */
1808 void xe_exec_queue_last_fence_set(struct xe_exec_queue *q, struct xe_vm *vm,
1809 				  struct dma_fence *fence)
1810 {
1811 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1812 	xe_assert(vm->xe, !dma_fence_is_container(fence));
1813 
1814 	xe_exec_queue_last_fence_put(q, vm);
1815 	q->last_fence = dma_fence_get(fence);
1816 }
1817 
1818 /**
1819  * xe_exec_queue_tlb_inval_last_fence_put() - Drop ref to last TLB invalidation fence
1820  * @q: The exec queue
1821  * @vm: The VM the engine does a bind for
1822  * @type: Either primary or media GT
1823  */
1824 void xe_exec_queue_tlb_inval_last_fence_put(struct xe_exec_queue *q,
1825 					    struct xe_vm *vm,
1826 					    unsigned int type)
1827 {
1828 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1829 	xe_assert(vm->xe, type == XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT ||
1830 		  type == XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT);
1831 
1832 	xe_exec_queue_tlb_inval_last_fence_put_unlocked(q, type);
1833 }
1834 
1835 /**
1836  * xe_exec_queue_tlb_inval_last_fence_put_unlocked() - Drop ref to last TLB
1837  * invalidation fence unlocked
1838  * @q: The exec queue
1839  * @type: Either primary or media GT
1840  *
1841  * Only safe to be called from xe_exec_queue_destroy().
1842  */
1843 void xe_exec_queue_tlb_inval_last_fence_put_unlocked(struct xe_exec_queue *q,
1844 						     unsigned int type)
1845 {
1846 	xe_assert(gt_to_xe(q->gt), type == XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT ||
1847 		  type == XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT);
1848 
1849 	dma_fence_put(q->tlb_inval[type].last_fence);
1850 	q->tlb_inval[type].last_fence = NULL;
1851 }
1852 
1853 /**
1854  * xe_exec_queue_tlb_inval_last_fence_get() - Get last fence for TLB invalidation
1855  * @q: The exec queue
1856  * @vm: The VM the engine does a bind for
1857  * @type: Either primary or media GT
1858  *
1859  * Get last fence, takes a ref
1860  *
1861  * Returns: last fence if not signaled, dma fence stub if signaled
1862  */
1863 struct dma_fence *xe_exec_queue_tlb_inval_last_fence_get(struct xe_exec_queue *q,
1864 							 struct xe_vm *vm,
1865 							 unsigned int type)
1866 {
1867 	struct dma_fence *fence;
1868 
1869 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1870 	xe_assert(vm->xe, type == XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT ||
1871 		  type == XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT);
1872 	xe_assert(vm->xe, q->flags & (EXEC_QUEUE_FLAG_VM |
1873 				      EXEC_QUEUE_FLAG_MIGRATE));
1874 
1875 	if (q->tlb_inval[type].last_fence &&
1876 	    test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
1877 		     &q->tlb_inval[type].last_fence->flags))
1878 		xe_exec_queue_tlb_inval_last_fence_put(q, vm, type);
1879 
1880 	fence = q->tlb_inval[type].last_fence ?: dma_fence_get_stub();
1881 	dma_fence_get(fence);
1882 	return fence;
1883 }
1884 
1885 /**
1886  * xe_exec_queue_tlb_inval_last_fence_set() - Set last fence for TLB invalidation
1887  * @q: The exec queue
1888  * @vm: The VM the engine does a bind for
1889  * @fence: The fence
1890  * @type: Either primary or media GT
1891  *
1892  * Set the last fence for the tlb invalidation type on the queue. Increases
1893  * reference count for fence, when closing queue
1894  * xe_exec_queue_tlb_inval_last_fence_put should be called.
1895  */
1896 void xe_exec_queue_tlb_inval_last_fence_set(struct xe_exec_queue *q,
1897 					    struct xe_vm *vm,
1898 					    struct dma_fence *fence,
1899 					    unsigned int type)
1900 {
1901 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1902 	xe_assert(vm->xe, type == XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT ||
1903 		  type == XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT);
1904 	xe_assert(vm->xe, q->flags & (EXEC_QUEUE_FLAG_VM |
1905 				      EXEC_QUEUE_FLAG_MIGRATE));
1906 	xe_assert(vm->xe, !dma_fence_is_container(fence));
1907 
1908 	xe_exec_queue_tlb_inval_last_fence_put(q, vm, type);
1909 	q->tlb_inval[type].last_fence = dma_fence_get(fence);
1910 }
1911 
1912 /**
1913  * xe_exec_queue_contexts_hwsp_rebase - Re-compute GGTT references
1914  * within all LRCs of a queue.
1915  * @q: the &xe_exec_queue struct instance containing target LRCs
1916  * @scratch: scratch buffer to be used as temporary storage
1917  *
1918  * Returns: zero on success, negative error code on failure
1919  */
1920 int xe_exec_queue_contexts_hwsp_rebase(struct xe_exec_queue *q, void *scratch)
1921 {
1922 	int i;
1923 	int err = 0;
1924 
1925 	for (i = 0; i < q->width; ++i) {
1926 		struct xe_lrc *lrc;
1927 
1928 		lrc = xe_exec_queue_get_lrc(q, i);
1929 		if (!lrc)
1930 			continue;
1931 
1932 		xe_lrc_update_memirq_regs_with_address(lrc, q->hwe, scratch);
1933 		xe_lrc_update_hwctx_regs_with_address(lrc);
1934 		err = xe_lrc_setup_wa_bb_with_scratch(lrc, q->hwe, scratch);
1935 		xe_lrc_put(lrc);
1936 		if (err)
1937 			break;
1938 	}
1939 
1940 	return err;
1941 }
1942