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