xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c (revision 504f9bdd3a1588604b0452bfe927ff86e5f6e6df)
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_userq_fence.h"
33 
34 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
35 {
36 	int i;
37 	u32 userq_ip_mask = 0;
38 
39 	for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {
40 		if (adev->userq_funcs[i])
41 			userq_ip_mask |= (1 << i);
42 	}
43 
44 	return userq_ip_mask;
45 }
46 
47 static int
48 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
49 			  struct amdgpu_usermode_queue *queue)
50 {
51 	struct amdgpu_device *adev = uq_mgr->adev;
52 	const struct amdgpu_userq_funcs *userq_funcs =
53 		adev->userq_funcs[queue->queue_type];
54 	int r = 0;
55 
56 	if (queue->state == AMDGPU_USERQ_STATE_MAPPED) {
57 		r = userq_funcs->unmap(uq_mgr, queue);
58 		if (r)
59 			queue->state = AMDGPU_USERQ_STATE_HUNG;
60 		else
61 			queue->state = AMDGPU_USERQ_STATE_UNMAPPED;
62 	}
63 	return r;
64 }
65 
66 static int
67 amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr,
68 			struct amdgpu_usermode_queue *queue)
69 {
70 	struct amdgpu_device *adev = uq_mgr->adev;
71 	const struct amdgpu_userq_funcs *userq_funcs =
72 		adev->userq_funcs[queue->queue_type];
73 	int r = 0;
74 
75 	if (queue->state == AMDGPU_USERQ_STATE_UNMAPPED) {
76 		r = userq_funcs->map(uq_mgr, queue);
77 		if (r) {
78 			queue->state = AMDGPU_USERQ_STATE_HUNG;
79 		} else {
80 			queue->state = AMDGPU_USERQ_STATE_MAPPED;
81 		}
82 	}
83 	return r;
84 }
85 
86 static void
87 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
88 				 struct amdgpu_usermode_queue *queue)
89 {
90 	struct dma_fence *f = queue->last_fence;
91 	int ret;
92 
93 	if (f && !dma_fence_is_signaled(f)) {
94 		ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
95 		if (ret <= 0)
96 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
97 				     f->context, f->seqno);
98 	}
99 }
100 
101 static void
102 amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
103 		     struct amdgpu_usermode_queue *queue,
104 		     int queue_id)
105 {
106 	struct amdgpu_device *adev = uq_mgr->adev;
107 	const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
108 
109 	uq_funcs->mqd_destroy(uq_mgr, queue);
110 	amdgpu_userq_fence_driver_free(queue);
111 	idr_remove(&uq_mgr->userq_idr, queue_id);
112 	kfree(queue);
113 }
114 
115 int
116 amdgpu_userq_active(struct amdgpu_userq_mgr *uq_mgr)
117 {
118 	struct amdgpu_usermode_queue *queue;
119 	int queue_id;
120 	int ret = 0;
121 
122 	mutex_lock(&uq_mgr->userq_mutex);
123 	/* Resume all the queues for this process */
124 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id)
125 		ret += queue->state == AMDGPU_USERQ_STATE_MAPPED;
126 
127 	mutex_unlock(&uq_mgr->userq_mutex);
128 	return ret;
129 }
130 
131 static struct amdgpu_usermode_queue *
132 amdgpu_userq_find(struct amdgpu_userq_mgr *uq_mgr, int qid)
133 {
134 	return idr_find(&uq_mgr->userq_idr, qid);
135 }
136 
137 void
138 amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
139 			     struct amdgpu_eviction_fence_mgr *evf_mgr)
140 {
141 	struct amdgpu_eviction_fence *ev_fence;
142 
143 retry:
144 	/* Flush any pending resume work to create ev_fence */
145 	flush_delayed_work(&uq_mgr->resume_work);
146 
147 	mutex_lock(&uq_mgr->userq_mutex);
148 	spin_lock(&evf_mgr->ev_fence_lock);
149 	ev_fence = evf_mgr->ev_fence;
150 	spin_unlock(&evf_mgr->ev_fence_lock);
151 	if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
152 		mutex_unlock(&uq_mgr->userq_mutex);
153 		/*
154 		 * Looks like there was no pending resume work,
155 		 * add one now to create a valid eviction fence
156 		 */
157 		schedule_delayed_work(&uq_mgr->resume_work, 0);
158 		goto retry;
159 	}
160 }
161 
162 int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr,
163 			       struct amdgpu_userq_obj *userq_obj,
164 			       int size)
165 {
166 	struct amdgpu_device *adev = uq_mgr->adev;
167 	struct amdgpu_bo_param bp;
168 	int r;
169 
170 	memset(&bp, 0, sizeof(bp));
171 	bp.byte_align = PAGE_SIZE;
172 	bp.domain = AMDGPU_GEM_DOMAIN_GTT;
173 	bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
174 		   AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
175 	bp.type = ttm_bo_type_kernel;
176 	bp.size = size;
177 	bp.resv = NULL;
178 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
179 
180 	r = amdgpu_bo_create(adev, &bp, &userq_obj->obj);
181 	if (r) {
182 		drm_file_err(uq_mgr->file, "Failed to allocate BO for userqueue (%d)", r);
183 		return r;
184 	}
185 
186 	r = amdgpu_bo_reserve(userq_obj->obj, true);
187 	if (r) {
188 		drm_file_err(uq_mgr->file, "Failed to reserve BO to map (%d)", r);
189 		goto free_obj;
190 	}
191 
192 	r = amdgpu_ttm_alloc_gart(&(userq_obj->obj)->tbo);
193 	if (r) {
194 		drm_file_err(uq_mgr->file, "Failed to alloc GART for userqueue object (%d)", r);
195 		goto unresv;
196 	}
197 
198 	r = amdgpu_bo_kmap(userq_obj->obj, &userq_obj->cpu_ptr);
199 	if (r) {
200 		drm_file_err(uq_mgr->file, "Failed to map BO for userqueue (%d)", r);
201 		goto unresv;
202 	}
203 
204 	userq_obj->gpu_addr = amdgpu_bo_gpu_offset(userq_obj->obj);
205 	amdgpu_bo_unreserve(userq_obj->obj);
206 	memset(userq_obj->cpu_ptr, 0, size);
207 	return 0;
208 
209 unresv:
210 	amdgpu_bo_unreserve(userq_obj->obj);
211 
212 free_obj:
213 	amdgpu_bo_unref(&userq_obj->obj);
214 	return r;
215 }
216 
217 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
218 				 struct amdgpu_userq_obj *userq_obj)
219 {
220 	amdgpu_bo_kunmap(userq_obj->obj);
221 	amdgpu_bo_unref(&userq_obj->obj);
222 }
223 
224 uint64_t
225 amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,
226 				struct amdgpu_db_info *db_info,
227 				struct drm_file *filp)
228 {
229 	uint64_t index;
230 	struct drm_gem_object *gobj;
231 	struct amdgpu_userq_obj *db_obj = db_info->db_obj;
232 	int r, db_size;
233 
234 	gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle);
235 	if (gobj == NULL) {
236 		drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n");
237 		return -EINVAL;
238 	}
239 
240 	db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
241 	drm_gem_object_put(gobj);
242 
243 	r = amdgpu_bo_reserve(db_obj->obj, true);
244 	if (r) {
245 		drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n");
246 		goto unref_bo;
247 	}
248 
249 	/* Pin the BO before generating the index, unpin in queue destroy */
250 	r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL);
251 	if (r) {
252 		drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n");
253 		goto unresv_bo;
254 	}
255 
256 	switch (db_info->queue_type) {
257 	case AMDGPU_HW_IP_GFX:
258 	case AMDGPU_HW_IP_COMPUTE:
259 	case AMDGPU_HW_IP_DMA:
260 		db_size = sizeof(u64);
261 		break;
262 
263 	case AMDGPU_HW_IP_VCN_ENC:
264 		db_size = sizeof(u32);
265 		db_info->doorbell_offset += AMDGPU_NAVI10_DOORBELL64_VCN0_1 << 1;
266 		break;
267 
268 	case AMDGPU_HW_IP_VPE:
269 		db_size = sizeof(u32);
270 		db_info->doorbell_offset += AMDGPU_NAVI10_DOORBELL64_VPE << 1;
271 		break;
272 
273 	default:
274 		drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n",
275 			     db_info->queue_type);
276 		r = -EINVAL;
277 		goto unpin_bo;
278 	}
279 
280 	index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj,
281 					     db_info->doorbell_offset, db_size);
282 	drm_dbg_driver(adev_to_drm(uq_mgr->adev),
283 		       "[Usermode queues] doorbell index=%lld\n", index);
284 	amdgpu_bo_unreserve(db_obj->obj);
285 	return index;
286 
287 unpin_bo:
288 	amdgpu_bo_unpin(db_obj->obj);
289 unresv_bo:
290 	amdgpu_bo_unreserve(db_obj->obj);
291 unref_bo:
292 	amdgpu_bo_unref(&db_obj->obj);
293 	return r;
294 }
295 
296 static int
297 amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
298 {
299 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
300 	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
301 	struct amdgpu_device *adev = uq_mgr->adev;
302 	struct amdgpu_usermode_queue *queue;
303 	int r = 0;
304 
305 	cancel_delayed_work_sync(&uq_mgr->resume_work);
306 	mutex_lock(&uq_mgr->userq_mutex);
307 
308 	queue = amdgpu_userq_find(uq_mgr, queue_id);
309 	if (!queue) {
310 		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
311 		mutex_unlock(&uq_mgr->userq_mutex);
312 		return -EINVAL;
313 	}
314 	amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
315 	r = amdgpu_bo_reserve(queue->db_obj.obj, true);
316 	if (!r) {
317 		amdgpu_bo_unpin(queue->db_obj.obj);
318 		amdgpu_bo_unreserve(queue->db_obj.obj);
319 	}
320 	amdgpu_bo_unref(&queue->db_obj.obj);
321 
322 #if defined(CONFIG_DEBUG_FS)
323 	debugfs_remove_recursive(queue->debugfs_queue);
324 #endif
325 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
326 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
327 	mutex_unlock(&uq_mgr->userq_mutex);
328 
329 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
330 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
331 
332 	return r;
333 }
334 
335 static int amdgpu_userq_priority_permit(struct drm_file *filp,
336 					int priority)
337 {
338 	if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH)
339 		return 0;
340 
341 	if (capable(CAP_SYS_NICE))
342 		return 0;
343 
344 	if (drm_is_current_master(filp))
345 		return 0;
346 
347 	return -EACCES;
348 }
349 
350 #if defined(CONFIG_DEBUG_FS)
351 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused)
352 {
353 	struct amdgpu_usermode_queue *queue = m->private;
354 	struct amdgpu_bo *bo;
355 	int r;
356 
357 	if (!queue || !queue->mqd.obj)
358 		return -EINVAL;
359 
360 	bo = amdgpu_bo_ref(queue->mqd.obj);
361 	r = amdgpu_bo_reserve(bo, true);
362 	if (r) {
363 		amdgpu_bo_unref(&bo);
364 		return -EINVAL;
365 	}
366 
367 	seq_printf(m, "queue_type %d\n", queue->queue_type);
368 	seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj));
369 
370 	amdgpu_bo_unreserve(bo);
371 	amdgpu_bo_unref(&bo);
372 
373 	return 0;
374 }
375 
376 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file)
377 {
378 	return single_open(file, amdgpu_mqd_info_read, inode->i_private);
379 }
380 
381 static const struct file_operations amdgpu_mqd_info_fops = {
382 	.owner = THIS_MODULE,
383 	.open = amdgpu_mqd_info_open,
384 	.read = seq_read,
385 	.llseek = seq_lseek,
386 	.release = single_release,
387 };
388 #endif
389 
390 static int
391 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
392 {
393 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
394 	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
395 	struct amdgpu_device *adev = uq_mgr->adev;
396 	const struct amdgpu_userq_funcs *uq_funcs;
397 	struct amdgpu_usermode_queue *queue;
398 	struct amdgpu_db_info db_info;
399 	char *queue_name;
400 	bool skip_map_queue;
401 	uint64_t index;
402 	int qid, r = 0;
403 	int priority =
404 		(args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>
405 		AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
406 
407 	/* Usermode queues are only supported for GFX IP as of now */
408 	if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
409 	    args->in.ip_type != AMDGPU_HW_IP_DMA &&
410 	    args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
411 		drm_file_err(uq_mgr->file, "Usermode queue doesn't support IP type %u\n",
412 			     args->in.ip_type);
413 		return -EINVAL;
414 	}
415 
416 	r = amdgpu_userq_priority_permit(filp, priority);
417 	if (r)
418 		return r;
419 
420 	if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
421 	    (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
422 	    (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
423 	    !amdgpu_is_tmz(adev)) {
424 		drm_file_err(uq_mgr->file, "Secure only supported on GFX/Compute queues\n");
425 		return -EINVAL;
426 	}
427 
428 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
429 	if (r < 0) {
430 		drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n");
431 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
432 		return r;
433 	}
434 
435 	/*
436 	 * There could be a situation that we are creating a new queue while
437 	 * the other queues under this UQ_mgr are suspended. So if there is any
438 	 * resume work pending, wait for it to get done.
439 	 *
440 	 * This will also make sure we have a valid eviction fence ready to be used.
441 	 */
442 	mutex_lock(&adev->userq_mutex);
443 	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
444 
445 	uq_funcs = adev->userq_funcs[args->in.ip_type];
446 	if (!uq_funcs) {
447 		drm_file_err(uq_mgr->file, "Usermode queue is not supported for this IP (%u)\n",
448 			     args->in.ip_type);
449 		r = -EINVAL;
450 		goto unlock;
451 	}
452 
453 	queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
454 	if (!queue) {
455 		drm_file_err(uq_mgr->file, "Failed to allocate memory for queue\n");
456 		r = -ENOMEM;
457 		goto unlock;
458 	}
459 	queue->doorbell_handle = args->in.doorbell_handle;
460 	queue->queue_type = args->in.ip_type;
461 	queue->vm = &fpriv->vm;
462 	queue->priority = priority;
463 
464 	db_info.queue_type = queue->queue_type;
465 	db_info.doorbell_handle = queue->doorbell_handle;
466 	db_info.db_obj = &queue->db_obj;
467 	db_info.doorbell_offset = args->in.doorbell_offset;
468 
469 	/* Convert relative doorbell offset into absolute doorbell index */
470 	index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
471 	if (index == (uint64_t)-EINVAL) {
472 		drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n");
473 		kfree(queue);
474 		goto unlock;
475 	}
476 
477 	queue->doorbell_index = index;
478 	xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC);
479 	r = amdgpu_userq_fence_driver_alloc(adev, queue);
480 	if (r) {
481 		drm_file_err(uq_mgr->file, "Failed to alloc fence driver\n");
482 		goto unlock;
483 	}
484 
485 	r = uq_funcs->mqd_create(uq_mgr, &args->in, queue);
486 	if (r) {
487 		drm_file_err(uq_mgr->file, "Failed to create Queue\n");
488 		amdgpu_userq_fence_driver_free(queue);
489 		kfree(queue);
490 		goto unlock;
491 	}
492 
493 
494 	qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);
495 	if (qid < 0) {
496 		drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n");
497 		amdgpu_userq_fence_driver_free(queue);
498 		uq_funcs->mqd_destroy(uq_mgr, queue);
499 		kfree(queue);
500 		r = -ENOMEM;
501 		goto unlock;
502 	}
503 
504 	/* don't map the queue if scheduling is halted */
505 	if (adev->userq_halt_for_enforce_isolation &&
506 	    ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
507 	     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)))
508 		skip_map_queue = true;
509 	else
510 		skip_map_queue = false;
511 	if (!skip_map_queue) {
512 		r = amdgpu_userq_map_helper(uq_mgr, queue);
513 		if (r) {
514 			drm_file_err(uq_mgr->file, "Failed to map Queue\n");
515 			idr_remove(&uq_mgr->userq_idr, qid);
516 			amdgpu_userq_fence_driver_free(queue);
517 			uq_funcs->mqd_destroy(uq_mgr, queue);
518 			kfree(queue);
519 			goto unlock;
520 		}
521 	}
522 
523 	queue_name = kasprintf(GFP_KERNEL, "queue-%d", qid);
524 	if (!queue_name)
525 		return -ENOMEM;
526 
527 #if defined(CONFIG_DEBUG_FS)
528 	/* Queue dentry per client to hold MQD information   */
529 	queue->debugfs_queue = debugfs_create_dir(queue_name, filp->debugfs_client);
530 	debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops);
531 #endif
532 	kfree(queue_name);
533 
534 	args->out.queue_id = qid;
535 
536 unlock:
537 	mutex_unlock(&uq_mgr->userq_mutex);
538 	mutex_unlock(&adev->userq_mutex);
539 
540 	return r;
541 }
542 
543 int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
544 		       struct drm_file *filp)
545 {
546 	union drm_amdgpu_userq *args = data;
547 	int r;
548 
549 	switch (args->in.op) {
550 	case AMDGPU_USERQ_OP_CREATE:
551 		if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
552 				       AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))
553 			return -EINVAL;
554 		r = amdgpu_userq_create(filp, args);
555 		if (r)
556 			drm_file_err(filp, "Failed to create usermode queue\n");
557 		break;
558 
559 	case AMDGPU_USERQ_OP_FREE:
560 		if (args->in.ip_type ||
561 		    args->in.doorbell_handle ||
562 		    args->in.doorbell_offset ||
563 		    args->in.flags ||
564 		    args->in.queue_va ||
565 		    args->in.queue_size ||
566 		    args->in.rptr_va ||
567 		    args->in.wptr_va ||
568 		    args->in.wptr_va ||
569 		    args->in.mqd ||
570 		    args->in.mqd_size)
571 			return -EINVAL;
572 		r = amdgpu_userq_destroy(filp, args->in.queue_id);
573 		if (r)
574 			drm_file_err(filp, "Failed to destroy usermode queue\n");
575 		break;
576 
577 	default:
578 		drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
579 		return -EINVAL;
580 	}
581 
582 	return r;
583 }
584 
585 static int
586 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
587 {
588 	struct amdgpu_usermode_queue *queue;
589 	int queue_id;
590 	int ret = 0, r;
591 
592 	/* Resume all the queues for this process */
593 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
594 		r = amdgpu_userq_map_helper(uq_mgr, queue);
595 		if (r)
596 			ret = r;
597 	}
598 
599 	if (ret)
600 		drm_file_err(uq_mgr->file, "Failed to map all the queues\n");
601 	return ret;
602 }
603 
604 static int
605 amdgpu_userq_validate_vm_bo(void *_unused, struct amdgpu_bo *bo)
606 {
607 	struct ttm_operation_ctx ctx = { false, false };
608 	int ret;
609 
610 	amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
611 
612 	ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
613 	if (ret)
614 		DRM_ERROR("Fail to validate\n");
615 
616 	return ret;
617 }
618 
619 static int
620 amdgpu_userq_validate_bos(struct amdgpu_userq_mgr *uq_mgr)
621 {
622 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
623 	struct amdgpu_vm *vm = &fpriv->vm;
624 	struct amdgpu_device *adev = uq_mgr->adev;
625 	struct amdgpu_bo_va *bo_va;
626 	struct ww_acquire_ctx *ticket;
627 	struct drm_exec exec;
628 	struct amdgpu_bo *bo;
629 	struct dma_resv *resv;
630 	bool clear, unlock;
631 	int ret = 0;
632 
633 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
634 	drm_exec_until_all_locked(&exec) {
635 		ret = amdgpu_vm_lock_pd(vm, &exec, 2);
636 		drm_exec_retry_on_contention(&exec);
637 		if (unlikely(ret)) {
638 			drm_file_err(uq_mgr->file, "Failed to lock PD\n");
639 			goto unlock_all;
640 		}
641 
642 		/* Lock the done list */
643 		list_for_each_entry(bo_va, &vm->done, base.vm_status) {
644 			bo = bo_va->base.bo;
645 			if (!bo)
646 				continue;
647 
648 			ret = drm_exec_lock_obj(&exec, &bo->tbo.base);
649 			drm_exec_retry_on_contention(&exec);
650 			if (unlikely(ret))
651 				goto unlock_all;
652 		}
653 	}
654 
655 	spin_lock(&vm->status_lock);
656 	while (!list_empty(&vm->moved)) {
657 		bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
658 					 base.vm_status);
659 		spin_unlock(&vm->status_lock);
660 
661 		/* Per VM BOs never need to bo cleared in the page tables */
662 		ret = amdgpu_vm_bo_update(adev, bo_va, false);
663 		if (ret)
664 			goto unlock_all;
665 		spin_lock(&vm->status_lock);
666 	}
667 
668 	ticket = &exec.ticket;
669 	while (!list_empty(&vm->invalidated)) {
670 		bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
671 					 base.vm_status);
672 		resv = bo_va->base.bo->tbo.base.resv;
673 		spin_unlock(&vm->status_lock);
674 
675 		bo = bo_va->base.bo;
676 		ret = amdgpu_userq_validate_vm_bo(NULL, bo);
677 		if (ret) {
678 			drm_file_err(uq_mgr->file, "Failed to validate BO\n");
679 			goto unlock_all;
680 		}
681 
682 		/* Try to reserve the BO to avoid clearing its ptes */
683 		if (!adev->debug_vm && dma_resv_trylock(resv)) {
684 			clear = false;
685 			unlock = true;
686 		/* The caller is already holding the reservation lock */
687 		} else if (dma_resv_locking_ctx(resv) == ticket) {
688 			clear = false;
689 			unlock = false;
690 		/* Somebody else is using the BO right now */
691 		} else {
692 			clear = true;
693 			unlock = false;
694 		}
695 
696 		ret = amdgpu_vm_bo_update(adev, bo_va, clear);
697 
698 		if (unlock)
699 			dma_resv_unlock(resv);
700 		if (ret)
701 			goto unlock_all;
702 
703 		spin_lock(&vm->status_lock);
704 	}
705 	spin_unlock(&vm->status_lock);
706 
707 	ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec);
708 	if (ret)
709 		drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n");
710 
711 unlock_all:
712 	drm_exec_fini(&exec);
713 	return ret;
714 }
715 
716 static void amdgpu_userq_restore_worker(struct work_struct *work)
717 {
718 	struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
719 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
720 	int ret;
721 
722 	flush_delayed_work(&fpriv->evf_mgr.suspend_work);
723 
724 	mutex_lock(&uq_mgr->userq_mutex);
725 
726 	ret = amdgpu_userq_validate_bos(uq_mgr);
727 	if (ret) {
728 		drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n");
729 		goto unlock;
730 	}
731 
732 	ret = amdgpu_userq_restore_all(uq_mgr);
733 	if (ret) {
734 		drm_file_err(uq_mgr->file, "Failed to restore all queues\n");
735 		goto unlock;
736 	}
737 
738 unlock:
739 	mutex_unlock(&uq_mgr->userq_mutex);
740 }
741 
742 static int
743 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
744 {
745 	struct amdgpu_usermode_queue *queue;
746 	int queue_id;
747 	int ret = 0, r;
748 
749 	/* Try to unmap all the queues in this process ctx */
750 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
751 		r = amdgpu_userq_unmap_helper(uq_mgr, queue);
752 		if (r)
753 			ret = r;
754 	}
755 
756 	if (ret)
757 		drm_file_err(uq_mgr->file, "Couldn't unmap all the queues\n");
758 	return ret;
759 }
760 
761 static int
762 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr)
763 {
764 	struct amdgpu_usermode_queue *queue;
765 	int queue_id, ret;
766 
767 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
768 		struct dma_fence *f = queue->last_fence;
769 
770 		if (!f || dma_fence_is_signaled(f))
771 			continue;
772 		ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
773 		if (ret <= 0) {
774 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
775 				     f->context, f->seqno);
776 			return -ETIMEDOUT;
777 		}
778 	}
779 
780 	return 0;
781 }
782 
783 void
784 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
785 		   struct amdgpu_eviction_fence *ev_fence)
786 {
787 	int ret;
788 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
789 	struct amdgpu_eviction_fence_mgr *evf_mgr = &fpriv->evf_mgr;
790 
791 	/* Wait for any pending userqueue fence work to finish */
792 	ret = amdgpu_userq_wait_for_signal(uq_mgr);
793 	if (ret) {
794 		drm_file_err(uq_mgr->file, "Not evicting userqueue, timeout waiting for work\n");
795 		return;
796 	}
797 
798 	ret = amdgpu_userq_evict_all(uq_mgr);
799 	if (ret) {
800 		drm_file_err(uq_mgr->file, "Failed to evict userqueue\n");
801 		return;
802 	}
803 
804 	/* Signal current eviction fence */
805 	amdgpu_eviction_fence_signal(evf_mgr, ev_fence);
806 
807 	if (evf_mgr->fd_closing) {
808 		cancel_delayed_work_sync(&uq_mgr->resume_work);
809 		return;
810 	}
811 
812 	/* Schedule a resume work */
813 	schedule_delayed_work(&uq_mgr->resume_work, 0);
814 }
815 
816 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
817 			  struct amdgpu_device *adev)
818 {
819 	mutex_init(&userq_mgr->userq_mutex);
820 	idr_init_base(&userq_mgr->userq_idr, 1);
821 	userq_mgr->adev = adev;
822 	userq_mgr->file = file_priv;
823 
824 	mutex_lock(&adev->userq_mutex);
825 	list_add(&userq_mgr->list, &adev->userq_mgr_list);
826 	mutex_unlock(&adev->userq_mutex);
827 
828 	INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
829 	return 0;
830 }
831 
832 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
833 {
834 	struct amdgpu_device *adev = userq_mgr->adev;
835 	struct amdgpu_usermode_queue *queue;
836 	struct amdgpu_userq_mgr *uqm, *tmp;
837 	uint32_t queue_id;
838 
839 	cancel_delayed_work_sync(&userq_mgr->resume_work);
840 
841 	mutex_lock(&adev->userq_mutex);
842 	mutex_lock(&userq_mgr->userq_mutex);
843 	idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
844 		amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
845 		amdgpu_userq_unmap_helper(userq_mgr, queue);
846 		amdgpu_userq_cleanup(userq_mgr, queue, queue_id);
847 	}
848 
849 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
850 		if (uqm == userq_mgr) {
851 			list_del(&uqm->list);
852 			break;
853 		}
854 	}
855 	idr_destroy(&userq_mgr->userq_idr);
856 	mutex_unlock(&userq_mgr->userq_mutex);
857 	mutex_unlock(&adev->userq_mutex);
858 	mutex_destroy(&userq_mgr->userq_mutex);
859 }
860 
861 int amdgpu_userq_suspend(struct amdgpu_device *adev)
862 {
863 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
864 	struct amdgpu_usermode_queue *queue;
865 	struct amdgpu_userq_mgr *uqm, *tmp;
866 	int queue_id;
867 	int ret = 0, r;
868 
869 	if (!ip_mask)
870 		return 0;
871 
872 	mutex_lock(&adev->userq_mutex);
873 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
874 		cancel_delayed_work_sync(&uqm->resume_work);
875 		mutex_lock(&uqm->userq_mutex);
876 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
877 			r = amdgpu_userq_unmap_helper(uqm, queue);
878 			if (r)
879 				ret = r;
880 		}
881 		mutex_unlock(&uqm->userq_mutex);
882 	}
883 	mutex_unlock(&adev->userq_mutex);
884 	return ret;
885 }
886 
887 int amdgpu_userq_resume(struct amdgpu_device *adev)
888 {
889 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
890 	struct amdgpu_usermode_queue *queue;
891 	struct amdgpu_userq_mgr *uqm, *tmp;
892 	int queue_id;
893 	int ret = 0, r;
894 
895 	if (!ip_mask)
896 		return 0;
897 
898 	mutex_lock(&adev->userq_mutex);
899 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
900 		mutex_lock(&uqm->userq_mutex);
901 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
902 			r = amdgpu_userq_map_helper(uqm, queue);
903 			if (r)
904 				ret = r;
905 		}
906 		mutex_unlock(&uqm->userq_mutex);
907 	}
908 	mutex_unlock(&adev->userq_mutex);
909 	return ret;
910 }
911 
912 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
913 						  u32 idx)
914 {
915 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
916 	struct amdgpu_usermode_queue *queue;
917 	struct amdgpu_userq_mgr *uqm, *tmp;
918 	int queue_id;
919 	int ret = 0, r;
920 
921 	/* only need to stop gfx/compute */
922 	if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
923 		return 0;
924 
925 	mutex_lock(&adev->userq_mutex);
926 	if (adev->userq_halt_for_enforce_isolation)
927 		dev_warn(adev->dev, "userq scheduling already stopped!\n");
928 	adev->userq_halt_for_enforce_isolation = true;
929 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
930 		cancel_delayed_work_sync(&uqm->resume_work);
931 		mutex_lock(&uqm->userq_mutex);
932 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
933 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
934 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
935 			    (queue->xcp_id == idx)) {
936 				r = amdgpu_userq_unmap_helper(uqm, queue);
937 				if (r)
938 					ret = r;
939 			}
940 		}
941 		mutex_unlock(&uqm->userq_mutex);
942 	}
943 	mutex_unlock(&adev->userq_mutex);
944 	return ret;
945 }
946 
947 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
948 						   u32 idx)
949 {
950 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
951 	struct amdgpu_usermode_queue *queue;
952 	struct amdgpu_userq_mgr *uqm, *tmp;
953 	int queue_id;
954 	int ret = 0, r;
955 
956 	/* only need to stop gfx/compute */
957 	if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
958 		return 0;
959 
960 	mutex_lock(&adev->userq_mutex);
961 	if (!adev->userq_halt_for_enforce_isolation)
962 		dev_warn(adev->dev, "userq scheduling already started!\n");
963 	adev->userq_halt_for_enforce_isolation = false;
964 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
965 		mutex_lock(&uqm->userq_mutex);
966 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
967 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
968 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
969 			    (queue->xcp_id == idx)) {
970 				r = amdgpu_userq_map_helper(uqm, queue);
971 				if (r)
972 					ret = r;
973 			}
974 		}
975 		mutex_unlock(&uqm->userq_mutex);
976 	}
977 	mutex_unlock(&adev->userq_mutex);
978 	return ret;
979 }
980