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