xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c (revision 6ff9385c07aa311f01f87307e6256231be7d8675)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2023 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include <drm/drm_auth.h>
26 #include <drm/drm_exec.h>
27 #include <linux/pm_runtime.h>
28 
29 #include "amdgpu.h"
30 #include "amdgpu_vm.h"
31 #include "amdgpu_userq.h"
32 #include "amdgpu_hmm.h"
33 #include "amdgpu_reset.h"
34 #include "amdgpu_userq_fence.h"
35 
36 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
37 {
38 	int i;
39 	u32 userq_ip_mask = 0;
40 
41 	for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {
42 		if (adev->userq_funcs[i])
43 			userq_ip_mask |= (1 << i);
44 	}
45 
46 	return userq_ip_mask;
47 }
48 
49 static int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue *queue,
50 					   struct amdgpu_bo_va_mapping *va_map, u64 addr)
51 {
52 	struct amdgpu_userq_va_cursor *va_cursor;
53 	struct userq_va_list;
54 
55 	va_cursor = kzalloc(sizeof(*va_cursor), GFP_KERNEL);
56 	if (!va_cursor)
57 		return -ENOMEM;
58 
59 	INIT_LIST_HEAD(&va_cursor->list);
60 	va_cursor->gpu_addr = addr;
61 	atomic_set(&va_map->bo_va->userq_va_mapped, 1);
62 	list_add(&va_cursor->list, &queue->userq_va_list);
63 
64 	return 0;
65 }
66 
67 int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
68 				   u64 addr, u64 expected_size)
69 {
70 	struct amdgpu_bo_va_mapping *va_map;
71 	struct amdgpu_vm *vm = queue->vm;
72 	u64 user_addr;
73 	u64 size;
74 	int r = 0;
75 
76 	user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT;
77 	size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
78 
79 	r = amdgpu_bo_reserve(vm->root.bo, false);
80 	if (r)
81 		return r;
82 
83 	va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
84 	if (!va_map) {
85 		r = -EINVAL;
86 		goto out_err;
87 	}
88 	/* Only validate the userq whether resident in the VM mapping range */
89 	if (user_addr >= va_map->start  &&
90 	    va_map->last - user_addr + 1 >= size) {
91 		amdgpu_userq_buffer_va_list_add(queue, va_map, user_addr);
92 		amdgpu_bo_unreserve(vm->root.bo);
93 		return 0;
94 	}
95 
96 	r = -EINVAL;
97 out_err:
98 	amdgpu_bo_unreserve(vm->root.bo);
99 	return r;
100 }
101 
102 static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
103 {
104 	struct amdgpu_bo_va_mapping *mapping;
105 	bool r;
106 
107 	if (amdgpu_bo_reserve(vm->root.bo, false))
108 		return false;
109 
110 	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
111 	if (!IS_ERR_OR_NULL(mapping) && atomic_read(&mapping->bo_va->userq_va_mapped))
112 		r = true;
113 	else
114 		r = false;
115 	amdgpu_bo_unreserve(vm->root.bo);
116 
117 	return r;
118 }
119 
120 static bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue)
121 {
122 	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
123 	int r = 0;
124 
125 	list_for_each_entry_safe(va_cursor, tmp, &queue->userq_va_list, list) {
126 		r += amdgpu_userq_buffer_va_mapped(queue->vm, va_cursor->gpu_addr);
127 		dev_dbg(queue->userq_mgr->adev->dev,
128 			"validate the userq mapping:%p va:%llx r:%d\n",
129 			queue, va_cursor->gpu_addr, r);
130 	}
131 
132 	if (r != 0)
133 		return true;
134 
135 	return false;
136 }
137 
138 static void amdgpu_userq_buffer_va_list_del(struct amdgpu_bo_va_mapping *mapping,
139 					    struct amdgpu_userq_va_cursor *va_cursor)
140 {
141 	atomic_set(&mapping->bo_va->userq_va_mapped, 0);
142 	list_del(&va_cursor->list);
143 	kfree(va_cursor);
144 }
145 
146 static int amdgpu_userq_buffer_vas_list_cleanup(struct amdgpu_device *adev,
147 						struct amdgpu_usermode_queue *queue)
148 {
149 	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
150 	struct amdgpu_bo_va_mapping *mapping;
151 	int r;
152 
153 	r = amdgpu_bo_reserve(queue->vm->root.bo, false);
154 	if (r)
155 		return r;
156 
157 	list_for_each_entry_safe(va_cursor, tmp, &queue->userq_va_list, list) {
158 		mapping = amdgpu_vm_bo_lookup_mapping(queue->vm, va_cursor->gpu_addr);
159 		if (!mapping) {
160 			r = -EINVAL;
161 			goto err;
162 		}
163 		dev_dbg(adev->dev, "delete the userq:%p va:%llx\n",
164 			queue, va_cursor->gpu_addr);
165 		amdgpu_userq_buffer_va_list_del(mapping, va_cursor);
166 	}
167 err:
168 	amdgpu_bo_unreserve(queue->vm->root.bo);
169 	return r;
170 }
171 
172 static int
173 amdgpu_userq_preempt_helper(struct amdgpu_userq_mgr *uq_mgr,
174 			  struct amdgpu_usermode_queue *queue)
175 {
176 	struct amdgpu_device *adev = uq_mgr->adev;
177 	const struct amdgpu_userq_funcs *userq_funcs =
178 		adev->userq_funcs[queue->queue_type];
179 	int r = 0;
180 
181 	if (queue->state == AMDGPU_USERQ_STATE_MAPPED) {
182 		r = userq_funcs->preempt(uq_mgr, queue);
183 		if (r) {
184 			queue->state = AMDGPU_USERQ_STATE_HUNG;
185 		} else {
186 			queue->state = AMDGPU_USERQ_STATE_PREEMPTED;
187 		}
188 	}
189 
190 	return r;
191 }
192 
193 static int
194 amdgpu_userq_restore_helper(struct amdgpu_userq_mgr *uq_mgr,
195 			struct amdgpu_usermode_queue *queue)
196 {
197 	struct amdgpu_device *adev = uq_mgr->adev;
198 	const struct amdgpu_userq_funcs *userq_funcs =
199 		adev->userq_funcs[queue->queue_type];
200 	int r = 0;
201 
202 	if (queue->state == AMDGPU_USERQ_STATE_PREEMPTED) {
203 		r = userq_funcs->restore(uq_mgr, queue);
204 		if (r) {
205 			queue->state = AMDGPU_USERQ_STATE_HUNG;
206 		} else {
207 			queue->state = AMDGPU_USERQ_STATE_MAPPED;
208 		}
209 	}
210 
211 	return r;
212 }
213 
214 static int
215 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
216 			  struct amdgpu_usermode_queue *queue)
217 {
218 	struct amdgpu_device *adev = uq_mgr->adev;
219 	const struct amdgpu_userq_funcs *userq_funcs =
220 		adev->userq_funcs[queue->queue_type];
221 	int r = 0;
222 
223 	if ((queue->state == AMDGPU_USERQ_STATE_MAPPED) ||
224 		(queue->state == AMDGPU_USERQ_STATE_PREEMPTED)) {
225 		r = userq_funcs->unmap(uq_mgr, queue);
226 		if (r)
227 			queue->state = AMDGPU_USERQ_STATE_HUNG;
228 		else
229 			queue->state = AMDGPU_USERQ_STATE_UNMAPPED;
230 	}
231 	return r;
232 }
233 
234 static int
235 amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr,
236 			struct amdgpu_usermode_queue *queue)
237 {
238 	struct amdgpu_device *adev = uq_mgr->adev;
239 	const struct amdgpu_userq_funcs *userq_funcs =
240 		adev->userq_funcs[queue->queue_type];
241 	int r = 0;
242 
243 	if (queue->state == AMDGPU_USERQ_STATE_UNMAPPED) {
244 		r = userq_funcs->map(uq_mgr, queue);
245 		if (r) {
246 			queue->state = AMDGPU_USERQ_STATE_HUNG;
247 		} else {
248 			queue->state = AMDGPU_USERQ_STATE_MAPPED;
249 		}
250 	}
251 	return r;
252 }
253 
254 static int
255 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
256 				 struct amdgpu_usermode_queue *queue)
257 {
258 	struct dma_fence *f = queue->last_fence;
259 	int ret = 0;
260 
261 	if (f && !dma_fence_is_signaled(f)) {
262 		ret = dma_fence_wait_timeout(f, true, MAX_SCHEDULE_TIMEOUT);
263 		if (ret <= 0) {
264 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
265 				     f->context, f->seqno);
266 			queue->state = AMDGPU_USERQ_STATE_HUNG;
267 			return -ETIME;
268 		}
269 	}
270 
271 	return ret;
272 }
273 
274 static void
275 amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
276 		     struct amdgpu_usermode_queue *queue,
277 		     int queue_id)
278 {
279 	struct amdgpu_device *adev = uq_mgr->adev;
280 	const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
281 
282 	/* Wait for mode-1 reset to complete */
283 	down_read(&adev->reset_domain->sem);
284 
285 	/* Drop the userq reference. */
286 	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
287 	uq_funcs->mqd_destroy(uq_mgr, queue);
288 	amdgpu_userq_fence_driver_free(queue);
289 	/* Use interrupt-safe locking since IRQ handlers may access these XArrays */
290 	xa_erase_irq(&uq_mgr->userq_mgr_xa, (unsigned long)queue_id);
291 	xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index);
292 	queue->userq_mgr = NULL;
293 	list_del(&queue->userq_va_list);
294 	kfree(queue);
295 
296 	up_read(&adev->reset_domain->sem);
297 }
298 
299 static struct amdgpu_usermode_queue *
300 amdgpu_userq_find(struct amdgpu_userq_mgr *uq_mgr, int qid)
301 {
302 	return xa_load(&uq_mgr->userq_mgr_xa, qid);
303 }
304 
305 void
306 amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
307 			     struct amdgpu_eviction_fence_mgr *evf_mgr)
308 {
309 	struct amdgpu_eviction_fence *ev_fence;
310 
311 retry:
312 	/* Flush any pending resume work to create ev_fence */
313 	flush_delayed_work(&uq_mgr->resume_work);
314 
315 	mutex_lock(&uq_mgr->userq_mutex);
316 	spin_lock(&evf_mgr->ev_fence_lock);
317 	ev_fence = evf_mgr->ev_fence;
318 	spin_unlock(&evf_mgr->ev_fence_lock);
319 	if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
320 		mutex_unlock(&uq_mgr->userq_mutex);
321 		/*
322 		 * Looks like there was no pending resume work,
323 		 * add one now to create a valid eviction fence
324 		 */
325 		schedule_delayed_work(&uq_mgr->resume_work, 0);
326 		goto retry;
327 	}
328 }
329 
330 int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr,
331 			       struct amdgpu_userq_obj *userq_obj,
332 			       int size)
333 {
334 	struct amdgpu_device *adev = uq_mgr->adev;
335 	struct amdgpu_bo_param bp;
336 	int r;
337 
338 	memset(&bp, 0, sizeof(bp));
339 	bp.byte_align = PAGE_SIZE;
340 	bp.domain = AMDGPU_GEM_DOMAIN_GTT;
341 	bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
342 		   AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
343 	bp.type = ttm_bo_type_kernel;
344 	bp.size = size;
345 	bp.resv = NULL;
346 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
347 
348 	r = amdgpu_bo_create(adev, &bp, &userq_obj->obj);
349 	if (r) {
350 		drm_file_err(uq_mgr->file, "Failed to allocate BO for userqueue (%d)", r);
351 		return r;
352 	}
353 
354 	r = amdgpu_bo_reserve(userq_obj->obj, true);
355 	if (r) {
356 		drm_file_err(uq_mgr->file, "Failed to reserve BO to map (%d)", r);
357 		goto free_obj;
358 	}
359 
360 	r = amdgpu_ttm_alloc_gart(&(userq_obj->obj)->tbo);
361 	if (r) {
362 		drm_file_err(uq_mgr->file, "Failed to alloc GART for userqueue object (%d)", r);
363 		goto unresv;
364 	}
365 
366 	r = amdgpu_bo_kmap(userq_obj->obj, &userq_obj->cpu_ptr);
367 	if (r) {
368 		drm_file_err(uq_mgr->file, "Failed to map BO for userqueue (%d)", r);
369 		goto unresv;
370 	}
371 
372 	userq_obj->gpu_addr = amdgpu_bo_gpu_offset(userq_obj->obj);
373 	amdgpu_bo_unreserve(userq_obj->obj);
374 	memset(userq_obj->cpu_ptr, 0, size);
375 	return 0;
376 
377 unresv:
378 	amdgpu_bo_unreserve(userq_obj->obj);
379 
380 free_obj:
381 	amdgpu_bo_unref(&userq_obj->obj);
382 	return r;
383 }
384 
385 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
386 				 struct amdgpu_userq_obj *userq_obj)
387 {
388 	amdgpu_bo_kunmap(userq_obj->obj);
389 	amdgpu_bo_unref(&userq_obj->obj);
390 }
391 
392 uint64_t
393 amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,
394 				struct amdgpu_db_info *db_info,
395 				struct drm_file *filp)
396 {
397 	uint64_t index;
398 	struct drm_gem_object *gobj;
399 	struct amdgpu_userq_obj *db_obj = db_info->db_obj;
400 	int r, db_size;
401 
402 	gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle);
403 	if (gobj == NULL) {
404 		drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n");
405 		return -EINVAL;
406 	}
407 
408 	db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
409 	drm_gem_object_put(gobj);
410 
411 	r = amdgpu_bo_reserve(db_obj->obj, true);
412 	if (r) {
413 		drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n");
414 		goto unref_bo;
415 	}
416 
417 	/* Pin the BO before generating the index, unpin in queue destroy */
418 	r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL);
419 	if (r) {
420 		drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n");
421 		goto unresv_bo;
422 	}
423 
424 	switch (db_info->queue_type) {
425 	case AMDGPU_HW_IP_GFX:
426 	case AMDGPU_HW_IP_COMPUTE:
427 	case AMDGPU_HW_IP_DMA:
428 		db_size = sizeof(u64);
429 		break;
430 	default:
431 		drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n",
432 			     db_info->queue_type);
433 		r = -EINVAL;
434 		goto unpin_bo;
435 	}
436 
437 	index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj,
438 					     db_info->doorbell_offset, db_size);
439 	drm_dbg_driver(adev_to_drm(uq_mgr->adev),
440 		       "[Usermode queues] doorbell index=%lld\n", index);
441 	amdgpu_bo_unreserve(db_obj->obj);
442 	return index;
443 
444 unpin_bo:
445 	amdgpu_bo_unpin(db_obj->obj);
446 unresv_bo:
447 	amdgpu_bo_unreserve(db_obj->obj);
448 unref_bo:
449 	amdgpu_bo_unref(&db_obj->obj);
450 	return r;
451 }
452 
453 static int
454 amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
455 {
456 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
457 	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
458 	struct amdgpu_device *adev = uq_mgr->adev;
459 	struct amdgpu_usermode_queue *queue;
460 	int r = 0;
461 
462 	cancel_delayed_work_sync(&uq_mgr->resume_work);
463 	mutex_lock(&uq_mgr->userq_mutex);
464 
465 	queue = amdgpu_userq_find(uq_mgr, queue_id);
466 	if (!queue) {
467 		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
468 		mutex_unlock(&uq_mgr->userq_mutex);
469 		return -EINVAL;
470 	}
471 	amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
472 	r = amdgpu_bo_reserve(queue->db_obj.obj, true);
473 	if (!r) {
474 		amdgpu_bo_unpin(queue->db_obj.obj);
475 		amdgpu_bo_unreserve(queue->db_obj.obj);
476 	}
477 	amdgpu_bo_unref(&queue->db_obj.obj);
478 
479 #if defined(CONFIG_DEBUG_FS)
480 	debugfs_remove_recursive(queue->debugfs_queue);
481 #endif
482 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
483 	/*TODO: It requires a reset for userq hw unmap error*/
484 	if (unlikely(r != AMDGPU_USERQ_STATE_UNMAPPED)) {
485 		drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n");
486 		queue->state = AMDGPU_USERQ_STATE_HUNG;
487 	}
488 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
489 	mutex_unlock(&uq_mgr->userq_mutex);
490 
491 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
492 
493 	return r;
494 }
495 
496 static int amdgpu_userq_priority_permit(struct drm_file *filp,
497 					int priority)
498 {
499 	if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH)
500 		return 0;
501 
502 	if (capable(CAP_SYS_NICE))
503 		return 0;
504 
505 	if (drm_is_current_master(filp))
506 		return 0;
507 
508 	return -EACCES;
509 }
510 
511 #if defined(CONFIG_DEBUG_FS)
512 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused)
513 {
514 	struct amdgpu_usermode_queue *queue = m->private;
515 	struct amdgpu_bo *bo;
516 	int r;
517 
518 	if (!queue || !queue->mqd.obj)
519 		return -EINVAL;
520 
521 	bo = amdgpu_bo_ref(queue->mqd.obj);
522 	r = amdgpu_bo_reserve(bo, true);
523 	if (r) {
524 		amdgpu_bo_unref(&bo);
525 		return -EINVAL;
526 	}
527 
528 	seq_printf(m, "queue_type: %d\n", queue->queue_type);
529 	seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj));
530 
531 	amdgpu_bo_unreserve(bo);
532 	amdgpu_bo_unref(&bo);
533 
534 	return 0;
535 }
536 
537 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file)
538 {
539 	return single_open(file, amdgpu_mqd_info_read, inode->i_private);
540 }
541 
542 static const struct file_operations amdgpu_mqd_info_fops = {
543 	.owner = THIS_MODULE,
544 	.open = amdgpu_mqd_info_open,
545 	.read = seq_read,
546 	.llseek = seq_lseek,
547 	.release = single_release,
548 };
549 #endif
550 
551 static int
552 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
553 {
554 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
555 	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
556 	struct amdgpu_device *adev = uq_mgr->adev;
557 	const struct amdgpu_userq_funcs *uq_funcs;
558 	struct amdgpu_usermode_queue *queue;
559 	struct amdgpu_db_info db_info;
560 	char *queue_name;
561 	bool skip_map_queue;
562 	u32 qid;
563 	uint64_t index;
564 	int r = 0;
565 	int priority =
566 		(args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>
567 		AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
568 
569 	r = amdgpu_userq_priority_permit(filp, priority);
570 	if (r)
571 		return r;
572 
573 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
574 	if (r < 0) {
575 		drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n");
576 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
577 		return r;
578 	}
579 
580 	/*
581 	 * There could be a situation that we are creating a new queue while
582 	 * the other queues under this UQ_mgr are suspended. So if there is any
583 	 * resume work pending, wait for it to get done.
584 	 *
585 	 * This will also make sure we have a valid eviction fence ready to be used.
586 	 */
587 	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
588 
589 	uq_funcs = adev->userq_funcs[args->in.ip_type];
590 	if (!uq_funcs) {
591 		drm_file_err(uq_mgr->file, "Usermode queue is not supported for this IP (%u)\n",
592 			     args->in.ip_type);
593 		r = -EINVAL;
594 		goto unlock;
595 	}
596 
597 	queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
598 	if (!queue) {
599 		drm_file_err(uq_mgr->file, "Failed to allocate memory for queue\n");
600 		r = -ENOMEM;
601 		goto unlock;
602 	}
603 
604 	INIT_LIST_HEAD(&queue->userq_va_list);
605 	queue->doorbell_handle = args->in.doorbell_handle;
606 	queue->queue_type = args->in.ip_type;
607 	queue->vm = &fpriv->vm;
608 	queue->priority = priority;
609 
610 	db_info.queue_type = queue->queue_type;
611 	db_info.doorbell_handle = queue->doorbell_handle;
612 	db_info.db_obj = &queue->db_obj;
613 	db_info.doorbell_offset = args->in.doorbell_offset;
614 
615 	/* Validate the userq virtual address.*/
616 	if (amdgpu_userq_input_va_validate(queue, args->in.queue_va, args->in.queue_size) ||
617 	    amdgpu_userq_input_va_validate(queue, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE) ||
618 	    amdgpu_userq_input_va_validate(queue, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) {
619 		r = -EINVAL;
620 		kfree(queue);
621 		goto unlock;
622 	}
623 
624 	/* Convert relative doorbell offset into absolute doorbell index */
625 	index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
626 	if (index == (uint64_t)-EINVAL) {
627 		drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n");
628 		kfree(queue);
629 		r = -EINVAL;
630 		goto unlock;
631 	}
632 
633 	queue->doorbell_index = index;
634 	xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC);
635 	r = amdgpu_userq_fence_driver_alloc(adev, queue);
636 	if (r) {
637 		drm_file_err(uq_mgr->file, "Failed to alloc fence driver\n");
638 		goto unlock;
639 	}
640 
641 	r = uq_funcs->mqd_create(uq_mgr, &args->in, queue);
642 	if (r) {
643 		drm_file_err(uq_mgr->file, "Failed to create Queue\n");
644 		amdgpu_userq_fence_driver_free(queue);
645 		kfree(queue);
646 		goto unlock;
647 	}
648 
649 	/* Wait for mode-1 reset to complete */
650 	down_read(&adev->reset_domain->sem);
651 	r = xa_err(xa_store_irq(&adev->userq_doorbell_xa, index, queue, GFP_KERNEL));
652 	if (r) {
653 		kfree(queue);
654 		up_read(&adev->reset_domain->sem);
655 		goto unlock;
656 	}
657 
658 	r = xa_alloc(&uq_mgr->userq_mgr_xa, &qid, queue, XA_LIMIT(1, AMDGPU_MAX_USERQ_COUNT), GFP_KERNEL);
659 	if (r) {
660 		drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n");
661 		amdgpu_userq_fence_driver_free(queue);
662 		uq_funcs->mqd_destroy(uq_mgr, queue);
663 		kfree(queue);
664 		r = -ENOMEM;
665 		up_read(&adev->reset_domain->sem);
666 		goto unlock;
667 	}
668 	up_read(&adev->reset_domain->sem);
669 	queue->userq_mgr = uq_mgr;
670 
671 	/* don't map the queue if scheduling is halted */
672 	if (adev->userq_halt_for_enforce_isolation &&
673 	    ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
674 	     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)))
675 		skip_map_queue = true;
676 	else
677 		skip_map_queue = false;
678 	if (!skip_map_queue) {
679 		r = amdgpu_userq_map_helper(uq_mgr, queue);
680 		if (r) {
681 			drm_file_err(uq_mgr->file, "Failed to map Queue\n");
682 			xa_erase(&uq_mgr->userq_mgr_xa, qid);
683 			amdgpu_userq_fence_driver_free(queue);
684 			uq_funcs->mqd_destroy(uq_mgr, queue);
685 			kfree(queue);
686 			goto unlock;
687 		}
688 	}
689 
690 	queue_name = kasprintf(GFP_KERNEL, "queue-%d", qid);
691 	if (!queue_name) {
692 		r = -ENOMEM;
693 		goto unlock;
694 	}
695 
696 #if defined(CONFIG_DEBUG_FS)
697 	/* Queue dentry per client to hold MQD information   */
698 	queue->debugfs_queue = debugfs_create_dir(queue_name, filp->debugfs_client);
699 	debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops);
700 #endif
701 	kfree(queue_name);
702 
703 	args->out.queue_id = qid;
704 
705 unlock:
706 	mutex_unlock(&uq_mgr->userq_mutex);
707 
708 	return r;
709 }
710 
711 static int amdgpu_userq_input_args_validate(struct drm_device *dev,
712 					union drm_amdgpu_userq *args,
713 					struct drm_file *filp)
714 {
715 	struct amdgpu_device *adev = drm_to_adev(dev);
716 
717 	switch (args->in.op) {
718 	case AMDGPU_USERQ_OP_CREATE:
719 		if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
720 				       AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))
721 			return -EINVAL;
722 		/* Usermode queues are only supported for GFX IP as of now */
723 		if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
724 		    args->in.ip_type != AMDGPU_HW_IP_DMA &&
725 		    args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
726 			drm_file_err(filp, "Usermode queue doesn't support IP type %u\n",
727 				     args->in.ip_type);
728 			return -EINVAL;
729 		}
730 
731 		if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
732 		    (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
733 		    (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
734 		    !amdgpu_is_tmz(adev)) {
735 			drm_file_err(filp, "Secure only supported on GFX/Compute queues\n");
736 			return -EINVAL;
737 		}
738 
739 		if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
740 		    args->in.queue_va == 0 ||
741 		    args->in.queue_size == 0) {
742 			drm_file_err(filp, "invalidate userq queue va or size\n");
743 			return -EINVAL;
744 		}
745 		if (!args->in.wptr_va || !args->in.rptr_va) {
746 			drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
747 			return -EINVAL;
748 		}
749 		break;
750 	case AMDGPU_USERQ_OP_FREE:
751 		if (args->in.ip_type ||
752 		    args->in.doorbell_handle ||
753 		    args->in.doorbell_offset ||
754 		    args->in.flags ||
755 		    args->in.queue_va ||
756 		    args->in.queue_size ||
757 		    args->in.rptr_va ||
758 		    args->in.wptr_va ||
759 		    args->in.mqd ||
760 		    args->in.mqd_size)
761 			return -EINVAL;
762 		break;
763 	default:
764 		return -EINVAL;
765 	}
766 
767 	return 0;
768 }
769 
770 int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
771 		       struct drm_file *filp)
772 {
773 	union drm_amdgpu_userq *args = data;
774 	int r;
775 
776 	if (amdgpu_userq_input_args_validate(dev, args, filp) < 0)
777 		return -EINVAL;
778 
779 	switch (args->in.op) {
780 	case AMDGPU_USERQ_OP_CREATE:
781 		r = amdgpu_userq_create(filp, args);
782 		if (r)
783 			drm_file_err(filp, "Failed to create usermode queue\n");
784 		break;
785 
786 	case AMDGPU_USERQ_OP_FREE:
787 		r = amdgpu_userq_destroy(filp, args->in.queue_id);
788 		if (r)
789 			drm_file_err(filp, "Failed to destroy usermode queue\n");
790 		break;
791 
792 	default:
793 		drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
794 		return -EINVAL;
795 	}
796 
797 	return r;
798 }
799 
800 static int
801 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
802 {
803 	struct amdgpu_usermode_queue *queue;
804 	unsigned long queue_id;
805 	int ret = 0, r;
806 
807 	/* Resume all the queues for this process */
808 	xa_for_each(&uq_mgr->userq_mgr_xa, queue_id, queue) {
809 
810 		if (!amdgpu_userq_buffer_vas_mapped(queue)) {
811 			drm_file_err(uq_mgr->file,
812 				     "trying restore queue without va mapping\n");
813 			queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
814 			continue;
815 		}
816 
817 		r = amdgpu_userq_restore_helper(uq_mgr, queue);
818 		if (r)
819 			ret = r;
820 	}
821 
822 	if (ret)
823 		drm_file_err(uq_mgr->file, "Failed to map all the queues\n");
824 	return ret;
825 }
826 
827 static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo)
828 {
829 	struct ttm_operation_ctx ctx = { false, false };
830 
831 	amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
832 	return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
833 }
834 
835 /* Handle all BOs on the invalidated list, validate them and update the PTs */
836 static int
837 amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,
838 			 struct amdgpu_vm *vm)
839 {
840 	struct ttm_operation_ctx ctx = { false, false };
841 	struct amdgpu_bo_va *bo_va;
842 	struct amdgpu_bo *bo;
843 	int ret;
844 
845 	spin_lock(&vm->status_lock);
846 	while (!list_empty(&vm->invalidated)) {
847 		bo_va = list_first_entry(&vm->invalidated,
848 					 struct amdgpu_bo_va,
849 					 base.vm_status);
850 		spin_unlock(&vm->status_lock);
851 
852 		bo = bo_va->base.bo;
853 		ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 2);
854 		if (unlikely(ret))
855 			return ret;
856 
857 		amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
858 		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
859 		if (ret)
860 			return ret;
861 
862 		/* This moves the bo_va to the done list */
863 		ret = amdgpu_vm_bo_update(adev, bo_va, false);
864 		if (ret)
865 			return ret;
866 
867 		spin_lock(&vm->status_lock);
868 	}
869 	spin_unlock(&vm->status_lock);
870 
871 	return 0;
872 }
873 
874 /* Make sure the whole VM is ready to be used */
875 static int
876 amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)
877 {
878 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
879 	bool invalidated = false, new_addition = false;
880 	struct ttm_operation_ctx ctx = { true, false };
881 	struct amdgpu_device *adev = uq_mgr->adev;
882 	struct amdgpu_hmm_range *range;
883 	struct amdgpu_vm *vm = &fpriv->vm;
884 	unsigned long key, tmp_key;
885 	struct amdgpu_bo_va *bo_va;
886 	struct amdgpu_bo *bo;
887 	struct drm_exec exec;
888 	struct xarray xa;
889 	int ret;
890 
891 	xa_init(&xa);
892 
893 retry_lock:
894 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
895 	drm_exec_until_all_locked(&exec) {
896 		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
897 		drm_exec_retry_on_contention(&exec);
898 		if (unlikely(ret))
899 			goto unlock_all;
900 
901 		ret = amdgpu_vm_lock_done_list(vm, &exec, 1);
902 		drm_exec_retry_on_contention(&exec);
903 		if (unlikely(ret))
904 			goto unlock_all;
905 
906 		/* This validates PDs, PTs and per VM BOs */
907 		ret = amdgpu_vm_validate(adev, vm, NULL,
908 					 amdgpu_userq_validate_vm,
909 					 NULL);
910 		if (unlikely(ret))
911 			goto unlock_all;
912 
913 		/* This locks and validates the remaining evicted BOs */
914 		ret = amdgpu_userq_bo_validate(adev, &exec, vm);
915 		drm_exec_retry_on_contention(&exec);
916 		if (unlikely(ret))
917 			goto unlock_all;
918 	}
919 
920 	if (invalidated) {
921 		xa_for_each(&xa, tmp_key, range) {
922 			bo = range->bo;
923 			amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
924 			ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
925 			if (ret)
926 				goto unlock_all;
927 
928 			amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
929 
930 			amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
931 			ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
932 			if (ret)
933 				goto unlock_all;
934 		}
935 		invalidated = false;
936 	}
937 
938 	ret = amdgpu_vm_handle_moved(adev, vm, NULL);
939 	if (ret)
940 		goto unlock_all;
941 
942 	key = 0;
943 	/* Validate User Ptr BOs */
944 	list_for_each_entry(bo_va, &vm->done, base.vm_status) {
945 		bo = bo_va->base.bo;
946 
947 		if (!amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
948 			continue;
949 
950 		range = xa_load(&xa, key);
951 		if (range && range->bo != bo) {
952 			xa_erase(&xa, key);
953 			amdgpu_hmm_range_free(range);
954 			range = NULL;
955 		}
956 
957 		if (!range) {
958 			range = amdgpu_hmm_range_alloc(bo);
959 			if (!range) {
960 				ret = -ENOMEM;
961 				goto unlock_all;
962 			}
963 
964 			xa_store(&xa, key, range, GFP_KERNEL);
965 			new_addition = true;
966 		}
967 		key++;
968 	}
969 
970 	if (new_addition) {
971 		drm_exec_fini(&exec);
972 		xa_for_each(&xa, tmp_key, range) {
973 			if (!range)
974 				continue;
975 			bo = range->bo;
976 			ret = amdgpu_ttm_tt_get_user_pages(bo, range);
977 			if (ret)
978 				goto unlock_all;
979 		}
980 
981 		invalidated = true;
982 		new_addition = false;
983 		goto retry_lock;
984 	}
985 
986 	ret = amdgpu_vm_update_pdes(adev, vm, false);
987 	if (ret)
988 		goto unlock_all;
989 
990 	/*
991 	 * We need to wait for all VM updates to finish before restarting the
992 	 * queues. Using the done list like that is now ok since everything is
993 	 * locked in place.
994 	 */
995 	list_for_each_entry(bo_va, &vm->done, base.vm_status)
996 		dma_fence_wait(bo_va->last_pt_update, false);
997 	dma_fence_wait(vm->last_update, false);
998 
999 	ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec);
1000 	if (ret)
1001 		drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n");
1002 
1003 unlock_all:
1004 	drm_exec_fini(&exec);
1005 	xa_for_each(&xa, tmp_key, range) {
1006 		if (!range)
1007 			continue;
1008 		bo = range->bo;
1009 		amdgpu_hmm_range_free(range);
1010 	}
1011 	xa_destroy(&xa);
1012 	return ret;
1013 }
1014 
1015 static void amdgpu_userq_restore_worker(struct work_struct *work)
1016 {
1017 	struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
1018 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
1019 	int ret;
1020 
1021 	flush_delayed_work(&fpriv->evf_mgr.suspend_work);
1022 
1023 	mutex_lock(&uq_mgr->userq_mutex);
1024 
1025 	ret = amdgpu_userq_vm_validate(uq_mgr);
1026 	if (ret) {
1027 		drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n");
1028 		goto unlock;
1029 	}
1030 
1031 	ret = amdgpu_userq_restore_all(uq_mgr);
1032 	if (ret) {
1033 		drm_file_err(uq_mgr->file, "Failed to restore all queues\n");
1034 		goto unlock;
1035 	}
1036 
1037 unlock:
1038 	mutex_unlock(&uq_mgr->userq_mutex);
1039 }
1040 
1041 static int
1042 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
1043 {
1044 	struct amdgpu_usermode_queue *queue;
1045 	unsigned long queue_id;
1046 	int ret = 0, r;
1047 
1048 	/* Try to unmap all the queues in this process ctx */
1049 	xa_for_each(&uq_mgr->userq_mgr_xa, queue_id, queue) {
1050 		r = amdgpu_userq_preempt_helper(uq_mgr, queue);
1051 		if (r)
1052 			ret = r;
1053 	}
1054 
1055 	if (ret)
1056 		drm_file_err(uq_mgr->file, "Couldn't unmap all the queues\n");
1057 	return ret;
1058 }
1059 
1060 static int
1061 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr)
1062 {
1063 	struct amdgpu_usermode_queue *queue;
1064 	unsigned long queue_id;
1065 	int ret;
1066 
1067 	xa_for_each(&uq_mgr->userq_mgr_xa, queue_id, queue) {
1068 		struct dma_fence *f = queue->last_fence;
1069 
1070 		if (!f || dma_fence_is_signaled(f))
1071 			continue;
1072 		ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
1073 		if (ret <= 0) {
1074 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
1075 				     f->context, f->seqno);
1076 			return -ETIMEDOUT;
1077 		}
1078 	}
1079 
1080 	return 0;
1081 }
1082 
1083 void
1084 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
1085 		   struct amdgpu_eviction_fence *ev_fence)
1086 {
1087 	int ret;
1088 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
1089 	struct amdgpu_eviction_fence_mgr *evf_mgr = &fpriv->evf_mgr;
1090 
1091 	/* Wait for any pending userqueue fence work to finish */
1092 	ret = amdgpu_userq_wait_for_signal(uq_mgr);
1093 	if (ret) {
1094 		drm_file_err(uq_mgr->file, "Not evicting userqueue, timeout waiting for work\n");
1095 		return;
1096 	}
1097 
1098 	ret = amdgpu_userq_evict_all(uq_mgr);
1099 	if (ret) {
1100 		drm_file_err(uq_mgr->file, "Failed to evict userqueue\n");
1101 		return;
1102 	}
1103 
1104 	/* Signal current eviction fence */
1105 	amdgpu_eviction_fence_signal(evf_mgr, ev_fence);
1106 
1107 	if (evf_mgr->fd_closing) {
1108 		cancel_delayed_work_sync(&uq_mgr->resume_work);
1109 		return;
1110 	}
1111 
1112 	/* Schedule a resume work */
1113 	schedule_delayed_work(&uq_mgr->resume_work, 0);
1114 }
1115 
1116 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
1117 			  struct amdgpu_device *adev)
1118 {
1119 	mutex_init(&userq_mgr->userq_mutex);
1120 	xa_init_flags(&userq_mgr->userq_mgr_xa, XA_FLAGS_ALLOC);
1121 	userq_mgr->adev = adev;
1122 	userq_mgr->file = file_priv;
1123 
1124 	INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
1125 	return 0;
1126 }
1127 
1128 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
1129 {
1130 	struct amdgpu_usermode_queue *queue;
1131 	unsigned long queue_id;
1132 
1133 	cancel_delayed_work_sync(&userq_mgr->resume_work);
1134 
1135 	mutex_lock(&userq_mgr->userq_mutex);
1136 	xa_for_each(&userq_mgr->userq_mgr_xa, queue_id, queue) {
1137 		amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
1138 		amdgpu_userq_unmap_helper(userq_mgr, queue);
1139 		amdgpu_userq_cleanup(userq_mgr, queue, queue_id);
1140 	}
1141 
1142 	xa_destroy(&userq_mgr->userq_mgr_xa);
1143 	mutex_unlock(&userq_mgr->userq_mutex);
1144 	mutex_destroy(&userq_mgr->userq_mutex);
1145 }
1146 
1147 int amdgpu_userq_suspend(struct amdgpu_device *adev)
1148 {
1149 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1150 	struct amdgpu_usermode_queue *queue;
1151 	struct amdgpu_userq_mgr *uqm;
1152 	unsigned long queue_id;
1153 	int r;
1154 
1155 	if (!ip_mask)
1156 		return 0;
1157 
1158 	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1159 		uqm = queue->userq_mgr;
1160 		cancel_delayed_work_sync(&uqm->resume_work);
1161 		guard(mutex)(&uqm->userq_mutex);
1162 		if (adev->in_s0ix)
1163 			r = amdgpu_userq_preempt_helper(uqm, queue);
1164 		else
1165 			r = amdgpu_userq_unmap_helper(uqm, queue);
1166 		if (r)
1167 			return r;
1168 	}
1169 	return 0;
1170 }
1171 
1172 int amdgpu_userq_resume(struct amdgpu_device *adev)
1173 {
1174 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1175 	struct amdgpu_usermode_queue *queue;
1176 	struct amdgpu_userq_mgr *uqm;
1177 	unsigned long queue_id;
1178 	int r;
1179 
1180 	if (!ip_mask)
1181 		return 0;
1182 
1183 	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1184 		uqm = queue->userq_mgr;
1185 		guard(mutex)(&uqm->userq_mutex);
1186 		if (adev->in_s0ix)
1187 			r = amdgpu_userq_restore_helper(uqm, queue);
1188 		else
1189 			r = amdgpu_userq_map_helper(uqm, queue);
1190 		if (r)
1191 			return r;
1192 	}
1193 
1194 	return 0;
1195 }
1196 
1197 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
1198 						  u32 idx)
1199 {
1200 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1201 	struct amdgpu_usermode_queue *queue;
1202 	struct amdgpu_userq_mgr *uqm;
1203 	unsigned long queue_id;
1204 	int ret = 0, r;
1205 
1206 	/* only need to stop gfx/compute */
1207 	if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
1208 		return 0;
1209 
1210 	if (adev->userq_halt_for_enforce_isolation)
1211 		dev_warn(adev->dev, "userq scheduling already stopped!\n");
1212 	adev->userq_halt_for_enforce_isolation = true;
1213 	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1214 		uqm = queue->userq_mgr;
1215 		cancel_delayed_work_sync(&uqm->resume_work);
1216 		mutex_lock(&uqm->userq_mutex);
1217 		if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
1218 		     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
1219 		    (queue->xcp_id == idx)) {
1220 			r = amdgpu_userq_preempt_helper(uqm, queue);
1221 			if (r)
1222 				ret = r;
1223 		}
1224 		mutex_unlock(&uqm->userq_mutex);
1225 	}
1226 
1227 	return ret;
1228 }
1229 
1230 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
1231 						   u32 idx)
1232 {
1233 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1234 	struct amdgpu_usermode_queue *queue;
1235 	struct amdgpu_userq_mgr *uqm;
1236 	unsigned long queue_id;
1237 	int ret = 0, r;
1238 
1239 	/* only need to stop gfx/compute */
1240 	if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
1241 		return 0;
1242 
1243 	if (!adev->userq_halt_for_enforce_isolation)
1244 		dev_warn(adev->dev, "userq scheduling already started!\n");
1245 	adev->userq_halt_for_enforce_isolation = false;
1246 	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
1247 		uqm = queue->userq_mgr;
1248 		mutex_lock(&uqm->userq_mutex);
1249 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
1250 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
1251 			    (queue->xcp_id == idx)) {
1252 				r = amdgpu_userq_restore_helper(uqm, queue);
1253 				if (r)
1254 					ret = r;
1255 			}
1256 		mutex_unlock(&uqm->userq_mutex);
1257 	}
1258 
1259 	return ret;
1260 }
1261 
1262 int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
1263 				       struct amdgpu_bo_va_mapping *mapping,
1264 				       uint64_t saddr)
1265 {
1266 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
1267 	struct amdgpu_bo_va *bo_va = mapping->bo_va;
1268 	struct dma_resv *resv = bo_va->base.bo->tbo.base.resv;
1269 	int ret = 0;
1270 
1271 	if (!ip_mask)
1272 		return 0;
1273 
1274 	dev_warn_once(adev->dev, "now unmapping a vital queue va:%llx\n", saddr);
1275 	/**
1276 	 * The userq VA mapping reservation should include the eviction fence,
1277 	 * if the eviction fence can't signal successfully during unmapping,
1278 	 * then driver will warn to flag this improper unmap of the userq VA.
1279 	 * Note: The eviction fence may be attached to different BOs, and this
1280 	 * unmap is only for one kind of userq VAs, so at this point suppose
1281 	 * the eviction fence is always unsignaled.
1282 	 */
1283 	if (!dma_resv_test_signaled(resv, DMA_RESV_USAGE_BOOKKEEP)) {
1284 		ret = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP, true,
1285 					    MAX_SCHEDULE_TIMEOUT);
1286 		if (ret <= 0)
1287 			return -EBUSY;
1288 	}
1289 
1290 	return 0;
1291 }
1292