xref: /linux-6.15/drivers/gpu/drm/xe/xe_exec_queue.c (revision 41a97c4a)
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 <uapi/drm/xe_drm.h>
14 
15 #include "xe_device.h"
16 #include "xe_gt.h"
17 #include "xe_hw_engine_class_sysfs.h"
18 #include "xe_hw_engine_group.h"
19 #include "xe_hw_fence.h"
20 #include "xe_irq.h"
21 #include "xe_lrc.h"
22 #include "xe_macros.h"
23 #include "xe_migrate.h"
24 #include "xe_pm.h"
25 #include "xe_ring_ops_types.h"
26 #include "xe_trace.h"
27 #include "xe_vm.h"
28 #include "xe_pxp.h"
29 
30 enum xe_exec_queue_sched_prop {
31 	XE_EXEC_QUEUE_JOB_TIMEOUT = 0,
32 	XE_EXEC_QUEUE_TIMESLICE = 1,
33 	XE_EXEC_QUEUE_PREEMPT_TIMEOUT = 2,
34 	XE_EXEC_QUEUE_SCHED_PROP_MAX = 3,
35 };
36 
37 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q,
38 				      u64 extensions, int ext_number);
39 
40 static void __xe_exec_queue_free(struct xe_exec_queue *q)
41 {
42 	if (xe_exec_queue_uses_pxp(q))
43 		xe_pxp_exec_queue_remove(gt_to_xe(q->gt)->pxp, q);
44 	if (q->vm)
45 		xe_vm_put(q->vm);
46 
47 	if (q->xef)
48 		xe_file_put(q->xef);
49 
50 	kfree(q);
51 }
52 
53 static struct xe_exec_queue *__xe_exec_queue_alloc(struct xe_device *xe,
54 						   struct xe_vm *vm,
55 						   u32 logical_mask,
56 						   u16 width, struct xe_hw_engine *hwe,
57 						   u32 flags, u64 extensions)
58 {
59 	struct xe_exec_queue *q;
60 	struct xe_gt *gt = hwe->gt;
61 	int err;
62 
63 	/* only kernel queues can be permanent */
64 	XE_WARN_ON((flags & EXEC_QUEUE_FLAG_PERMANENT) && !(flags & EXEC_QUEUE_FLAG_KERNEL));
65 
66 	q = kzalloc(struct_size(q, lrc, width), GFP_KERNEL);
67 	if (!q)
68 		return ERR_PTR(-ENOMEM);
69 
70 	kref_init(&q->refcount);
71 	q->flags = flags;
72 	q->hwe = hwe;
73 	q->gt = gt;
74 	q->class = hwe->class;
75 	q->width = width;
76 	q->msix_vec = XE_IRQ_DEFAULT_MSIX;
77 	q->logical_mask = logical_mask;
78 	q->fence_irq = &gt->fence_irq[hwe->class];
79 	q->ring_ops = gt->ring_ops[hwe->class];
80 	q->ops = gt->exec_queue_ops;
81 	INIT_LIST_HEAD(&q->lr.link);
82 	INIT_LIST_HEAD(&q->multi_gt_link);
83 	INIT_LIST_HEAD(&q->hw_engine_group_link);
84 	INIT_LIST_HEAD(&q->pxp.link);
85 
86 	q->sched_props.timeslice_us = hwe->eclass->sched_props.timeslice_us;
87 	q->sched_props.preempt_timeout_us =
88 				hwe->eclass->sched_props.preempt_timeout_us;
89 	q->sched_props.job_timeout_ms =
90 				hwe->eclass->sched_props.job_timeout_ms;
91 	if (q->flags & EXEC_QUEUE_FLAG_KERNEL &&
92 	    q->flags & EXEC_QUEUE_FLAG_HIGH_PRIORITY)
93 		q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_KERNEL;
94 	else
95 		q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_NORMAL;
96 
97 	if (vm)
98 		q->vm = xe_vm_get(vm);
99 
100 	if (extensions) {
101 		/*
102 		 * may set q->usm, must come before xe_lrc_create(),
103 		 * may overwrite q->sched_props, must come before q->ops->init()
104 		 */
105 		err = exec_queue_user_extensions(xe, q, extensions, 0);
106 		if (err) {
107 			__xe_exec_queue_free(q);
108 			return ERR_PTR(err);
109 		}
110 	}
111 
112 	return q;
113 }
114 
115 static int __xe_exec_queue_init(struct xe_exec_queue *q)
116 {
117 	struct xe_vm *vm = q->vm;
118 	int i, err;
119 	u32 flags = 0;
120 
121 	/*
122 	 * PXP workloads executing on RCS or CCS must run in isolation (i.e. no
123 	 * other workload can use the EUs at the same time). On MTL this is done
124 	 * by setting the RUNALONE bit in the LRC, while starting on Xe2 there
125 	 * is a dedicated bit for it.
126 	 */
127 	if (xe_exec_queue_uses_pxp(q) &&
128 	    (q->class == XE_ENGINE_CLASS_RENDER || q->class == XE_ENGINE_CLASS_COMPUTE)) {
129 		if (GRAPHICS_VER(gt_to_xe(q->gt)) >= 20)
130 			flags |= XE_LRC_CREATE_PXP;
131 		else
132 			flags |= XE_LRC_CREATE_RUNALONE;
133 	}
134 
135 	if (vm) {
136 		err = xe_vm_lock(vm, true);
137 		if (err)
138 			return err;
139 	}
140 
141 	for (i = 0; i < q->width; ++i) {
142 		q->lrc[i] = xe_lrc_create(q->hwe, q->vm, SZ_16K, q->msix_vec, flags);
143 		if (IS_ERR(q->lrc[i])) {
144 			err = PTR_ERR(q->lrc[i]);
145 			goto err_unlock;
146 		}
147 	}
148 
149 	if (vm)
150 		xe_vm_unlock(vm);
151 
152 	err = q->ops->init(q);
153 	if (err)
154 		goto err_lrc;
155 
156 	return 0;
157 
158 err_unlock:
159 	if (vm)
160 		xe_vm_unlock(vm);
161 err_lrc:
162 	for (i = i - 1; i >= 0; --i)
163 		xe_lrc_put(q->lrc[i]);
164 	return err;
165 }
166 
167 struct xe_exec_queue *xe_exec_queue_create(struct xe_device *xe, struct xe_vm *vm,
168 					   u32 logical_mask, u16 width,
169 					   struct xe_hw_engine *hwe, u32 flags,
170 					   u64 extensions)
171 {
172 	struct xe_exec_queue *q;
173 	int err;
174 
175 	/* VMs for GSCCS queues (and only those) must have the XE_VM_FLAG_GSC flag */
176 	xe_assert(xe, !vm || (!!(vm->flags & XE_VM_FLAG_GSC) == !!(hwe->engine_id == XE_HW_ENGINE_GSCCS0)));
177 
178 	q = __xe_exec_queue_alloc(xe, vm, logical_mask, width, hwe, flags,
179 				  extensions);
180 	if (IS_ERR(q))
181 		return q;
182 
183 	err = __xe_exec_queue_init(q);
184 	if (err)
185 		goto err_post_alloc;
186 
187 	/*
188 	 * We can only add the queue to the PXP list after the init is complete,
189 	 * because the PXP termination can call exec_queue_kill and that will
190 	 * go bad if the queue is only half-initialized. This means that we
191 	 * can't do it when we handle the PXP extension in __xe_exec_queue_alloc
192 	 * and we need to do it here instead.
193 	 */
194 	if (xe_exec_queue_uses_pxp(q)) {
195 		err = xe_pxp_exec_queue_add(xe->pxp, q);
196 		if (err)
197 			goto err_post_alloc;
198 	}
199 
200 	return q;
201 
202 err_post_alloc:
203 	__xe_exec_queue_free(q);
204 	return ERR_PTR(err);
205 }
206 
207 struct xe_exec_queue *xe_exec_queue_create_class(struct xe_device *xe, struct xe_gt *gt,
208 						 struct xe_vm *vm,
209 						 enum xe_engine_class class,
210 						 u32 flags, u64 extensions)
211 {
212 	struct xe_hw_engine *hwe, *hwe0 = NULL;
213 	enum xe_hw_engine_id id;
214 	u32 logical_mask = 0;
215 
216 	for_each_hw_engine(hwe, gt, id) {
217 		if (xe_hw_engine_is_reserved(hwe))
218 			continue;
219 
220 		if (hwe->class == class) {
221 			logical_mask |= BIT(hwe->logical_instance);
222 			if (!hwe0)
223 				hwe0 = hwe;
224 		}
225 	}
226 
227 	if (!logical_mask)
228 		return ERR_PTR(-ENODEV);
229 
230 	return xe_exec_queue_create(xe, vm, logical_mask, 1, hwe0, flags, extensions);
231 }
232 
233 /**
234  * xe_exec_queue_create_bind() - Create bind exec queue.
235  * @xe: Xe device.
236  * @tile: tile which bind exec queue belongs to.
237  * @flags: exec queue creation flags
238  * @extensions: exec queue creation extensions
239  *
240  * Normalize bind exec queue creation. Bind exec queue is tied to migration VM
241  * for access to physical memory required for page table programming. On a
242  * faulting devices the reserved copy engine instance must be used to avoid
243  * deadlocking (user binds cannot get stuck behind faults as kernel binds which
244  * resolve faults depend on user binds). On non-faulting devices any copy engine
245  * can be used.
246  *
247  * Returns exec queue on success, ERR_PTR on failure
248  */
249 struct xe_exec_queue *xe_exec_queue_create_bind(struct xe_device *xe,
250 						struct xe_tile *tile,
251 						u32 flags, u64 extensions)
252 {
253 	struct xe_gt *gt = tile->primary_gt;
254 	struct xe_exec_queue *q;
255 	struct xe_vm *migrate_vm;
256 
257 	migrate_vm = xe_migrate_get_vm(tile->migrate);
258 	if (xe->info.has_usm) {
259 		struct xe_hw_engine *hwe = xe_gt_hw_engine(gt,
260 							   XE_ENGINE_CLASS_COPY,
261 							   gt->usm.reserved_bcs_instance,
262 							   false);
263 
264 		if (!hwe) {
265 			xe_vm_put(migrate_vm);
266 			return ERR_PTR(-EINVAL);
267 		}
268 
269 		q = xe_exec_queue_create(xe, migrate_vm,
270 					 BIT(hwe->logical_instance), 1, hwe,
271 					 flags, extensions);
272 	} else {
273 		q = xe_exec_queue_create_class(xe, gt, migrate_vm,
274 					       XE_ENGINE_CLASS_COPY, flags,
275 					       extensions);
276 	}
277 	xe_vm_put(migrate_vm);
278 
279 	return q;
280 }
281 ALLOW_ERROR_INJECTION(xe_exec_queue_create_bind, ERRNO);
282 
283 void xe_exec_queue_destroy(struct kref *ref)
284 {
285 	struct xe_exec_queue *q = container_of(ref, struct xe_exec_queue, refcount);
286 	struct xe_exec_queue *eq, *next;
287 
288 	if (xe_exec_queue_uses_pxp(q))
289 		xe_pxp_exec_queue_remove(gt_to_xe(q->gt)->pxp, q);
290 
291 	xe_exec_queue_last_fence_put_unlocked(q);
292 	if (!(q->flags & EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD)) {
293 		list_for_each_entry_safe(eq, next, &q->multi_gt_list,
294 					 multi_gt_link)
295 			xe_exec_queue_put(eq);
296 	}
297 
298 	q->ops->fini(q);
299 }
300 
301 void xe_exec_queue_fini(struct xe_exec_queue *q)
302 {
303 	int i;
304 
305 	/*
306 	 * Before releasing our ref to lrc and xef, accumulate our run ticks
307 	 * and wakeup any waiters.
308 	 */
309 	xe_exec_queue_update_run_ticks(q);
310 	if (q->xef && atomic_dec_and_test(&q->xef->exec_queue.pending_removal))
311 		wake_up_var(&q->xef->exec_queue.pending_removal);
312 
313 	for (i = 0; i < q->width; ++i)
314 		xe_lrc_put(q->lrc[i]);
315 
316 	__xe_exec_queue_free(q);
317 }
318 
319 void xe_exec_queue_assign_name(struct xe_exec_queue *q, u32 instance)
320 {
321 	switch (q->class) {
322 	case XE_ENGINE_CLASS_RENDER:
323 		snprintf(q->name, sizeof(q->name), "rcs%d", instance);
324 		break;
325 	case XE_ENGINE_CLASS_VIDEO_DECODE:
326 		snprintf(q->name, sizeof(q->name), "vcs%d", instance);
327 		break;
328 	case XE_ENGINE_CLASS_VIDEO_ENHANCE:
329 		snprintf(q->name, sizeof(q->name), "vecs%d", instance);
330 		break;
331 	case XE_ENGINE_CLASS_COPY:
332 		snprintf(q->name, sizeof(q->name), "bcs%d", instance);
333 		break;
334 	case XE_ENGINE_CLASS_COMPUTE:
335 		snprintf(q->name, sizeof(q->name), "ccs%d", instance);
336 		break;
337 	case XE_ENGINE_CLASS_OTHER:
338 		snprintf(q->name, sizeof(q->name), "gsccs%d", instance);
339 		break;
340 	default:
341 		XE_WARN_ON(q->class);
342 	}
343 }
344 
345 struct xe_exec_queue *xe_exec_queue_lookup(struct xe_file *xef, u32 id)
346 {
347 	struct xe_exec_queue *q;
348 
349 	mutex_lock(&xef->exec_queue.lock);
350 	q = xa_load(&xef->exec_queue.xa, id);
351 	if (q)
352 		xe_exec_queue_get(q);
353 	mutex_unlock(&xef->exec_queue.lock);
354 
355 	return q;
356 }
357 
358 enum xe_exec_queue_priority
359 xe_exec_queue_device_get_max_priority(struct xe_device *xe)
360 {
361 	return capable(CAP_SYS_NICE) ? XE_EXEC_QUEUE_PRIORITY_HIGH :
362 				       XE_EXEC_QUEUE_PRIORITY_NORMAL;
363 }
364 
365 static int exec_queue_set_priority(struct xe_device *xe, struct xe_exec_queue *q,
366 				   u64 value)
367 {
368 	if (XE_IOCTL_DBG(xe, value > XE_EXEC_QUEUE_PRIORITY_HIGH))
369 		return -EINVAL;
370 
371 	if (XE_IOCTL_DBG(xe, value > xe_exec_queue_device_get_max_priority(xe)))
372 		return -EPERM;
373 
374 	q->sched_props.priority = value;
375 	return 0;
376 }
377 
378 static bool xe_exec_queue_enforce_schedule_limit(void)
379 {
380 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT)
381 	return true;
382 #else
383 	return !capable(CAP_SYS_NICE);
384 #endif
385 }
386 
387 static void
388 xe_exec_queue_get_prop_minmax(struct xe_hw_engine_class_intf *eclass,
389 			      enum xe_exec_queue_sched_prop prop,
390 			      u32 *min, u32 *max)
391 {
392 	switch (prop) {
393 	case XE_EXEC_QUEUE_JOB_TIMEOUT:
394 		*min = eclass->sched_props.job_timeout_min;
395 		*max = eclass->sched_props.job_timeout_max;
396 		break;
397 	case XE_EXEC_QUEUE_TIMESLICE:
398 		*min = eclass->sched_props.timeslice_min;
399 		*max = eclass->sched_props.timeslice_max;
400 		break;
401 	case XE_EXEC_QUEUE_PREEMPT_TIMEOUT:
402 		*min = eclass->sched_props.preempt_timeout_min;
403 		*max = eclass->sched_props.preempt_timeout_max;
404 		break;
405 	default:
406 		break;
407 	}
408 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT)
409 	if (capable(CAP_SYS_NICE)) {
410 		switch (prop) {
411 		case XE_EXEC_QUEUE_JOB_TIMEOUT:
412 			*min = XE_HW_ENGINE_JOB_TIMEOUT_MIN;
413 			*max = XE_HW_ENGINE_JOB_TIMEOUT_MAX;
414 			break;
415 		case XE_EXEC_QUEUE_TIMESLICE:
416 			*min = XE_HW_ENGINE_TIMESLICE_MIN;
417 			*max = XE_HW_ENGINE_TIMESLICE_MAX;
418 			break;
419 		case XE_EXEC_QUEUE_PREEMPT_TIMEOUT:
420 			*min = XE_HW_ENGINE_PREEMPT_TIMEOUT_MIN;
421 			*max = XE_HW_ENGINE_PREEMPT_TIMEOUT_MAX;
422 			break;
423 		default:
424 			break;
425 		}
426 	}
427 #endif
428 }
429 
430 static int exec_queue_set_timeslice(struct xe_device *xe, struct xe_exec_queue *q,
431 				    u64 value)
432 {
433 	u32 min = 0, max = 0;
434 
435 	xe_exec_queue_get_prop_minmax(q->hwe->eclass,
436 				      XE_EXEC_QUEUE_TIMESLICE, &min, &max);
437 
438 	if (xe_exec_queue_enforce_schedule_limit() &&
439 	    !xe_hw_engine_timeout_in_range(value, min, max))
440 		return -EINVAL;
441 
442 	q->sched_props.timeslice_us = value;
443 	return 0;
444 }
445 
446 static int
447 exec_queue_set_pxp_type(struct xe_device *xe, struct xe_exec_queue *q, u64 value)
448 {
449 	if (value == DRM_XE_PXP_TYPE_NONE)
450 		return 0;
451 
452 	/* we only support HWDRM sessions right now */
453 	if (XE_IOCTL_DBG(xe, value != DRM_XE_PXP_TYPE_HWDRM))
454 		return -EINVAL;
455 
456 	if (!xe_pxp_is_enabled(xe->pxp))
457 		return -ENODEV;
458 
459 	return xe_pxp_exec_queue_set_type(xe->pxp, q, DRM_XE_PXP_TYPE_HWDRM);
460 }
461 
462 typedef int (*xe_exec_queue_set_property_fn)(struct xe_device *xe,
463 					     struct xe_exec_queue *q,
464 					     u64 value);
465 
466 static const xe_exec_queue_set_property_fn exec_queue_set_property_funcs[] = {
467 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY] = exec_queue_set_priority,
468 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE] = exec_queue_set_timeslice,
469 	[DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE] = exec_queue_set_pxp_type,
470 };
471 
472 static int exec_queue_user_ext_set_property(struct xe_device *xe,
473 					    struct xe_exec_queue *q,
474 					    u64 extension)
475 {
476 	u64 __user *address = u64_to_user_ptr(extension);
477 	struct drm_xe_ext_set_property ext;
478 	int err;
479 	u32 idx;
480 
481 	err = __copy_from_user(&ext, address, sizeof(ext));
482 	if (XE_IOCTL_DBG(xe, err))
483 		return -EFAULT;
484 
485 	if (XE_IOCTL_DBG(xe, ext.property >=
486 			 ARRAY_SIZE(exec_queue_set_property_funcs)) ||
487 	    XE_IOCTL_DBG(xe, ext.pad) ||
488 	    XE_IOCTL_DBG(xe, ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY &&
489 			 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE &&
490 			 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE))
491 		return -EINVAL;
492 
493 	idx = array_index_nospec(ext.property, ARRAY_SIZE(exec_queue_set_property_funcs));
494 	if (!exec_queue_set_property_funcs[idx])
495 		return -EINVAL;
496 
497 	return exec_queue_set_property_funcs[idx](xe, q, ext.value);
498 }
499 
500 typedef int (*xe_exec_queue_user_extension_fn)(struct xe_device *xe,
501 					       struct xe_exec_queue *q,
502 					       u64 extension);
503 
504 static const xe_exec_queue_user_extension_fn exec_queue_user_extension_funcs[] = {
505 	[DRM_XE_EXEC_QUEUE_EXTENSION_SET_PROPERTY] = exec_queue_user_ext_set_property,
506 };
507 
508 #define MAX_USER_EXTENSIONS	16
509 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q,
510 				      u64 extensions, int ext_number)
511 {
512 	u64 __user *address = u64_to_user_ptr(extensions);
513 	struct drm_xe_user_extension ext;
514 	int err;
515 	u32 idx;
516 
517 	if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS))
518 		return -E2BIG;
519 
520 	err = __copy_from_user(&ext, address, sizeof(ext));
521 	if (XE_IOCTL_DBG(xe, err))
522 		return -EFAULT;
523 
524 	if (XE_IOCTL_DBG(xe, ext.pad) ||
525 	    XE_IOCTL_DBG(xe, ext.name >=
526 			 ARRAY_SIZE(exec_queue_user_extension_funcs)))
527 		return -EINVAL;
528 
529 	idx = array_index_nospec(ext.name,
530 				 ARRAY_SIZE(exec_queue_user_extension_funcs));
531 	err = exec_queue_user_extension_funcs[idx](xe, q, extensions);
532 	if (XE_IOCTL_DBG(xe, err))
533 		return err;
534 
535 	if (ext.next_extension)
536 		return exec_queue_user_extensions(xe, q, ext.next_extension,
537 						  ++ext_number);
538 
539 	return 0;
540 }
541 
542 static u32 calc_validate_logical_mask(struct xe_device *xe, struct xe_gt *gt,
543 				      struct drm_xe_engine_class_instance *eci,
544 				      u16 width, u16 num_placements)
545 {
546 	int len = width * num_placements;
547 	int i, j, n;
548 	u16 class;
549 	u16 gt_id;
550 	u32 return_mask = 0, prev_mask;
551 
552 	if (XE_IOCTL_DBG(xe, !xe_device_uc_enabled(xe) &&
553 			 len > 1))
554 		return 0;
555 
556 	for (i = 0; i < width; ++i) {
557 		u32 current_mask = 0;
558 
559 		for (j = 0; j < num_placements; ++j) {
560 			struct xe_hw_engine *hwe;
561 
562 			n = j * width + i;
563 
564 			hwe = xe_hw_engine_lookup(xe, eci[n]);
565 			if (XE_IOCTL_DBG(xe, !hwe))
566 				return 0;
567 
568 			if (XE_IOCTL_DBG(xe, xe_hw_engine_is_reserved(hwe)))
569 				return 0;
570 
571 			if (XE_IOCTL_DBG(xe, n && eci[n].gt_id != gt_id) ||
572 			    XE_IOCTL_DBG(xe, n && eci[n].engine_class != class))
573 				return 0;
574 
575 			class = eci[n].engine_class;
576 			gt_id = eci[n].gt_id;
577 
578 			if (width == 1 || !i)
579 				return_mask |= BIT(eci[n].engine_instance);
580 			current_mask |= BIT(eci[n].engine_instance);
581 		}
582 
583 		/* Parallel submissions must be logically contiguous */
584 		if (i && XE_IOCTL_DBG(xe, current_mask != prev_mask << 1))
585 			return 0;
586 
587 		prev_mask = current_mask;
588 	}
589 
590 	return return_mask;
591 }
592 
593 int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
594 			       struct drm_file *file)
595 {
596 	struct xe_device *xe = to_xe_device(dev);
597 	struct xe_file *xef = to_xe_file(file);
598 	struct drm_xe_exec_queue_create *args = data;
599 	struct drm_xe_engine_class_instance eci[XE_HW_ENGINE_MAX_INSTANCE];
600 	struct drm_xe_engine_class_instance __user *user_eci =
601 		u64_to_user_ptr(args->instances);
602 	struct xe_hw_engine *hwe;
603 	struct xe_vm *vm;
604 	struct xe_gt *gt;
605 	struct xe_tile *tile;
606 	struct xe_exec_queue *q = NULL;
607 	u32 logical_mask;
608 	u32 id;
609 	u32 len;
610 	int err;
611 
612 	if (XE_IOCTL_DBG(xe, args->flags) ||
613 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
614 		return -EINVAL;
615 
616 	len = args->width * args->num_placements;
617 	if (XE_IOCTL_DBG(xe, !len || len > XE_HW_ENGINE_MAX_INSTANCE))
618 		return -EINVAL;
619 
620 	err = __copy_from_user(eci, user_eci,
621 			       sizeof(struct drm_xe_engine_class_instance) *
622 			       len);
623 	if (XE_IOCTL_DBG(xe, err))
624 		return -EFAULT;
625 
626 	if (XE_IOCTL_DBG(xe, eci[0].gt_id >= xe->info.gt_count))
627 		return -EINVAL;
628 
629 	if (eci[0].engine_class == DRM_XE_ENGINE_CLASS_VM_BIND) {
630 		if (XE_IOCTL_DBG(xe, args->width != 1) ||
631 		    XE_IOCTL_DBG(xe, args->num_placements != 1) ||
632 		    XE_IOCTL_DBG(xe, eci[0].engine_instance != 0))
633 			return -EINVAL;
634 
635 		for_each_tile(tile, xe, id) {
636 			struct xe_exec_queue *new;
637 			u32 flags = EXEC_QUEUE_FLAG_VM;
638 
639 			if (id)
640 				flags |= EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD;
641 
642 			new = xe_exec_queue_create_bind(xe, tile, flags,
643 							args->extensions);
644 			if (IS_ERR(new)) {
645 				err = PTR_ERR(new);
646 				if (q)
647 					goto put_exec_queue;
648 				return err;
649 			}
650 			if (id == 0)
651 				q = new;
652 			else
653 				list_add_tail(&new->multi_gt_list,
654 					      &q->multi_gt_link);
655 		}
656 	} else {
657 		gt = xe_device_get_gt(xe, eci[0].gt_id);
658 		logical_mask = calc_validate_logical_mask(xe, gt, eci,
659 							  args->width,
660 							  args->num_placements);
661 		if (XE_IOCTL_DBG(xe, !logical_mask))
662 			return -EINVAL;
663 
664 		hwe = xe_hw_engine_lookup(xe, eci[0]);
665 		if (XE_IOCTL_DBG(xe, !hwe))
666 			return -EINVAL;
667 
668 		vm = xe_vm_lookup(xef, args->vm_id);
669 		if (XE_IOCTL_DBG(xe, !vm))
670 			return -ENOENT;
671 
672 		err = down_read_interruptible(&vm->lock);
673 		if (err) {
674 			xe_vm_put(vm);
675 			return err;
676 		}
677 
678 		if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
679 			up_read(&vm->lock);
680 			xe_vm_put(vm);
681 			return -ENOENT;
682 		}
683 
684 		q = xe_exec_queue_create(xe, vm, logical_mask,
685 					 args->width, hwe, 0,
686 					 args->extensions);
687 		up_read(&vm->lock);
688 		xe_vm_put(vm);
689 		if (IS_ERR(q))
690 			return PTR_ERR(q);
691 
692 		if (xe_vm_in_preempt_fence_mode(vm)) {
693 			q->lr.context = dma_fence_context_alloc(1);
694 
695 			err = xe_vm_add_compute_exec_queue(vm, q);
696 			if (XE_IOCTL_DBG(xe, err))
697 				goto put_exec_queue;
698 		}
699 
700 		if (q->vm && q->hwe->hw_engine_group) {
701 			err = xe_hw_engine_group_add_exec_queue(q->hwe->hw_engine_group, q);
702 			if (err)
703 				goto put_exec_queue;
704 		}
705 	}
706 
707 	q->xef = xe_file_get(xef);
708 
709 	/* user id alloc must always be last in ioctl to prevent UAF */
710 	err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
711 	if (err)
712 		goto kill_exec_queue;
713 
714 	args->exec_queue_id = id;
715 
716 	return 0;
717 
718 kill_exec_queue:
719 	xe_exec_queue_kill(q);
720 put_exec_queue:
721 	xe_exec_queue_put(q);
722 	return err;
723 }
724 
725 int xe_exec_queue_get_property_ioctl(struct drm_device *dev, void *data,
726 				     struct drm_file *file)
727 {
728 	struct xe_device *xe = to_xe_device(dev);
729 	struct xe_file *xef = to_xe_file(file);
730 	struct drm_xe_exec_queue_get_property *args = data;
731 	struct xe_exec_queue *q;
732 	int ret;
733 
734 	if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
735 		return -EINVAL;
736 
737 	q = xe_exec_queue_lookup(xef, args->exec_queue_id);
738 	if (XE_IOCTL_DBG(xe, !q))
739 		return -ENOENT;
740 
741 	switch (args->property) {
742 	case DRM_XE_EXEC_QUEUE_GET_PROPERTY_BAN:
743 		args->value = q->ops->reset_status(q);
744 		ret = 0;
745 		break;
746 	default:
747 		ret = -EINVAL;
748 	}
749 
750 	xe_exec_queue_put(q);
751 
752 	return ret;
753 }
754 
755 /**
756  * xe_exec_queue_is_lr() - Whether an exec_queue is long-running
757  * @q: The exec_queue
758  *
759  * Return: True if the exec_queue is long-running, false otherwise.
760  */
761 bool xe_exec_queue_is_lr(struct xe_exec_queue *q)
762 {
763 	return q->vm && xe_vm_in_lr_mode(q->vm) &&
764 		!(q->flags & EXEC_QUEUE_FLAG_VM);
765 }
766 
767 static s32 xe_exec_queue_num_job_inflight(struct xe_exec_queue *q)
768 {
769 	return q->lrc[0]->fence_ctx.next_seqno - xe_lrc_seqno(q->lrc[0]) - 1;
770 }
771 
772 /**
773  * xe_exec_queue_ring_full() - Whether an exec_queue's ring is full
774  * @q: The exec_queue
775  *
776  * Return: True if the exec_queue's ring is full, false otherwise.
777  */
778 bool xe_exec_queue_ring_full(struct xe_exec_queue *q)
779 {
780 	struct xe_lrc *lrc = q->lrc[0];
781 	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
782 
783 	return xe_exec_queue_num_job_inflight(q) >= max_job;
784 }
785 
786 /**
787  * xe_exec_queue_is_idle() - Whether an exec_queue is idle.
788  * @q: The exec_queue
789  *
790  * FIXME: Need to determine what to use as the short-lived
791  * timeline lock for the exec_queues, so that the return value
792  * of this function becomes more than just an advisory
793  * snapshot in time. The timeline lock must protect the
794  * seqno from racing submissions on the same exec_queue.
795  * Typically vm->resv, but user-created timeline locks use the migrate vm
796  * and never grabs the migrate vm->resv so we have a race there.
797  *
798  * Return: True if the exec_queue is idle, false otherwise.
799  */
800 bool xe_exec_queue_is_idle(struct xe_exec_queue *q)
801 {
802 	if (xe_exec_queue_is_parallel(q)) {
803 		int i;
804 
805 		for (i = 0; i < q->width; ++i) {
806 			if (xe_lrc_seqno(q->lrc[i]) !=
807 			    q->lrc[i]->fence_ctx.next_seqno - 1)
808 				return false;
809 		}
810 
811 		return true;
812 	}
813 
814 	return xe_lrc_seqno(q->lrc[0]) ==
815 		q->lrc[0]->fence_ctx.next_seqno - 1;
816 }
817 
818 /**
819  * xe_exec_queue_update_run_ticks() - Update run time in ticks for this exec queue
820  * from hw
821  * @q: The exec queue
822  *
823  * Update the timestamp saved by HW for this exec queue and save run ticks
824  * calculated by using the delta from last update.
825  */
826 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
827 {
828 	struct xe_device *xe = gt_to_xe(q->gt);
829 	struct xe_lrc *lrc;
830 	u32 old_ts, new_ts;
831 	int idx;
832 
833 	/*
834 	 * Jobs that are executed by kernel doesn't have a corresponding xe_file
835 	 * and thus are not accounted.
836 	 */
837 	if (!q->xef)
838 		return;
839 
840 	/* Synchronize with unbind while holding the xe file open */
841 	if (!drm_dev_enter(&xe->drm, &idx))
842 		return;
843 	/*
844 	 * Only sample the first LRC. For parallel submission, all of them are
845 	 * scheduled together and we compensate that below by multiplying by
846 	 * width - this may introduce errors if that premise is not true and
847 	 * they don't exit 100% aligned. On the other hand, looping through
848 	 * the LRCs and reading them in different time could also introduce
849 	 * errors.
850 	 */
851 	lrc = q->lrc[0];
852 	new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
853 	q->xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
854 
855 	drm_dev_exit(idx);
856 }
857 
858 /**
859  * xe_exec_queue_kill - permanently stop all execution from an exec queue
860  * @q: The exec queue
861  *
862  * This function permanently stops all activity on an exec queue. If the queue
863  * is actively executing on the HW, it will be kicked off the engine; any
864  * pending jobs are discarded and all future submissions are rejected.
865  * This function is safe to call multiple times.
866  */
867 void xe_exec_queue_kill(struct xe_exec_queue *q)
868 {
869 	struct xe_exec_queue *eq = q, *next;
870 
871 	list_for_each_entry_safe(eq, next, &eq->multi_gt_list,
872 				 multi_gt_link) {
873 		q->ops->kill(eq);
874 		xe_vm_remove_compute_exec_queue(q->vm, eq);
875 	}
876 
877 	q->ops->kill(q);
878 	xe_vm_remove_compute_exec_queue(q->vm, q);
879 }
880 
881 int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data,
882 				struct drm_file *file)
883 {
884 	struct xe_device *xe = to_xe_device(dev);
885 	struct xe_file *xef = to_xe_file(file);
886 	struct drm_xe_exec_queue_destroy *args = data;
887 	struct xe_exec_queue *q;
888 
889 	if (XE_IOCTL_DBG(xe, args->pad) ||
890 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
891 		return -EINVAL;
892 
893 	mutex_lock(&xef->exec_queue.lock);
894 	q = xa_erase(&xef->exec_queue.xa, args->exec_queue_id);
895 	if (q)
896 		atomic_inc(&xef->exec_queue.pending_removal);
897 	mutex_unlock(&xef->exec_queue.lock);
898 
899 	if (XE_IOCTL_DBG(xe, !q))
900 		return -ENOENT;
901 
902 	if (q->vm && q->hwe->hw_engine_group)
903 		xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
904 
905 	xe_exec_queue_kill(q);
906 
907 	trace_xe_exec_queue_close(q);
908 	xe_exec_queue_put(q);
909 
910 	return 0;
911 }
912 
913 static void xe_exec_queue_last_fence_lockdep_assert(struct xe_exec_queue *q,
914 						    struct xe_vm *vm)
915 {
916 	if (q->flags & EXEC_QUEUE_FLAG_VM) {
917 		lockdep_assert_held(&vm->lock);
918 	} else {
919 		xe_vm_assert_held(vm);
920 		lockdep_assert_held(&q->hwe->hw_engine_group->mode_sem);
921 	}
922 }
923 
924 /**
925  * xe_exec_queue_last_fence_put() - Drop ref to last fence
926  * @q: The exec queue
927  * @vm: The VM the engine does a bind or exec for
928  */
929 void xe_exec_queue_last_fence_put(struct xe_exec_queue *q, struct xe_vm *vm)
930 {
931 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
932 
933 	xe_exec_queue_last_fence_put_unlocked(q);
934 }
935 
936 /**
937  * xe_exec_queue_last_fence_put_unlocked() - Drop ref to last fence unlocked
938  * @q: The exec queue
939  *
940  * Only safe to be called from xe_exec_queue_destroy().
941  */
942 void xe_exec_queue_last_fence_put_unlocked(struct xe_exec_queue *q)
943 {
944 	if (q->last_fence) {
945 		dma_fence_put(q->last_fence);
946 		q->last_fence = NULL;
947 	}
948 }
949 
950 /**
951  * xe_exec_queue_last_fence_get() - Get last fence
952  * @q: The exec queue
953  * @vm: The VM the engine does a bind or exec for
954  *
955  * Get last fence, takes a ref
956  *
957  * Returns: last fence if not signaled, dma fence stub if signaled
958  */
959 struct dma_fence *xe_exec_queue_last_fence_get(struct xe_exec_queue *q,
960 					       struct xe_vm *vm)
961 {
962 	struct dma_fence *fence;
963 
964 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
965 
966 	if (q->last_fence &&
967 	    test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
968 		xe_exec_queue_last_fence_put(q, vm);
969 
970 	fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
971 	dma_fence_get(fence);
972 	return fence;
973 }
974 
975 /**
976  * xe_exec_queue_last_fence_get_for_resume() - Get last fence
977  * @q: The exec queue
978  * @vm: The VM the engine does a bind or exec for
979  *
980  * Get last fence, takes a ref. Only safe to be called in the context of
981  * resuming the hw engine group's long-running exec queue, when the group
982  * semaphore is held.
983  *
984  * Returns: last fence if not signaled, dma fence stub if signaled
985  */
986 struct dma_fence *xe_exec_queue_last_fence_get_for_resume(struct xe_exec_queue *q,
987 							  struct xe_vm *vm)
988 {
989 	struct dma_fence *fence;
990 
991 	lockdep_assert_held_write(&q->hwe->hw_engine_group->mode_sem);
992 
993 	if (q->last_fence &&
994 	    test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
995 		xe_exec_queue_last_fence_put_unlocked(q);
996 
997 	fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
998 	dma_fence_get(fence);
999 	return fence;
1000 }
1001 
1002 /**
1003  * xe_exec_queue_last_fence_set() - Set last fence
1004  * @q: The exec queue
1005  * @vm: The VM the engine does a bind or exec for
1006  * @fence: The fence
1007  *
1008  * Set the last fence for the engine. Increases reference count for fence, when
1009  * closing engine xe_exec_queue_last_fence_put should be called.
1010  */
1011 void xe_exec_queue_last_fence_set(struct xe_exec_queue *q, struct xe_vm *vm,
1012 				  struct dma_fence *fence)
1013 {
1014 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1015 
1016 	xe_exec_queue_last_fence_put(q, vm);
1017 	q->last_fence = dma_fence_get(fence);
1018 }
1019 
1020 /**
1021  * xe_exec_queue_last_fence_test_dep - Test last fence dependency of queue
1022  * @q: The exec queue
1023  * @vm: The VM the engine does a bind or exec for
1024  *
1025  * Returns:
1026  * -ETIME if there exists an unsignalled last fence dependency, zero otherwise.
1027  */
1028 int xe_exec_queue_last_fence_test_dep(struct xe_exec_queue *q, struct xe_vm *vm)
1029 {
1030 	struct dma_fence *fence;
1031 	int err = 0;
1032 
1033 	fence = xe_exec_queue_last_fence_get(q, vm);
1034 	if (fence) {
1035 		err = test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) ?
1036 			0 : -ETIME;
1037 		dma_fence_put(fence);
1038 	}
1039 
1040 	return err;
1041 }
1042