1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
6 #include "xe_exec_queue.h"
8 #include <linux/nospec.h>
10 #include <drm/drm_device.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_file.h>
13 #include <uapi/drm/xe_drm.h>
15 #include "xe_device.h"
17 #include "xe_hw_engine_class_sysfs.h"
18 #include "xe_hw_engine_group.h"
19 #include "xe_hw_fence.h"
21 #include "xe_macros.h"
22 #include "xe_migrate.h"
24 #include "xe_ring_ops_types.h"
28 enum xe_exec_queue_sched_prop {
29 XE_EXEC_QUEUE_JOB_TIMEOUT = 0,
30 XE_EXEC_QUEUE_TIMESLICE = 1,
31 XE_EXEC_QUEUE_PREEMPT_TIMEOUT = 2,
32 XE_EXEC_QUEUE_SCHED_PROP_MAX = 3,
35 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q,
36 u64 extensions, int ext_number);
38 static void __xe_exec_queue_free(struct xe_exec_queue *q)
49 static struct xe_exec_queue *__xe_exec_queue_alloc(struct xe_device *xe,
52 u16 width, struct xe_hw_engine *hwe,
53 u32 flags, u64 extensions)
55 struct xe_exec_queue *q;
56 struct xe_gt *gt = hwe->gt;
59 /* only kernel queues can be permanent */
60 XE_WARN_ON((flags & EXEC_QUEUE_FLAG_PERMANENT) && !(flags & EXEC_QUEUE_FLAG_KERNEL));
62 q = kzalloc(struct_size(q, lrc, width), GFP_KERNEL);
64 return ERR_PTR(-ENOMEM);
66 kref_init(&q->refcount);
70 q->class = hwe->class;
72 q->logical_mask = logical_mask;
73 q->fence_irq = >->fence_irq[hwe->class];
74 q->ring_ops = gt->ring_ops[hwe->class];
75 q->ops = gt->exec_queue_ops;
76 INIT_LIST_HEAD(&q->lr.link);
77 INIT_LIST_HEAD(&q->multi_gt_link);
78 INIT_LIST_HEAD(&q->hw_engine_group_link);
80 q->sched_props.timeslice_us = hwe->eclass->sched_props.timeslice_us;
81 q->sched_props.preempt_timeout_us =
82 hwe->eclass->sched_props.preempt_timeout_us;
83 q->sched_props.job_timeout_ms =
84 hwe->eclass->sched_props.job_timeout_ms;
85 if (q->flags & EXEC_QUEUE_FLAG_KERNEL &&
86 q->flags & EXEC_QUEUE_FLAG_HIGH_PRIORITY)
87 q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_KERNEL;
89 q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_NORMAL;
92 q->vm = xe_vm_get(vm);
96 * may set q->usm, must come before xe_lrc_create(),
97 * may overwrite q->sched_props, must come before q->ops->init()
99 err = exec_queue_user_extensions(xe, q, extensions, 0);
101 __xe_exec_queue_free(q);
109 static int __xe_exec_queue_init(struct xe_exec_queue *q)
111 struct xe_vm *vm = q->vm;
115 err = xe_vm_lock(vm, true);
120 for (i = 0; i < q->width; ++i) {
121 q->lrc[i] = xe_lrc_create(q->hwe, q->vm, SZ_16K);
122 if (IS_ERR(q->lrc[i])) {
123 err = PTR_ERR(q->lrc[i]);
131 err = q->ops->init(q);
141 for (i = i - 1; i >= 0; --i)
142 xe_lrc_put(q->lrc[i]);
146 struct xe_exec_queue *xe_exec_queue_create(struct xe_device *xe, struct xe_vm *vm,
147 u32 logical_mask, u16 width,
148 struct xe_hw_engine *hwe, u32 flags,
151 struct xe_exec_queue *q;
154 q = __xe_exec_queue_alloc(xe, vm, logical_mask, width, hwe, flags,
159 err = __xe_exec_queue_init(q);
166 __xe_exec_queue_free(q);
170 struct xe_exec_queue *xe_exec_queue_create_class(struct xe_device *xe, struct xe_gt *gt,
172 enum xe_engine_class class,
173 u32 flags, u64 extensions)
175 struct xe_hw_engine *hwe, *hwe0 = NULL;
176 enum xe_hw_engine_id id;
177 u32 logical_mask = 0;
179 for_each_hw_engine(hwe, gt, id) {
180 if (xe_hw_engine_is_reserved(hwe))
183 if (hwe->class == class) {
184 logical_mask |= BIT(hwe->logical_instance);
191 return ERR_PTR(-ENODEV);
193 return xe_exec_queue_create(xe, vm, logical_mask, 1, hwe0, flags, extensions);
197 * xe_exec_queue_create_bind() - Create bind exec queue.
199 * @tile: tile which bind exec queue belongs to.
200 * @flags: exec queue creation flags
201 * @extensions: exec queue creation extensions
203 * Normalize bind exec queue creation. Bind exec queue is tied to migration VM
204 * for access to physical memory required for page table programming. On a
205 * faulting devices the reserved copy engine instance must be used to avoid
206 * deadlocking (user binds cannot get stuck behind faults as kernel binds which
207 * resolve faults depend on user binds). On non-faulting devices any copy engine
210 * Returns exec queue on success, ERR_PTR on failure
212 struct xe_exec_queue *xe_exec_queue_create_bind(struct xe_device *xe,
213 struct xe_tile *tile,
214 u32 flags, u64 extensions)
216 struct xe_gt *gt = tile->primary_gt;
217 struct xe_exec_queue *q;
218 struct xe_vm *migrate_vm;
220 migrate_vm = xe_migrate_get_vm(tile->migrate);
221 if (xe->info.has_usm) {
222 struct xe_hw_engine *hwe = xe_gt_hw_engine(gt,
223 XE_ENGINE_CLASS_COPY,
224 gt->usm.reserved_bcs_instance,
228 xe_vm_put(migrate_vm);
229 return ERR_PTR(-EINVAL);
232 q = xe_exec_queue_create(xe, migrate_vm,
233 BIT(hwe->logical_instance), 1, hwe,
236 q = xe_exec_queue_create_class(xe, gt, migrate_vm,
237 XE_ENGINE_CLASS_COPY, flags,
240 xe_vm_put(migrate_vm);
245 void xe_exec_queue_destroy(struct kref *ref)
247 struct xe_exec_queue *q = container_of(ref, struct xe_exec_queue, refcount);
248 struct xe_exec_queue *eq, *next;
250 xe_exec_queue_last_fence_put_unlocked(q);
251 if (!(q->flags & EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD)) {
252 list_for_each_entry_safe(eq, next, &q->multi_gt_list,
254 xe_exec_queue_put(eq);
260 void xe_exec_queue_fini(struct xe_exec_queue *q)
265 * Before releasing our ref to lrc and xef, accumulate our run ticks
267 xe_exec_queue_update_run_ticks(q);
269 for (i = 0; i < q->width; ++i)
270 xe_lrc_put(q->lrc[i]);
272 __xe_exec_queue_free(q);
275 void xe_exec_queue_assign_name(struct xe_exec_queue *q, u32 instance)
278 case XE_ENGINE_CLASS_RENDER:
279 snprintf(q->name, sizeof(q->name), "rcs%d", instance);
281 case XE_ENGINE_CLASS_VIDEO_DECODE:
282 snprintf(q->name, sizeof(q->name), "vcs%d", instance);
284 case XE_ENGINE_CLASS_VIDEO_ENHANCE:
285 snprintf(q->name, sizeof(q->name), "vecs%d", instance);
287 case XE_ENGINE_CLASS_COPY:
288 snprintf(q->name, sizeof(q->name), "bcs%d", instance);
290 case XE_ENGINE_CLASS_COMPUTE:
291 snprintf(q->name, sizeof(q->name), "ccs%d", instance);
293 case XE_ENGINE_CLASS_OTHER:
294 snprintf(q->name, sizeof(q->name), "gsccs%d", instance);
297 XE_WARN_ON(q->class);
301 struct xe_exec_queue *xe_exec_queue_lookup(struct xe_file *xef, u32 id)
303 struct xe_exec_queue *q;
305 mutex_lock(&xef->exec_queue.lock);
306 q = xa_load(&xef->exec_queue.xa, id);
308 xe_exec_queue_get(q);
309 mutex_unlock(&xef->exec_queue.lock);
314 enum xe_exec_queue_priority
315 xe_exec_queue_device_get_max_priority(struct xe_device *xe)
317 return capable(CAP_SYS_NICE) ? XE_EXEC_QUEUE_PRIORITY_HIGH :
318 XE_EXEC_QUEUE_PRIORITY_NORMAL;
321 static int exec_queue_set_priority(struct xe_device *xe, struct xe_exec_queue *q,
324 if (XE_IOCTL_DBG(xe, value > XE_EXEC_QUEUE_PRIORITY_HIGH))
327 if (XE_IOCTL_DBG(xe, value > xe_exec_queue_device_get_max_priority(xe)))
330 q->sched_props.priority = value;
334 static bool xe_exec_queue_enforce_schedule_limit(void)
336 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT)
339 return !capable(CAP_SYS_NICE);
344 xe_exec_queue_get_prop_minmax(struct xe_hw_engine_class_intf *eclass,
345 enum xe_exec_queue_sched_prop prop,
349 case XE_EXEC_QUEUE_JOB_TIMEOUT:
350 *min = eclass->sched_props.job_timeout_min;
351 *max = eclass->sched_props.job_timeout_max;
353 case XE_EXEC_QUEUE_TIMESLICE:
354 *min = eclass->sched_props.timeslice_min;
355 *max = eclass->sched_props.timeslice_max;
357 case XE_EXEC_QUEUE_PREEMPT_TIMEOUT:
358 *min = eclass->sched_props.preempt_timeout_min;
359 *max = eclass->sched_props.preempt_timeout_max;
364 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT)
365 if (capable(CAP_SYS_NICE)) {
367 case XE_EXEC_QUEUE_JOB_TIMEOUT:
368 *min = XE_HW_ENGINE_JOB_TIMEOUT_MIN;
369 *max = XE_HW_ENGINE_JOB_TIMEOUT_MAX;
371 case XE_EXEC_QUEUE_TIMESLICE:
372 *min = XE_HW_ENGINE_TIMESLICE_MIN;
373 *max = XE_HW_ENGINE_TIMESLICE_MAX;
375 case XE_EXEC_QUEUE_PREEMPT_TIMEOUT:
376 *min = XE_HW_ENGINE_PREEMPT_TIMEOUT_MIN;
377 *max = XE_HW_ENGINE_PREEMPT_TIMEOUT_MAX;
386 static int exec_queue_set_timeslice(struct xe_device *xe, struct xe_exec_queue *q,
389 u32 min = 0, max = 0;
391 xe_exec_queue_get_prop_minmax(q->hwe->eclass,
392 XE_EXEC_QUEUE_TIMESLICE, &min, &max);
394 if (xe_exec_queue_enforce_schedule_limit() &&
395 !xe_hw_engine_timeout_in_range(value, min, max))
398 q->sched_props.timeslice_us = value;
402 typedef int (*xe_exec_queue_set_property_fn)(struct xe_device *xe,
403 struct xe_exec_queue *q,
406 static const xe_exec_queue_set_property_fn exec_queue_set_property_funcs[] = {
407 [DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY] = exec_queue_set_priority,
408 [DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE] = exec_queue_set_timeslice,
411 static int exec_queue_user_ext_set_property(struct xe_device *xe,
412 struct xe_exec_queue *q,
415 u64 __user *address = u64_to_user_ptr(extension);
416 struct drm_xe_ext_set_property ext;
420 err = __copy_from_user(&ext, address, sizeof(ext));
421 if (XE_IOCTL_DBG(xe, err))
424 if (XE_IOCTL_DBG(xe, ext.property >=
425 ARRAY_SIZE(exec_queue_set_property_funcs)) ||
426 XE_IOCTL_DBG(xe, ext.pad) ||
427 XE_IOCTL_DBG(xe, ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY &&
428 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE))
431 idx = array_index_nospec(ext.property, ARRAY_SIZE(exec_queue_set_property_funcs));
432 if (!exec_queue_set_property_funcs[idx])
435 return exec_queue_set_property_funcs[idx](xe, q, ext.value);
438 typedef int (*xe_exec_queue_user_extension_fn)(struct xe_device *xe,
439 struct xe_exec_queue *q,
442 static const xe_exec_queue_user_extension_fn exec_queue_user_extension_funcs[] = {
443 [DRM_XE_EXEC_QUEUE_EXTENSION_SET_PROPERTY] = exec_queue_user_ext_set_property,
446 #define MAX_USER_EXTENSIONS 16
447 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q,
448 u64 extensions, int ext_number)
450 u64 __user *address = u64_to_user_ptr(extensions);
451 struct drm_xe_user_extension ext;
455 if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS))
458 err = __copy_from_user(&ext, address, sizeof(ext));
459 if (XE_IOCTL_DBG(xe, err))
462 if (XE_IOCTL_DBG(xe, ext.pad) ||
463 XE_IOCTL_DBG(xe, ext.name >=
464 ARRAY_SIZE(exec_queue_user_extension_funcs)))
467 idx = array_index_nospec(ext.name,
468 ARRAY_SIZE(exec_queue_user_extension_funcs));
469 err = exec_queue_user_extension_funcs[idx](xe, q, extensions);
470 if (XE_IOCTL_DBG(xe, err))
473 if (ext.next_extension)
474 return exec_queue_user_extensions(xe, q, ext.next_extension,
480 static u32 calc_validate_logical_mask(struct xe_device *xe, struct xe_gt *gt,
481 struct drm_xe_engine_class_instance *eci,
482 u16 width, u16 num_placements)
484 int len = width * num_placements;
488 u32 return_mask = 0, prev_mask;
490 if (XE_IOCTL_DBG(xe, !xe_device_uc_enabled(xe) &&
494 for (i = 0; i < width; ++i) {
495 u32 current_mask = 0;
497 for (j = 0; j < num_placements; ++j) {
498 struct xe_hw_engine *hwe;
502 hwe = xe_hw_engine_lookup(xe, eci[n]);
503 if (XE_IOCTL_DBG(xe, !hwe))
506 if (XE_IOCTL_DBG(xe, xe_hw_engine_is_reserved(hwe)))
509 if (XE_IOCTL_DBG(xe, n && eci[n].gt_id != gt_id) ||
510 XE_IOCTL_DBG(xe, n && eci[n].engine_class != class))
513 class = eci[n].engine_class;
514 gt_id = eci[n].gt_id;
516 if (width == 1 || !i)
517 return_mask |= BIT(eci[n].engine_instance);
518 current_mask |= BIT(eci[n].engine_instance);
521 /* Parallel submissions must be logically contiguous */
522 if (i && XE_IOCTL_DBG(xe, current_mask != prev_mask << 1))
525 prev_mask = current_mask;
531 int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
532 struct drm_file *file)
534 struct xe_device *xe = to_xe_device(dev);
535 struct xe_file *xef = to_xe_file(file);
536 struct drm_xe_exec_queue_create *args = data;
537 struct drm_xe_engine_class_instance eci[XE_HW_ENGINE_MAX_INSTANCE];
538 struct drm_xe_engine_class_instance __user *user_eci =
539 u64_to_user_ptr(args->instances);
540 struct xe_hw_engine *hwe;
543 struct xe_tile *tile;
544 struct xe_exec_queue *q = NULL;
550 if (XE_IOCTL_DBG(xe, args->flags) ||
551 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
554 len = args->width * args->num_placements;
555 if (XE_IOCTL_DBG(xe, !len || len > XE_HW_ENGINE_MAX_INSTANCE))
558 err = __copy_from_user(eci, user_eci,
559 sizeof(struct drm_xe_engine_class_instance) *
561 if (XE_IOCTL_DBG(xe, err))
564 if (XE_IOCTL_DBG(xe, eci[0].gt_id >= xe->info.gt_count))
567 if (eci[0].engine_class == DRM_XE_ENGINE_CLASS_VM_BIND) {
568 if (XE_IOCTL_DBG(xe, args->width != 1) ||
569 XE_IOCTL_DBG(xe, args->num_placements != 1) ||
570 XE_IOCTL_DBG(xe, eci[0].engine_instance != 0))
573 for_each_tile(tile, xe, id) {
574 struct xe_exec_queue *new;
575 u32 flags = EXEC_QUEUE_FLAG_VM;
578 flags |= EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD;
580 new = xe_exec_queue_create_bind(xe, tile, flags,
591 list_add_tail(&new->multi_gt_list,
595 gt = xe_device_get_gt(xe, eci[0].gt_id);
596 logical_mask = calc_validate_logical_mask(xe, gt, eci,
598 args->num_placements);
599 if (XE_IOCTL_DBG(xe, !logical_mask))
602 hwe = xe_hw_engine_lookup(xe, eci[0]);
603 if (XE_IOCTL_DBG(xe, !hwe))
606 vm = xe_vm_lookup(xef, args->vm_id);
607 if (XE_IOCTL_DBG(xe, !vm))
610 err = down_read_interruptible(&vm->lock);
616 if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
622 q = xe_exec_queue_create(xe, vm, logical_mask,
630 if (xe_vm_in_preempt_fence_mode(vm)) {
631 q->lr.context = dma_fence_context_alloc(1);
633 err = xe_vm_add_compute_exec_queue(vm, q);
634 if (XE_IOCTL_DBG(xe, err))
638 if (q->vm && q->hwe->hw_engine_group) {
639 err = xe_hw_engine_group_add_exec_queue(q->hwe->hw_engine_group, q);
645 q->xef = xe_file_get(xef);
647 /* user id alloc must always be last in ioctl to prevent UAF */
648 err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
650 goto kill_exec_queue;
652 args->exec_queue_id = id;
657 xe_exec_queue_kill(q);
659 xe_exec_queue_put(q);
663 int xe_exec_queue_get_property_ioctl(struct drm_device *dev, void *data,
664 struct drm_file *file)
666 struct xe_device *xe = to_xe_device(dev);
667 struct xe_file *xef = to_xe_file(file);
668 struct drm_xe_exec_queue_get_property *args = data;
669 struct xe_exec_queue *q;
672 if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
675 q = xe_exec_queue_lookup(xef, args->exec_queue_id);
676 if (XE_IOCTL_DBG(xe, !q))
679 switch (args->property) {
680 case DRM_XE_EXEC_QUEUE_GET_PROPERTY_BAN:
681 args->value = q->ops->reset_status(q);
688 xe_exec_queue_put(q);
694 * xe_exec_queue_is_lr() - Whether an exec_queue is long-running
697 * Return: True if the exec_queue is long-running, false otherwise.
699 bool xe_exec_queue_is_lr(struct xe_exec_queue *q)
701 return q->vm && xe_vm_in_lr_mode(q->vm) &&
702 !(q->flags & EXEC_QUEUE_FLAG_VM);
705 static s32 xe_exec_queue_num_job_inflight(struct xe_exec_queue *q)
707 return q->lrc[0]->fence_ctx.next_seqno - xe_lrc_seqno(q->lrc[0]) - 1;
711 * xe_exec_queue_ring_full() - Whether an exec_queue's ring is full
714 * Return: True if the exec_queue's ring is full, false otherwise.
716 bool xe_exec_queue_ring_full(struct xe_exec_queue *q)
718 struct xe_lrc *lrc = q->lrc[0];
719 s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
721 return xe_exec_queue_num_job_inflight(q) >= max_job;
725 * xe_exec_queue_is_idle() - Whether an exec_queue is idle.
728 * FIXME: Need to determine what to use as the short-lived
729 * timeline lock for the exec_queues, so that the return value
730 * of this function becomes more than just an advisory
731 * snapshot in time. The timeline lock must protect the
732 * seqno from racing submissions on the same exec_queue.
733 * Typically vm->resv, but user-created timeline locks use the migrate vm
734 * and never grabs the migrate vm->resv so we have a race there.
736 * Return: True if the exec_queue is idle, false otherwise.
738 bool xe_exec_queue_is_idle(struct xe_exec_queue *q)
740 if (xe_exec_queue_is_parallel(q)) {
743 for (i = 0; i < q->width; ++i) {
744 if (xe_lrc_seqno(q->lrc[i]) !=
745 q->lrc[i]->fence_ctx.next_seqno - 1)
752 return xe_lrc_seqno(q->lrc[0]) ==
753 q->lrc[0]->fence_ctx.next_seqno - 1;
757 * xe_exec_queue_update_run_ticks() - Update run time in ticks for this exec queue
761 * Update the timestamp saved by HW for this exec queue and save run ticks
762 * calculated by using the delta from last update.
764 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
766 struct xe_device *xe = gt_to_xe(q->gt);
773 * Jobs that are run during driver load may use an exec_queue, but are
774 * not associated with a user xe file, so avoid accumulating busyness
775 * for kernel specific work.
777 if (!q->vm || !q->vm->xef)
780 /* Synchronize with unbind while holding the xe file open */
781 if (!drm_dev_enter(&xe->drm, &idx))
787 * Only sample the first LRC. For parallel submission, all of them are
788 * scheduled together and we compensate that below by multiplying by
789 * width - this may introduce errors if that premise is not true and
790 * they don't exit 100% aligned. On the other hand, looping through
791 * the LRCs and reading them in different time could also introduce
795 new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
796 xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
802 * xe_exec_queue_kill - permanently stop all execution from an exec queue
805 * This function permanently stops all activity on an exec queue. If the queue
806 * is actively executing on the HW, it will be kicked off the engine; any
807 * pending jobs are discarded and all future submissions are rejected.
808 * This function is safe to call multiple times.
810 void xe_exec_queue_kill(struct xe_exec_queue *q)
812 struct xe_exec_queue *eq = q, *next;
814 list_for_each_entry_safe(eq, next, &eq->multi_gt_list,
817 xe_vm_remove_compute_exec_queue(q->vm, eq);
821 xe_vm_remove_compute_exec_queue(q->vm, q);
824 int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data,
825 struct drm_file *file)
827 struct xe_device *xe = to_xe_device(dev);
828 struct xe_file *xef = to_xe_file(file);
829 struct drm_xe_exec_queue_destroy *args = data;
830 struct xe_exec_queue *q;
832 if (XE_IOCTL_DBG(xe, args->pad) ||
833 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
836 mutex_lock(&xef->exec_queue.lock);
837 q = xa_erase(&xef->exec_queue.xa, args->exec_queue_id);
838 mutex_unlock(&xef->exec_queue.lock);
839 if (XE_IOCTL_DBG(xe, !q))
842 if (q->vm && q->hwe->hw_engine_group)
843 xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
845 xe_exec_queue_kill(q);
847 trace_xe_exec_queue_close(q);
848 xe_exec_queue_put(q);
853 static void xe_exec_queue_last_fence_lockdep_assert(struct xe_exec_queue *q,
856 if (q->flags & EXEC_QUEUE_FLAG_VM) {
857 lockdep_assert_held(&vm->lock);
859 xe_vm_assert_held(vm);
860 lockdep_assert_held(&q->hwe->hw_engine_group->mode_sem);
865 * xe_exec_queue_last_fence_put() - Drop ref to last fence
867 * @vm: The VM the engine does a bind or exec for
869 void xe_exec_queue_last_fence_put(struct xe_exec_queue *q, struct xe_vm *vm)
871 xe_exec_queue_last_fence_lockdep_assert(q, vm);
873 xe_exec_queue_last_fence_put_unlocked(q);
877 * xe_exec_queue_last_fence_put_unlocked() - Drop ref to last fence unlocked
880 * Only safe to be called from xe_exec_queue_destroy().
882 void xe_exec_queue_last_fence_put_unlocked(struct xe_exec_queue *q)
885 dma_fence_put(q->last_fence);
886 q->last_fence = NULL;
891 * xe_exec_queue_last_fence_get() - Get last fence
893 * @vm: The VM the engine does a bind or exec for
895 * Get last fence, takes a ref
897 * Returns: last fence if not signaled, dma fence stub if signaled
899 struct dma_fence *xe_exec_queue_last_fence_get(struct xe_exec_queue *q,
902 struct dma_fence *fence;
904 xe_exec_queue_last_fence_lockdep_assert(q, vm);
907 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
908 xe_exec_queue_last_fence_put(q, vm);
910 fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
911 dma_fence_get(fence);
916 * xe_exec_queue_last_fence_get_for_resume() - Get last fence
918 * @vm: The VM the engine does a bind or exec for
920 * Get last fence, takes a ref. Only safe to be called in the context of
921 * resuming the hw engine group's long-running exec queue, when the group
924 * Returns: last fence if not signaled, dma fence stub if signaled
926 struct dma_fence *xe_exec_queue_last_fence_get_for_resume(struct xe_exec_queue *q,
929 struct dma_fence *fence;
931 lockdep_assert_held_write(&q->hwe->hw_engine_group->mode_sem);
934 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
935 xe_exec_queue_last_fence_put_unlocked(q);
937 fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
938 dma_fence_get(fence);
943 * xe_exec_queue_last_fence_set() - Set last fence
945 * @vm: The VM the engine does a bind or exec for
948 * Set the last fence for the engine. Increases reference count for fence, when
949 * closing engine xe_exec_queue_last_fence_put should be called.
951 void xe_exec_queue_last_fence_set(struct xe_exec_queue *q, struct xe_vm *vm,
952 struct dma_fence *fence)
954 xe_exec_queue_last_fence_lockdep_assert(q, vm);
956 xe_exec_queue_last_fence_put(q, vm);
957 q->last_fence = dma_fence_get(fence);
961 * xe_exec_queue_last_fence_test_dep - Test last fence dependency of queue
963 * @vm: The VM the engine does a bind or exec for
966 * -ETIME if there exists an unsignalled last fence dependency, zero otherwise.
968 int xe_exec_queue_last_fence_test_dep(struct xe_exec_queue *q, struct xe_vm *vm)
970 struct dma_fence *fence;
973 fence = xe_exec_queue_last_fence_get(q, vm);
975 err = test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) ?
977 dma_fence_put(fence);