xref: /linux/drivers/gpu/drm/xe/xe_exec_queue.c (revision 5488bec96bccbd87335921338f8dc38b87db7d2c)
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,
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_tile *tile;
605 	struct xe_exec_queue *q = NULL;
606 	u32 logical_mask;
607 	u32 flags = 0;
608 	u32 id;
609 	u32 len;
610 	int err;
611 
612 	if (XE_IOCTL_DBG(xe, args->flags & ~DRM_XE_EXEC_QUEUE_LOW_LATENCY_HINT) ||
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 (args->flags & DRM_XE_EXEC_QUEUE_LOW_LATENCY_HINT)
630 		flags |= EXEC_QUEUE_FLAG_LOW_LATENCY;
631 
632 	if (eci[0].engine_class == DRM_XE_ENGINE_CLASS_VM_BIND) {
633 		if (XE_IOCTL_DBG(xe, args->width != 1) ||
634 		    XE_IOCTL_DBG(xe, args->num_placements != 1) ||
635 		    XE_IOCTL_DBG(xe, eci[0].engine_instance != 0))
636 			return -EINVAL;
637 
638 		for_each_tile(tile, xe, id) {
639 			struct xe_exec_queue *new;
640 
641 			flags |= EXEC_QUEUE_FLAG_VM;
642 			if (id)
643 				flags |= EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD;
644 
645 			new = xe_exec_queue_create_bind(xe, tile, flags,
646 							args->extensions);
647 			if (IS_ERR(new)) {
648 				err = PTR_ERR(new);
649 				if (q)
650 					goto put_exec_queue;
651 				return err;
652 			}
653 			if (id == 0)
654 				q = new;
655 			else
656 				list_add_tail(&new->multi_gt_list,
657 					      &q->multi_gt_link);
658 		}
659 	} else {
660 		logical_mask = calc_validate_logical_mask(xe, eci,
661 							  args->width,
662 							  args->num_placements);
663 		if (XE_IOCTL_DBG(xe, !logical_mask))
664 			return -EINVAL;
665 
666 		hwe = xe_hw_engine_lookup(xe, eci[0]);
667 		if (XE_IOCTL_DBG(xe, !hwe))
668 			return -EINVAL;
669 
670 		vm = xe_vm_lookup(xef, args->vm_id);
671 		if (XE_IOCTL_DBG(xe, !vm))
672 			return -ENOENT;
673 
674 		err = down_read_interruptible(&vm->lock);
675 		if (err) {
676 			xe_vm_put(vm);
677 			return err;
678 		}
679 
680 		if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
681 			up_read(&vm->lock);
682 			xe_vm_put(vm);
683 			return -ENOENT;
684 		}
685 
686 		q = xe_exec_queue_create(xe, vm, logical_mask,
687 					 args->width, hwe, flags,
688 					 args->extensions);
689 		up_read(&vm->lock);
690 		xe_vm_put(vm);
691 		if (IS_ERR(q))
692 			return PTR_ERR(q);
693 
694 		if (xe_vm_in_preempt_fence_mode(vm)) {
695 			q->lr.context = dma_fence_context_alloc(1);
696 
697 			err = xe_vm_add_compute_exec_queue(vm, q);
698 			if (XE_IOCTL_DBG(xe, err))
699 				goto put_exec_queue;
700 		}
701 
702 		if (q->vm && q->hwe->hw_engine_group) {
703 			err = xe_hw_engine_group_add_exec_queue(q->hwe->hw_engine_group, q);
704 			if (err)
705 				goto put_exec_queue;
706 		}
707 	}
708 
709 	q->xef = xe_file_get(xef);
710 
711 	/* user id alloc must always be last in ioctl to prevent UAF */
712 	err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
713 	if (err)
714 		goto kill_exec_queue;
715 
716 	args->exec_queue_id = id;
717 
718 	return 0;
719 
720 kill_exec_queue:
721 	xe_exec_queue_kill(q);
722 put_exec_queue:
723 	xe_exec_queue_put(q);
724 	return err;
725 }
726 
727 int xe_exec_queue_get_property_ioctl(struct drm_device *dev, void *data,
728 				     struct drm_file *file)
729 {
730 	struct xe_device *xe = to_xe_device(dev);
731 	struct xe_file *xef = to_xe_file(file);
732 	struct drm_xe_exec_queue_get_property *args = data;
733 	struct xe_exec_queue *q;
734 	int ret;
735 
736 	if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
737 		return -EINVAL;
738 
739 	q = xe_exec_queue_lookup(xef, args->exec_queue_id);
740 	if (XE_IOCTL_DBG(xe, !q))
741 		return -ENOENT;
742 
743 	switch (args->property) {
744 	case DRM_XE_EXEC_QUEUE_GET_PROPERTY_BAN:
745 		args->value = q->ops->reset_status(q);
746 		ret = 0;
747 		break;
748 	default:
749 		ret = -EINVAL;
750 	}
751 
752 	xe_exec_queue_put(q);
753 
754 	return ret;
755 }
756 
757 /**
758  * xe_exec_queue_is_lr() - Whether an exec_queue is long-running
759  * @q: The exec_queue
760  *
761  * Return: True if the exec_queue is long-running, false otherwise.
762  */
763 bool xe_exec_queue_is_lr(struct xe_exec_queue *q)
764 {
765 	return q->vm && xe_vm_in_lr_mode(q->vm) &&
766 		!(q->flags & EXEC_QUEUE_FLAG_VM);
767 }
768 
769 static s32 xe_exec_queue_num_job_inflight(struct xe_exec_queue *q)
770 {
771 	return q->lrc[0]->fence_ctx.next_seqno - xe_lrc_seqno(q->lrc[0]) - 1;
772 }
773 
774 /**
775  * xe_exec_queue_ring_full() - Whether an exec_queue's ring is full
776  * @q: The exec_queue
777  *
778  * Return: True if the exec_queue's ring is full, false otherwise.
779  */
780 bool xe_exec_queue_ring_full(struct xe_exec_queue *q)
781 {
782 	struct xe_lrc *lrc = q->lrc[0];
783 	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
784 
785 	return xe_exec_queue_num_job_inflight(q) >= max_job;
786 }
787 
788 /**
789  * xe_exec_queue_is_idle() - Whether an exec_queue is idle.
790  * @q: The exec_queue
791  *
792  * FIXME: Need to determine what to use as the short-lived
793  * timeline lock for the exec_queues, so that the return value
794  * of this function becomes more than just an advisory
795  * snapshot in time. The timeline lock must protect the
796  * seqno from racing submissions on the same exec_queue.
797  * Typically vm->resv, but user-created timeline locks use the migrate vm
798  * and never grabs the migrate vm->resv so we have a race there.
799  *
800  * Return: True if the exec_queue is idle, false otherwise.
801  */
802 bool xe_exec_queue_is_idle(struct xe_exec_queue *q)
803 {
804 	if (xe_exec_queue_is_parallel(q)) {
805 		int i;
806 
807 		for (i = 0; i < q->width; ++i) {
808 			if (xe_lrc_seqno(q->lrc[i]) !=
809 			    q->lrc[i]->fence_ctx.next_seqno - 1)
810 				return false;
811 		}
812 
813 		return true;
814 	}
815 
816 	return xe_lrc_seqno(q->lrc[0]) ==
817 		q->lrc[0]->fence_ctx.next_seqno - 1;
818 }
819 
820 /**
821  * xe_exec_queue_update_run_ticks() - Update run time in ticks for this exec queue
822  * from hw
823  * @q: The exec queue
824  *
825  * Update the timestamp saved by HW for this exec queue and save run ticks
826  * calculated by using the delta from last update.
827  */
828 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
829 {
830 	struct xe_device *xe = gt_to_xe(q->gt);
831 	struct xe_lrc *lrc;
832 	u32 old_ts, new_ts;
833 	int idx;
834 
835 	/*
836 	 * Jobs that are executed by kernel doesn't have a corresponding xe_file
837 	 * and thus are not accounted.
838 	 */
839 	if (!q->xef)
840 		return;
841 
842 	/* Synchronize with unbind while holding the xe file open */
843 	if (!drm_dev_enter(&xe->drm, &idx))
844 		return;
845 	/*
846 	 * Only sample the first LRC. For parallel submission, all of them are
847 	 * scheduled together and we compensate that below by multiplying by
848 	 * width - this may introduce errors if that premise is not true and
849 	 * they don't exit 100% aligned. On the other hand, looping through
850 	 * the LRCs and reading them in different time could also introduce
851 	 * errors.
852 	 */
853 	lrc = q->lrc[0];
854 	new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
855 	q->xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
856 
857 	drm_dev_exit(idx);
858 }
859 
860 /**
861  * xe_exec_queue_kill - permanently stop all execution from an exec queue
862  * @q: The exec queue
863  *
864  * This function permanently stops all activity on an exec queue. If the queue
865  * is actively executing on the HW, it will be kicked off the engine; any
866  * pending jobs are discarded and all future submissions are rejected.
867  * This function is safe to call multiple times.
868  */
869 void xe_exec_queue_kill(struct xe_exec_queue *q)
870 {
871 	struct xe_exec_queue *eq = q, *next;
872 
873 	list_for_each_entry_safe(eq, next, &eq->multi_gt_list,
874 				 multi_gt_link) {
875 		q->ops->kill(eq);
876 		xe_vm_remove_compute_exec_queue(q->vm, eq);
877 	}
878 
879 	q->ops->kill(q);
880 	xe_vm_remove_compute_exec_queue(q->vm, q);
881 }
882 
883 int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data,
884 				struct drm_file *file)
885 {
886 	struct xe_device *xe = to_xe_device(dev);
887 	struct xe_file *xef = to_xe_file(file);
888 	struct drm_xe_exec_queue_destroy *args = data;
889 	struct xe_exec_queue *q;
890 
891 	if (XE_IOCTL_DBG(xe, args->pad) ||
892 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
893 		return -EINVAL;
894 
895 	mutex_lock(&xef->exec_queue.lock);
896 	q = xa_erase(&xef->exec_queue.xa, args->exec_queue_id);
897 	if (q)
898 		atomic_inc(&xef->exec_queue.pending_removal);
899 	mutex_unlock(&xef->exec_queue.lock);
900 
901 	if (XE_IOCTL_DBG(xe, !q))
902 		return -ENOENT;
903 
904 	if (q->vm && q->hwe->hw_engine_group)
905 		xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
906 
907 	xe_exec_queue_kill(q);
908 
909 	trace_xe_exec_queue_close(q);
910 	xe_exec_queue_put(q);
911 
912 	return 0;
913 }
914 
915 static void xe_exec_queue_last_fence_lockdep_assert(struct xe_exec_queue *q,
916 						    struct xe_vm *vm)
917 {
918 	if (q->flags & EXEC_QUEUE_FLAG_VM) {
919 		lockdep_assert_held(&vm->lock);
920 	} else {
921 		xe_vm_assert_held(vm);
922 		lockdep_assert_held(&q->hwe->hw_engine_group->mode_sem);
923 	}
924 }
925 
926 /**
927  * xe_exec_queue_last_fence_put() - Drop ref to last fence
928  * @q: The exec queue
929  * @vm: The VM the engine does a bind or exec for
930  */
931 void xe_exec_queue_last_fence_put(struct xe_exec_queue *q, struct xe_vm *vm)
932 {
933 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
934 
935 	xe_exec_queue_last_fence_put_unlocked(q);
936 }
937 
938 /**
939  * xe_exec_queue_last_fence_put_unlocked() - Drop ref to last fence unlocked
940  * @q: The exec queue
941  *
942  * Only safe to be called from xe_exec_queue_destroy().
943  */
944 void xe_exec_queue_last_fence_put_unlocked(struct xe_exec_queue *q)
945 {
946 	if (q->last_fence) {
947 		dma_fence_put(q->last_fence);
948 		q->last_fence = NULL;
949 	}
950 }
951 
952 /**
953  * xe_exec_queue_last_fence_get() - Get last fence
954  * @q: The exec queue
955  * @vm: The VM the engine does a bind or exec for
956  *
957  * Get last fence, takes a ref
958  *
959  * Returns: last fence if not signaled, dma fence stub if signaled
960  */
961 struct dma_fence *xe_exec_queue_last_fence_get(struct xe_exec_queue *q,
962 					       struct xe_vm *vm)
963 {
964 	struct dma_fence *fence;
965 
966 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
967 
968 	if (q->last_fence &&
969 	    test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
970 		xe_exec_queue_last_fence_put(q, vm);
971 
972 	fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
973 	dma_fence_get(fence);
974 	return fence;
975 }
976 
977 /**
978  * xe_exec_queue_last_fence_get_for_resume() - Get last fence
979  * @q: The exec queue
980  * @vm: The VM the engine does a bind or exec for
981  *
982  * Get last fence, takes a ref. Only safe to be called in the context of
983  * resuming the hw engine group's long-running exec queue, when the group
984  * semaphore is held.
985  *
986  * Returns: last fence if not signaled, dma fence stub if signaled
987  */
988 struct dma_fence *xe_exec_queue_last_fence_get_for_resume(struct xe_exec_queue *q,
989 							  struct xe_vm *vm)
990 {
991 	struct dma_fence *fence;
992 
993 	lockdep_assert_held_write(&q->hwe->hw_engine_group->mode_sem);
994 
995 	if (q->last_fence &&
996 	    test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags))
997 		xe_exec_queue_last_fence_put_unlocked(q);
998 
999 	fence = q->last_fence ? q->last_fence : dma_fence_get_stub();
1000 	dma_fence_get(fence);
1001 	return fence;
1002 }
1003 
1004 /**
1005  * xe_exec_queue_last_fence_set() - Set last fence
1006  * @q: The exec queue
1007  * @vm: The VM the engine does a bind or exec for
1008  * @fence: The fence
1009  *
1010  * Set the last fence for the engine. Increases reference count for fence, when
1011  * closing engine xe_exec_queue_last_fence_put should be called.
1012  */
1013 void xe_exec_queue_last_fence_set(struct xe_exec_queue *q, struct xe_vm *vm,
1014 				  struct dma_fence *fence)
1015 {
1016 	xe_exec_queue_last_fence_lockdep_assert(q, vm);
1017 
1018 	xe_exec_queue_last_fence_put(q, vm);
1019 	q->last_fence = dma_fence_get(fence);
1020 }
1021 
1022 /**
1023  * xe_exec_queue_last_fence_test_dep - Test last fence dependency of queue
1024  * @q: The exec queue
1025  * @vm: The VM the engine does a bind or exec for
1026  *
1027  * Returns:
1028  * -ETIME if there exists an unsignalled last fence dependency, zero otherwise.
1029  */
1030 int xe_exec_queue_last_fence_test_dep(struct xe_exec_queue *q, struct xe_vm *vm)
1031 {
1032 	struct dma_fence *fence;
1033 	int err = 0;
1034 
1035 	fence = xe_exec_queue_last_fence_get(q, vm);
1036 	if (fence) {
1037 		err = test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) ?
1038 			0 : -ETIME;
1039 		dma_fence_put(fence);
1040 	}
1041 
1042 	return err;
1043 }
1044