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