xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c (revision 5d95cbf21a4a550f2a2050c947083de2587cf46d)
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 	debugfs_remove_recursive(queue->debugfs_queue);
323 
324 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
325 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
326 	mutex_unlock(&uq_mgr->userq_mutex);
327 
328 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
329 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
330 
331 	return r;
332 }
333 
334 static int amdgpu_userq_priority_permit(struct drm_file *filp,
335 					int priority)
336 {
337 	if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH)
338 		return 0;
339 
340 	if (capable(CAP_SYS_NICE))
341 		return 0;
342 
343 	if (drm_is_current_master(filp))
344 		return 0;
345 
346 	return -EACCES;
347 }
348 
349 #if defined(CONFIG_DEBUG_FS)
350 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused)
351 {
352 	struct amdgpu_usermode_queue *queue = m->private;
353 	struct amdgpu_bo *bo;
354 	int r;
355 
356 	if (!queue || !queue->mqd.obj)
357 		return -EINVAL;
358 
359 	bo = amdgpu_bo_ref(queue->mqd.obj);
360 	r = amdgpu_bo_reserve(bo, true);
361 	if (r) {
362 		amdgpu_bo_unref(&bo);
363 		return -EINVAL;
364 	}
365 
366 	seq_printf(m, "queue_type %d\n", queue->queue_type);
367 	seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj));
368 
369 	amdgpu_bo_unreserve(bo);
370 	amdgpu_bo_unref(&bo);
371 
372 	return 0;
373 }
374 
375 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file)
376 {
377 	return single_open(file, amdgpu_mqd_info_read, inode->i_private);
378 }
379 
380 static const struct file_operations amdgpu_mqd_info_fops = {
381 	.owner = THIS_MODULE,
382 	.open = amdgpu_mqd_info_open,
383 	.read = seq_read,
384 	.llseek = seq_lseek,
385 	.release = single_release,
386 };
387 #endif
388 
389 static int
390 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
391 {
392 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
393 	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
394 	struct amdgpu_device *adev = uq_mgr->adev;
395 	const struct amdgpu_userq_funcs *uq_funcs;
396 	struct amdgpu_usermode_queue *queue;
397 	struct amdgpu_db_info db_info;
398 	char *queue_name;
399 	bool skip_map_queue;
400 	uint64_t index;
401 	int qid, r = 0;
402 	int priority =
403 		(args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>
404 		AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
405 
406 	/* Usermode queues are only supported for GFX IP as of now */
407 	if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
408 	    args->in.ip_type != AMDGPU_HW_IP_DMA &&
409 	    args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
410 		drm_file_err(uq_mgr->file, "Usermode queue doesn't support IP type %u\n",
411 			     args->in.ip_type);
412 		return -EINVAL;
413 	}
414 
415 	r = amdgpu_userq_priority_permit(filp, priority);
416 	if (r)
417 		return r;
418 
419 	if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
420 	    (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
421 	    (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
422 	    !amdgpu_is_tmz(adev)) {
423 		drm_file_err(uq_mgr->file, "Secure only supported on GFX/Compute queues\n");
424 		return -EINVAL;
425 	}
426 
427 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
428 	if (r < 0) {
429 		drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n");
430 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
431 		return r;
432 	}
433 
434 	/*
435 	 * There could be a situation that we are creating a new queue while
436 	 * the other queues under this UQ_mgr are suspended. So if there is any
437 	 * resume work pending, wait for it to get done.
438 	 *
439 	 * This will also make sure we have a valid eviction fence ready to be used.
440 	 */
441 	mutex_lock(&adev->userq_mutex);
442 	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
443 
444 	uq_funcs = adev->userq_funcs[args->in.ip_type];
445 	if (!uq_funcs) {
446 		drm_file_err(uq_mgr->file, "Usermode queue is not supported for this IP (%u)\n",
447 			     args->in.ip_type);
448 		r = -EINVAL;
449 		goto unlock;
450 	}
451 
452 	queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
453 	if (!queue) {
454 		drm_file_err(uq_mgr->file, "Failed to allocate memory for queue\n");
455 		r = -ENOMEM;
456 		goto unlock;
457 	}
458 	queue->doorbell_handle = args->in.doorbell_handle;
459 	queue->queue_type = args->in.ip_type;
460 	queue->vm = &fpriv->vm;
461 	queue->priority = priority;
462 
463 	db_info.queue_type = queue->queue_type;
464 	db_info.doorbell_handle = queue->doorbell_handle;
465 	db_info.db_obj = &queue->db_obj;
466 	db_info.doorbell_offset = args->in.doorbell_offset;
467 
468 	/* Convert relative doorbell offset into absolute doorbell index */
469 	index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
470 	if (index == (uint64_t)-EINVAL) {
471 		drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n");
472 		kfree(queue);
473 		goto unlock;
474 	}
475 
476 	queue->doorbell_index = index;
477 	xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC);
478 	r = amdgpu_userq_fence_driver_alloc(adev, queue);
479 	if (r) {
480 		drm_file_err(uq_mgr->file, "Failed to alloc fence driver\n");
481 		goto unlock;
482 	}
483 
484 	r = uq_funcs->mqd_create(uq_mgr, &args->in, queue);
485 	if (r) {
486 		drm_file_err(uq_mgr->file, "Failed to create Queue\n");
487 		amdgpu_userq_fence_driver_free(queue);
488 		kfree(queue);
489 		goto unlock;
490 	}
491 
492 
493 	qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);
494 	if (qid < 0) {
495 		drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n");
496 		amdgpu_userq_fence_driver_free(queue);
497 		uq_funcs->mqd_destroy(uq_mgr, queue);
498 		kfree(queue);
499 		r = -ENOMEM;
500 		goto unlock;
501 	}
502 
503 	/* don't map the queue if scheduling is halted */
504 	if (adev->userq_halt_for_enforce_isolation &&
505 	    ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
506 	     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)))
507 		skip_map_queue = true;
508 	else
509 		skip_map_queue = false;
510 	if (!skip_map_queue) {
511 		r = amdgpu_userq_map_helper(uq_mgr, queue);
512 		if (r) {
513 			drm_file_err(uq_mgr->file, "Failed to map Queue\n");
514 			idr_remove(&uq_mgr->userq_idr, qid);
515 			amdgpu_userq_fence_driver_free(queue);
516 			uq_funcs->mqd_destroy(uq_mgr, queue);
517 			kfree(queue);
518 			goto unlock;
519 		}
520 	}
521 
522 	queue_name = kasprintf(GFP_KERNEL, "queue-%d", qid);
523 	if (!queue_name)
524 		return -ENOMEM;
525 
526 	/* Queue dentry per client to hold MQD information   */
527 	queue->debugfs_queue = debugfs_create_dir(queue_name, filp->debugfs_client);
528 	debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops);
529 	kfree(queue_name);
530 
531 	args->out.queue_id = qid;
532 
533 unlock:
534 	mutex_unlock(&uq_mgr->userq_mutex);
535 	mutex_unlock(&adev->userq_mutex);
536 
537 	return r;
538 }
539 
540 int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
541 		       struct drm_file *filp)
542 {
543 	union drm_amdgpu_userq *args = data;
544 	int r;
545 
546 	switch (args->in.op) {
547 	case AMDGPU_USERQ_OP_CREATE:
548 		if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
549 				       AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))
550 			return -EINVAL;
551 		r = amdgpu_userq_create(filp, args);
552 		if (r)
553 			drm_file_err(filp, "Failed to create usermode queue\n");
554 		break;
555 
556 	case AMDGPU_USERQ_OP_FREE:
557 		if (args->in.ip_type ||
558 		    args->in.doorbell_handle ||
559 		    args->in.doorbell_offset ||
560 		    args->in.flags ||
561 		    args->in.queue_va ||
562 		    args->in.queue_size ||
563 		    args->in.rptr_va ||
564 		    args->in.wptr_va ||
565 		    args->in.wptr_va ||
566 		    args->in.mqd ||
567 		    args->in.mqd_size)
568 			return -EINVAL;
569 		r = amdgpu_userq_destroy(filp, args->in.queue_id);
570 		if (r)
571 			drm_file_err(filp, "Failed to destroy usermode queue\n");
572 		break;
573 
574 	default:
575 		drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
576 		return -EINVAL;
577 	}
578 
579 	return r;
580 }
581 
582 static int
583 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
584 {
585 	struct amdgpu_usermode_queue *queue;
586 	int queue_id;
587 	int ret = 0, r;
588 
589 	/* Resume all the queues for this process */
590 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
591 		r = amdgpu_userq_map_helper(uq_mgr, queue);
592 		if (r)
593 			ret = r;
594 	}
595 
596 	if (ret)
597 		drm_file_err(uq_mgr->file, "Failed to map all the queues\n");
598 	return ret;
599 }
600 
601 static int
602 amdgpu_userq_validate_vm_bo(void *_unused, struct amdgpu_bo *bo)
603 {
604 	struct ttm_operation_ctx ctx = { false, false };
605 	int ret;
606 
607 	amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
608 
609 	ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
610 	if (ret)
611 		DRM_ERROR("Fail to validate\n");
612 
613 	return ret;
614 }
615 
616 static int
617 amdgpu_userq_validate_bos(struct amdgpu_userq_mgr *uq_mgr)
618 {
619 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
620 	struct amdgpu_vm *vm = &fpriv->vm;
621 	struct amdgpu_device *adev = uq_mgr->adev;
622 	struct amdgpu_bo_va *bo_va;
623 	struct ww_acquire_ctx *ticket;
624 	struct drm_exec exec;
625 	struct amdgpu_bo *bo;
626 	struct dma_resv *resv;
627 	bool clear, unlock;
628 	int ret = 0;
629 
630 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
631 	drm_exec_until_all_locked(&exec) {
632 		ret = amdgpu_vm_lock_pd(vm, &exec, 2);
633 		drm_exec_retry_on_contention(&exec);
634 		if (unlikely(ret)) {
635 			drm_file_err(uq_mgr->file, "Failed to lock PD\n");
636 			goto unlock_all;
637 		}
638 
639 		/* Lock the done list */
640 		list_for_each_entry(bo_va, &vm->done, base.vm_status) {
641 			bo = bo_va->base.bo;
642 			if (!bo)
643 				continue;
644 
645 			ret = drm_exec_lock_obj(&exec, &bo->tbo.base);
646 			drm_exec_retry_on_contention(&exec);
647 			if (unlikely(ret))
648 				goto unlock_all;
649 		}
650 	}
651 
652 	spin_lock(&vm->status_lock);
653 	while (!list_empty(&vm->moved)) {
654 		bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
655 					 base.vm_status);
656 		spin_unlock(&vm->status_lock);
657 
658 		/* Per VM BOs never need to bo cleared in the page tables */
659 		ret = amdgpu_vm_bo_update(adev, bo_va, false);
660 		if (ret)
661 			goto unlock_all;
662 		spin_lock(&vm->status_lock);
663 	}
664 
665 	ticket = &exec.ticket;
666 	while (!list_empty(&vm->invalidated)) {
667 		bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
668 					 base.vm_status);
669 		resv = bo_va->base.bo->tbo.base.resv;
670 		spin_unlock(&vm->status_lock);
671 
672 		bo = bo_va->base.bo;
673 		ret = amdgpu_userq_validate_vm_bo(NULL, bo);
674 		if (ret) {
675 			drm_file_err(uq_mgr->file, "Failed to validate BO\n");
676 			goto unlock_all;
677 		}
678 
679 		/* Try to reserve the BO to avoid clearing its ptes */
680 		if (!adev->debug_vm && dma_resv_trylock(resv)) {
681 			clear = false;
682 			unlock = true;
683 		/* The caller is already holding the reservation lock */
684 		} else if (dma_resv_locking_ctx(resv) == ticket) {
685 			clear = false;
686 			unlock = false;
687 		/* Somebody else is using the BO right now */
688 		} else {
689 			clear = true;
690 			unlock = false;
691 		}
692 
693 		ret = amdgpu_vm_bo_update(adev, bo_va, clear);
694 
695 		if (unlock)
696 			dma_resv_unlock(resv);
697 		if (ret)
698 			goto unlock_all;
699 
700 		spin_lock(&vm->status_lock);
701 	}
702 	spin_unlock(&vm->status_lock);
703 
704 	ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec);
705 	if (ret)
706 		drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n");
707 
708 unlock_all:
709 	drm_exec_fini(&exec);
710 	return ret;
711 }
712 
713 static void amdgpu_userq_restore_worker(struct work_struct *work)
714 {
715 	struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
716 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
717 	int ret;
718 
719 	flush_work(&fpriv->evf_mgr.suspend_work.work);
720 
721 	mutex_lock(&uq_mgr->userq_mutex);
722 
723 	ret = amdgpu_userq_validate_bos(uq_mgr);
724 	if (ret) {
725 		drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n");
726 		goto unlock;
727 	}
728 
729 	ret = amdgpu_userq_restore_all(uq_mgr);
730 	if (ret) {
731 		drm_file_err(uq_mgr->file, "Failed to restore all queues\n");
732 		goto unlock;
733 	}
734 
735 unlock:
736 	mutex_unlock(&uq_mgr->userq_mutex);
737 }
738 
739 static int
740 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
741 {
742 	struct amdgpu_usermode_queue *queue;
743 	int queue_id;
744 	int ret = 0, r;
745 
746 	/* Try to unmap all the queues in this process ctx */
747 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
748 		r = amdgpu_userq_unmap_helper(uq_mgr, queue);
749 		if (r)
750 			ret = r;
751 	}
752 
753 	if (ret)
754 		drm_file_err(uq_mgr->file, "Couldn't unmap all the queues\n");
755 	return ret;
756 }
757 
758 static int
759 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr)
760 {
761 	struct amdgpu_usermode_queue *queue;
762 	int queue_id, ret;
763 
764 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
765 		struct dma_fence *f = queue->last_fence;
766 
767 		if (!f || dma_fence_is_signaled(f))
768 			continue;
769 		ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
770 		if (ret <= 0) {
771 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
772 				     f->context, f->seqno);
773 			return -ETIMEDOUT;
774 		}
775 	}
776 
777 	return 0;
778 }
779 
780 void
781 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
782 		   struct amdgpu_eviction_fence *ev_fence)
783 {
784 	int ret;
785 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
786 	struct amdgpu_eviction_fence_mgr *evf_mgr = &fpriv->evf_mgr;
787 
788 	/* Wait for any pending userqueue fence work to finish */
789 	ret = amdgpu_userq_wait_for_signal(uq_mgr);
790 	if (ret) {
791 		drm_file_err(uq_mgr->file, "Not evicting userqueue, timeout waiting for work\n");
792 		return;
793 	}
794 
795 	ret = amdgpu_userq_evict_all(uq_mgr);
796 	if (ret) {
797 		drm_file_err(uq_mgr->file, "Failed to evict userqueue\n");
798 		return;
799 	}
800 
801 	/* Signal current eviction fence */
802 	amdgpu_eviction_fence_signal(evf_mgr, ev_fence);
803 
804 	if (evf_mgr->fd_closing) {
805 		cancel_delayed_work_sync(&uq_mgr->resume_work);
806 		return;
807 	}
808 
809 	/* Schedule a resume work */
810 	schedule_delayed_work(&uq_mgr->resume_work, 0);
811 }
812 
813 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
814 			  struct amdgpu_device *adev)
815 {
816 	mutex_init(&userq_mgr->userq_mutex);
817 	idr_init_base(&userq_mgr->userq_idr, 1);
818 	userq_mgr->adev = adev;
819 	userq_mgr->file = file_priv;
820 
821 	mutex_lock(&adev->userq_mutex);
822 	list_add(&userq_mgr->list, &adev->userq_mgr_list);
823 	mutex_unlock(&adev->userq_mutex);
824 
825 	INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
826 	return 0;
827 }
828 
829 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
830 {
831 	struct amdgpu_device *adev = userq_mgr->adev;
832 	struct amdgpu_usermode_queue *queue;
833 	struct amdgpu_userq_mgr *uqm, *tmp;
834 	uint32_t queue_id;
835 
836 	cancel_delayed_work_sync(&userq_mgr->resume_work);
837 
838 	mutex_lock(&adev->userq_mutex);
839 	mutex_lock(&userq_mgr->userq_mutex);
840 	idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
841 		amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
842 		amdgpu_userq_unmap_helper(userq_mgr, queue);
843 		amdgpu_userq_cleanup(userq_mgr, queue, queue_id);
844 	}
845 
846 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
847 		if (uqm == userq_mgr) {
848 			list_del(&uqm->list);
849 			break;
850 		}
851 	}
852 	idr_destroy(&userq_mgr->userq_idr);
853 	mutex_unlock(&userq_mgr->userq_mutex);
854 	mutex_unlock(&adev->userq_mutex);
855 	mutex_destroy(&userq_mgr->userq_mutex);
856 }
857 
858 int amdgpu_userq_suspend(struct amdgpu_device *adev)
859 {
860 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
861 	struct amdgpu_usermode_queue *queue;
862 	struct amdgpu_userq_mgr *uqm, *tmp;
863 	int queue_id;
864 	int ret = 0, r;
865 
866 	if (!ip_mask)
867 		return 0;
868 
869 	mutex_lock(&adev->userq_mutex);
870 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
871 		cancel_delayed_work_sync(&uqm->resume_work);
872 		mutex_lock(&uqm->userq_mutex);
873 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
874 			r = amdgpu_userq_unmap_helper(uqm, queue);
875 			if (r)
876 				ret = r;
877 		}
878 		mutex_unlock(&uqm->userq_mutex);
879 	}
880 	mutex_unlock(&adev->userq_mutex);
881 	return ret;
882 }
883 
884 int amdgpu_userq_resume(struct amdgpu_device *adev)
885 {
886 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
887 	struct amdgpu_usermode_queue *queue;
888 	struct amdgpu_userq_mgr *uqm, *tmp;
889 	int queue_id;
890 	int ret = 0, r;
891 
892 	if (!ip_mask)
893 		return 0;
894 
895 	mutex_lock(&adev->userq_mutex);
896 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
897 		mutex_lock(&uqm->userq_mutex);
898 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
899 			r = amdgpu_userq_map_helper(uqm, queue);
900 			if (r)
901 				ret = r;
902 		}
903 		mutex_unlock(&uqm->userq_mutex);
904 	}
905 	mutex_unlock(&adev->userq_mutex);
906 	return ret;
907 }
908 
909 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
910 						  u32 idx)
911 {
912 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
913 	struct amdgpu_usermode_queue *queue;
914 	struct amdgpu_userq_mgr *uqm, *tmp;
915 	int queue_id;
916 	int ret = 0, r;
917 
918 	/* only need to stop gfx/compute */
919 	if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
920 		return 0;
921 
922 	mutex_lock(&adev->userq_mutex);
923 	if (adev->userq_halt_for_enforce_isolation)
924 		dev_warn(adev->dev, "userq scheduling already stopped!\n");
925 	adev->userq_halt_for_enforce_isolation = true;
926 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
927 		cancel_delayed_work_sync(&uqm->resume_work);
928 		mutex_lock(&uqm->userq_mutex);
929 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
930 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
931 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
932 			    (queue->xcp_id == idx)) {
933 				r = amdgpu_userq_unmap_helper(uqm, queue);
934 				if (r)
935 					ret = r;
936 			}
937 		}
938 		mutex_unlock(&uqm->userq_mutex);
939 	}
940 	mutex_unlock(&adev->userq_mutex);
941 	return ret;
942 }
943 
944 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
945 						   u32 idx)
946 {
947 	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
948 	struct amdgpu_usermode_queue *queue;
949 	struct amdgpu_userq_mgr *uqm, *tmp;
950 	int queue_id;
951 	int ret = 0, r;
952 
953 	/* only need to stop gfx/compute */
954 	if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE))))
955 		return 0;
956 
957 	mutex_lock(&adev->userq_mutex);
958 	if (!adev->userq_halt_for_enforce_isolation)
959 		dev_warn(adev->dev, "userq scheduling already started!\n");
960 	adev->userq_halt_for_enforce_isolation = false;
961 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
962 		mutex_lock(&uqm->userq_mutex);
963 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
964 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
965 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
966 			    (queue->xcp_id == idx)) {
967 				r = amdgpu_userq_map_helper(uqm, queue);
968 				if (r)
969 					ret = r;
970 			}
971 		}
972 		mutex_unlock(&uqm->userq_mutex);
973 	}
974 	mutex_unlock(&adev->userq_mutex);
975 	return ret;
976 }
977