xref: /linux/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2024 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 #include <drm/drm_drv.h>
25 #include "amdgpu.h"
26 #include "amdgpu_gfx.h"
27 #include "mes_userqueue.h"
28 #include "amdgpu_userq_fence.h"
29 #include "amdgpu_trace.h"
30 
31 #define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE
32 #define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE
33 
34 static int
35 mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
36 			      struct amdgpu_userq_mgr *uq_mgr,
37 			      struct amdgpu_usermode_queue *queue,
38 			      uint64_t wptr)
39 {
40 	struct amdgpu_bo_va_mapping *wptr_mapping;
41 	struct amdgpu_userq_obj *wptr_obj = &queue->wptr_obj;
42 	struct amdgpu_bo *obj;
43 	struct amdgpu_vm *vm = queue->vm;
44 	struct drm_exec exec;
45 	int ret;
46 
47 	wptr &= AMDGPU_GMC_HOLE_MASK;
48 
49 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 2);
50 	drm_exec_until_all_locked(&exec) {
51 		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
52 		drm_exec_retry_on_contention(&exec);
53 		if (unlikely(ret))
54 			goto fail_lock;
55 
56 		wptr_mapping = amdgpu_vm_bo_lookup_mapping(vm, wptr);
57 		if (!wptr_mapping) {
58 			ret = -EINVAL;
59 			goto fail_lock;
60 		}
61 
62 		obj = wptr_mapping->bo_va->base.bo;
63 		ret = drm_exec_prepare_obj(&exec, &obj->tbo.base,
64 					   TTM_NUM_MOVE_FENCES + 1);
65 		drm_exec_retry_on_contention(&exec);
66 		if (unlikely(ret))
67 			goto fail_lock;
68 	}
69 
70 	wptr_obj->obj = amdgpu_bo_ref(wptr_mapping->bo_va->base.bo);
71 	if (wptr_obj->obj->tbo.base.size > PAGE_SIZE) {
72 		ret = -EINVAL;
73 		goto fail_map;
74 	}
75 	/* Keep WPTR BO under eviction-fence control instead of pinning. */
76 	ret = amdgpu_evf_mgr_attach_fence(&uq_mgr_to_fpriv(uq_mgr)->evf_mgr, wptr_obj->obj);
77 	if (ret) {
78 		DRM_ERROR("Failed to attach eviction fence to wptr bo. ret %d\n", ret);
79 		goto fail_map;
80 	}
81 
82 	ret = amdgpu_ttm_alloc_gart(&wptr_obj->obj->tbo);
83 	if (ret) {
84 		DRM_ERROR("Failed to bind wptr bo to GART. ret %d\n", ret);
85 		goto fail_map;
86 	}
87 
88 	queue->wptr_obj.gpu_addr = amdgpu_bo_gpu_offset(wptr_obj->obj);
89 
90 	drm_exec_fini(&exec);
91 	return 0;
92 fail_map:
93 	amdgpu_bo_unref(&wptr_obj->obj);
94 fail_lock:
95 	drm_exec_fini(&exec);
96 	return ret;
97 
98 }
99 
100 static int convert_to_mes_priority(int priority)
101 {
102 	switch (priority) {
103 	case AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_NORMAL_LOW:
104 	default:
105 		return AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
106 	case AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_LOW:
107 		return AMDGPU_MES_PRIORITY_LEVEL_LOW;
108 	case AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_NORMAL_HIGH:
109 		return AMDGPU_MES_PRIORITY_LEVEL_MEDIUM;
110 	case AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH:
111 		return AMDGPU_MES_PRIORITY_LEVEL_HIGH;
112 	}
113 }
114 
115 static int mes_userq_map(struct amdgpu_usermode_queue *queue)
116 {
117 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
118 	struct amdgpu_device *adev = uq_mgr->adev;
119 	struct amdgpu_userq_obj *ctx = &queue->fw_obj;
120 	struct amdgpu_mqd_prop *userq_props = queue->userq_prop;
121 	struct mes_add_queue_input queue_input;
122 	struct amdgpu_mes *mes = &adev->mes;
123 	int r;
124 
125 	memset(&queue_input, 0x0, sizeof(struct mes_add_queue_input));
126 
127 	queue_input.process_va_start = 0;
128 	queue_input.process_va_end = adev->vm_manager.max_pfn - 1;
129 
130 	/* set process quantum to 10 ms and gang quantum to 1 ms as default */
131 	queue_input.process_quantum = 100000;
132 	queue_input.gang_quantum = 10000;
133 	queue_input.paging = false;
134 
135 	queue_input.process_context_addr = uq_mgr->proc_ctx_obj.gpu_addr;
136 	queue_input.gang_context_addr = ctx->gpu_addr;
137 	queue_input.inprocess_gang_priority = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
138 	queue_input.gang_global_priority_level = convert_to_mes_priority(queue->priority);
139 
140 	queue_input.process_id = queue->vm->pasid;
141 	queue_input.queue_type = queue->queue_type;
142 	queue_input.mqd_addr = queue->mqd.gpu_addr;
143 	queue_input.wptr_addr = userq_props->wptr_gpu_addr;
144 	queue_input.queue_size = userq_props->queue_size >> 2;
145 	queue_input.doorbell_offset = userq_props->doorbell_index;
146 	queue_input.page_table_base_addr = amdgpu_gmc_pd_addr(queue->vm->root.bo);
147 	queue_input.wptr_mc_addr = queue->wptr_obj.gpu_addr;
148 
149 	if (mes->use_rs64mem) {
150 		if (!uq_mgr->proc_ctx_allocated) {
151 			r = amdgpu_mes_alloc_proc_ctx_index(mes, &uq_mgr->proc_ctx_array_index);
152 			if (r) {
153 				DRM_ERROR("Failed to allocate userq process index err:%d\n", r);
154 				return r;
155 			}
156 			uq_mgr->proc_ctx_allocated = true;
157 		}
158 
159 		r = amdgpu_mes_alloc_gang_ctx_index(mes, &queue->gang_ctx_array_index);
160 		if (r) {
161 			DRM_ERROR("Failed to allocate userq gang index err:%d\n", r);
162 			return r;
163 		}
164 		queue_input.process_context_array_index = uq_mgr->proc_ctx_array_index;
165 		queue_input.gang_context_array_index = queue->gang_ctx_array_index;
166 	}
167 	amdgpu_mes_lock(&adev->mes);
168 	r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
169 	amdgpu_mes_unlock(&adev->mes);
170 	if (r) {
171 		DRM_ERROR("Failed to map queue in HW, err (%d)\n", r);
172 		return r;
173 	}
174 
175 	amdgpu_mes_userq_queue_mapped(adev);
176 
177 	DRM_DEBUG_DRIVER("Queue (doorbell:%d) mapped successfully\n", userq_props->doorbell_index);
178 	return 0;
179 }
180 
181 static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
182 {
183 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
184 	struct amdgpu_device *adev = uq_mgr->adev;
185 	struct mes_remove_queue_input queue_input;
186 	struct amdgpu_userq_obj *ctx = &queue->fw_obj;
187 	struct amdgpu_mes *mes = &adev->mes;
188 	int r;
189 
190 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
191 	queue_input.gang_context_array_index = queue->gang_ctx_array_index;
192 	queue_input.doorbell_offset = queue->doorbell_index;
193 	queue_input.gang_context_addr = ctx->gpu_addr;
194 	queue_input.queue_type = queue->queue_type;
195 
196 	amdgpu_mes_lock(&adev->mes);
197 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
198 	amdgpu_mes_unlock(&adev->mes);
199 	if (mes->use_rs64mem)
200 		amdgpu_mes_free_gang_ctx_index(mes, queue->gang_ctx_array_index);
201 	if (r) {
202 		DRM_ERROR("Failed to unmap queue in HW, err (%d)\n", r);
203 		return r;
204 	}
205 
206 	amdgpu_mes_userq_queue_unmapped(adev);
207 	return 0;
208 }
209 
210 int mes_userq_reset(struct amdgpu_usermode_queue *queue)
211 {
212 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
213 	struct amdgpu_device *adev = uq_mgr->adev;
214 	struct mes_reset_queue_input queue_input;
215 	int r;
216 
217 	/* already reset by an earlier job's hang-detect; just signal and bail */
218 	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
219 		return 0;
220 
221 	memset(&queue_input, 0x0, sizeof(struct mes_reset_queue_input));
222 	queue_input.doorbell_offset = queue->doorbell_index;
223 	queue_input.queue_type = queue->queue_type;
224 
225 	/*
226 	 * The MES packet reset fails once the hung queue wedges MES. For SDMA the
227 	 * queue is still on its HW slot, so reset it over MMIO instead: recover
228 	 * the (instance, queue_id) slot from the doorbell.
229 	 */
230 	if (queue->queue_type == AMDGPU_HW_IP_DMA &&
231 	    adev->sdma.instance[0].funcs &&
232 	    adev->sdma.instance[0].funcs->detect_hung_queue) {
233 		u32 instance, hw_queue_id;
234 
235 		if (adev->sdma.instance[0].funcs->detect_hung_queue(adev,
236 				queue->doorbell_index, &instance, &hw_queue_id)) {
237 			queue_input.use_mmio = true;
238 			queue_input.me_id = instance;
239 			queue_input.queue_id = hw_queue_id;
240 		} else {
241 			dev_warn(adev->dev,
242 				 "SDMA userq (doorbell %llu) not on any HW slot; falling back to MES reset\n",
243 				 queue->doorbell_index);
244 		}
245 	}
246 
247 	amdgpu_mes_lock(&adev->mes);
248 	r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
249 	amdgpu_mes_unlock(&adev->mes);
250 	if (r)
251 		return r;
252 
253 	/* drop the queue from MES */
254 	r = mes_userq_unmap(queue);
255 	if (r)
256 		return r;
257 
258 	/*
259 	 * HUNG, not UNMAPPED: the guilty job is still in the ring, so the
260 	 * restore worker must not re-map and re-run it.
261 	 */
262 	trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG);
263 	queue->state = AMDGPU_USERQ_STATE_HUNG;
264 
265 	return 0;
266 }
267 
268 int mes_userq_reset_queue(struct amdgpu_device *adev,
269 			  struct amdgpu_usermode_queue *guilty_uq,
270 			  int queue_type,
271 			  unsigned int pipe,
272 			  unsigned int queue,
273 			  unsigned int db)
274 {
275 	struct amdgpu_usermode_queue *uq;
276 	bool use_mmio = adev->gfx.mec.use_mmio_for_reset;
277 	unsigned long uq_id;
278 	int r;
279 
280 	xa_for_each(&adev->userq_doorbell_xa, uq_id, uq) {
281 		if (uq->queue_type == queue_type) {
282 			if (uq == guilty_uq)
283 				continue;
284 			if (uq->doorbell_index == db) {
285 				uq->state = AMDGPU_USERQ_STATE_HUNG;
286 				if (use_mmio)
287 					r = amdgpu_mes_reset_queue_mmio(adev, queue_type, 0, 1, pipe, queue, 0);
288 				else
289 					r = amdgpu_mes_reset_user_queue(adev, queue_type, db, 0);
290 				if (r)
291 					return r;
292 				r = mes_userq_unmap(uq);
293 				if (r)
294 					return r;
295 				amdgpu_userq_fence_driver_force_completion(uq);
296 				break;
297 			}
298 		}
299 	}
300 	return 0;
301 }
302 
303 static int mes_userq_create_ctx_space(struct amdgpu_userq_mgr *uq_mgr,
304 				      struct amdgpu_usermode_queue *queue,
305 				      struct drm_amdgpu_userq_in *mqd_user)
306 {
307 	struct amdgpu_userq_obj *ctx = &queue->fw_obj;
308 	int r, size;
309 
310 	/* The FW expects at least one page space allocated for gang ctx. */
311 	size = AMDGPU_USERQ_GANG_CTX_SZ;
312 	r = amdgpu_bo_create_kernel(uq_mgr->adev, size, 0,
313 				    AMDGPU_GEM_DOMAIN_GTT,
314 				    &ctx->obj, &ctx->gpu_addr,
315 				    &ctx->cpu_ptr);
316 	if (r) {
317 		DRM_ERROR("Failed to allocate ctx space bo for userqueue, err:%d\n", r);
318 		return r;
319 	}
320 
321 	memset(ctx->cpu_ptr, 0, size);
322 	return 0;
323 }
324 
325 static int mes_userq_create_proc_ctx_space(struct amdgpu_userq_mgr *uq_mgr)
326 {
327 	int r = 0;
328 
329 	mutex_lock(&uq_mgr->proc_ctx_lock);
330 	/* This check is a necessary because amdgpu_bo_create_kernel()
331 	 * calls helpers like amdgpu_bo_pin() and memset() unconditionally
332 	 */
333 	if (!uq_mgr->proc_ctx_obj.obj) {
334 		r = amdgpu_bo_create_kernel(uq_mgr->adev, AMDGPU_USERQ_PROC_CTX_SZ,
335 					    0, AMDGPU_GEM_DOMAIN_GTT,
336 					    &uq_mgr->proc_ctx_obj.obj,
337 					    &uq_mgr->proc_ctx_obj.gpu_addr,
338 					    &uq_mgr->proc_ctx_obj.cpu_ptr);
339 
340 		if (!r)
341 			memset(uq_mgr->proc_ctx_obj.cpu_ptr, 0, AMDGPU_USERQ_PROC_CTX_SZ);
342 	}
343 
344 	mutex_unlock(&uq_mgr->proc_ctx_lock);
345 
346 	return r;
347 }
348 
349 static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
350 				struct drm_amdgpu_userq_in *args_in)
351 {
352 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
353 	struct amdgpu_device *adev = uq_mgr->adev;
354 	struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
355 	struct drm_amdgpu_userq_in *mqd_user = args_in;
356 	struct amdgpu_mqd_prop *userq_props;
357 	int r;
358 
359 	/* Structure to initialize MQD for userqueue using generic MQD init function */
360 	userq_props = kzalloc_obj(struct amdgpu_mqd_prop);
361 	if (!userq_props) {
362 		DRM_ERROR("Failed to allocate memory for userq_props\n");
363 		return -ENOMEM;
364 	}
365 
366 	r = amdgpu_bo_create_kernel(adev,
367 				    AMDGPU_MQD_SIZE_ALIGN(mqd_hw_default->mqd_size),
368 				    0, AMDGPU_GEM_DOMAIN_GTT,
369 				    &queue->mqd.obj, &queue->mqd.gpu_addr,
370 				    &queue->mqd.cpu_ptr);
371 	if (r) {
372 		DRM_ERROR("Failed to create MQD object for userqueue\n");
373 		goto free_props;
374 	}
375 
376 	memset(queue->mqd.cpu_ptr, 0,
377 	       AMDGPU_MQD_SIZE_ALIGN(mqd_hw_default->mqd_size));
378 
379 	/* Initialize the MQD BO with user given values */
380 	userq_props->wptr_gpu_addr = mqd_user->wptr_va;
381 	userq_props->rptr_gpu_addr = mqd_user->rptr_va;
382 	userq_props->queue_size = mqd_user->queue_size;
383 	userq_props->hqd_base_gpu_addr = mqd_user->queue_va;
384 	userq_props->mqd_gpu_addr = queue->mqd.gpu_addr;
385 	userq_props->use_doorbell = true;
386 	userq_props->doorbell_index = queue->doorbell_index;
387 	userq_props->fence_address = queue->fence_drv->gpu_addr;
388 
389 	if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
390 		struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
391 
392 		if (mqd_user->mqd_size != sizeof(*compute_mqd)) {
393 			DRM_ERROR("Invalid compute IP MQD size\n");
394 			r = -EINVAL;
395 			goto free_mqd;
396 		}
397 
398 		compute_mqd = memdup_user(u64_to_user_ptr(mqd_user->mqd), mqd_user->mqd_size);
399 		if (IS_ERR(compute_mqd)) {
400 			DRM_ERROR("Failed to read user MQD\n");
401 			r = -ENOMEM;
402 			goto free_mqd;
403 		}
404 
405 		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
406 		if (r) {
407 			kfree(compute_mqd);
408 			goto free_mqd;
409 		}
410 		r = amdgpu_userq_input_va_validate(adev, queue,
411 						   compute_mqd->eop_va, 2048,
412 						   &queue->userq_vas.va.eop);
413 		amdgpu_bo_unreserve(queue->vm->root.bo);
414 		if (r) {
415 			kfree(compute_mqd);
416 			goto free_mqd;
417 		}
418 
419 		userq_props->eop_gpu_addr = compute_mqd->eop_va;
420 		userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
421 		userq_props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
422 		userq_props->hqd_active = false;
423 		userq_props->tmz_queue =
424 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
425 		kfree(compute_mqd);
426 	} else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
427 		struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11;
428 		struct amdgpu_gfx_shadow_info shadow_info;
429 
430 		if (adev->gfx.funcs->get_gfx_shadow_info) {
431 			adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
432 		} else {
433 			r = -EINVAL;
434 			goto free_mqd;
435 		}
436 
437 		if (mqd_user->mqd_size != sizeof(*mqd_gfx_v11) || !mqd_user->mqd) {
438 			DRM_ERROR("Invalid GFX MQD\n");
439 			r = -EINVAL;
440 			goto free_mqd;
441 		}
442 
443 		mqd_gfx_v11 = memdup_user(u64_to_user_ptr(mqd_user->mqd), mqd_user->mqd_size);
444 		if (IS_ERR(mqd_gfx_v11)) {
445 			DRM_ERROR("Failed to read user MQD\n");
446 			r = -ENOMEM;
447 			goto free_mqd;
448 		}
449 
450 		userq_props->shadow_addr = mqd_gfx_v11->shadow_va;
451 		userq_props->csa_addr = mqd_gfx_v11->csa_va;
452 		userq_props->tmz_queue =
453 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
454 
455 		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
456 		if (r) {
457 			kfree(mqd_gfx_v11);
458 			goto free_mqd;
459 		}
460 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->shadow_va,
461 						   shadow_info.shadow_size,
462 						   &queue->userq_vas.va.shadow);
463 		if (r) {
464 			amdgpu_bo_unreserve(queue->vm->root.bo);
465 			kfree(mqd_gfx_v11);
466 			goto free_mqd;
467 		}
468 
469 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->csa_va,
470 						   shadow_info.csa_size,
471 						   &queue->userq_vas.va.csa);
472 		amdgpu_bo_unreserve(queue->vm->root.bo);
473 		if (r) {
474 			kfree(mqd_gfx_v11);
475 			goto free_mqd;
476 		}
477 
478 		kfree(mqd_gfx_v11);
479 	} else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
480 		struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;
481 
482 		if (mqd_user->mqd_size != sizeof(*mqd_sdma_v11) || !mqd_user->mqd) {
483 			DRM_ERROR("Invalid SDMA MQD\n");
484 			r = -EINVAL;
485 			goto free_mqd;
486 		}
487 
488 		mqd_sdma_v11 = memdup_user(u64_to_user_ptr(mqd_user->mqd), mqd_user->mqd_size);
489 		if (IS_ERR(mqd_sdma_v11)) {
490 			DRM_ERROR("Failed to read sdma user MQD\n");
491 			r = -ENOMEM;
492 			goto free_mqd;
493 		}
494 
495 		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
496 		if (r) {
497 			kfree(mqd_sdma_v11);
498 			goto free_mqd;
499 		}
500 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_sdma_v11->csa_va,
501 						   32,
502 						   &queue->userq_vas.va.csa);
503 		amdgpu_bo_unreserve(queue->vm->root.bo);
504 		if (r) {
505 			kfree(mqd_sdma_v11);
506 			goto free_mqd;
507 		}
508 
509 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
510 		kfree(mqd_sdma_v11);
511 	}
512 
513 	queue->userq_prop = userq_props;
514 
515 	r = mqd_hw_default->init_mqd(adev, (void *)queue->mqd.cpu_ptr, userq_props);
516 	if (r) {
517 		DRM_ERROR("Failed to initialize MQD for userqueue\n");
518 		goto free_mqd;
519 	}
520 
521 	/* Create per-process MES process context BO */
522 	r = mes_userq_create_proc_ctx_space(uq_mgr);
523 	if (r) {
524 		DRM_ERROR("Failed to allocate MES process context space bo, error: %d\n", r);
525 		goto free_mqd;
526 	}
527 
528 	/* Create BO of a gang for FW operations */
529 	r = mes_userq_create_ctx_space(uq_mgr, queue, mqd_user);
530 	if (r) {
531 		DRM_ERROR("Failed to allocate BO for userqueue (%d)", r);
532 		goto free_mqd;
533 	}
534 
535 	/* FW expects WPTR BOs to be mapped into GART */
536 	r = mes_userq_create_wptr_mapping(adev, uq_mgr, queue, userq_props->wptr_gpu_addr);
537 	if (r) {
538 		DRM_ERROR("Failed to create WPTR mapping\n");
539 		goto free_ctx;
540 	}
541 
542 	return 0;
543 
544 free_ctx:
545 	amdgpu_bo_free_kernel(&queue->fw_obj.obj, &queue->fw_obj.gpu_addr,
546 			      &queue->fw_obj.cpu_ptr);
547 
548 free_mqd:
549 	amdgpu_bo_free_kernel(&queue->mqd.obj, &queue->mqd.gpu_addr,
550 			      &queue->mqd.cpu_ptr);
551 
552 free_props:
553 	kfree(userq_props);
554 
555 	return r;
556 }
557 
558 static void mes_userq_mqd_destroy(struct amdgpu_usermode_queue *queue)
559 {
560 
561 	amdgpu_bo_free_kernel(&queue->fw_obj.obj, &queue->fw_obj.gpu_addr,
562 			      &queue->fw_obj.cpu_ptr);
563 	kfree(queue->userq_prop);
564 	amdgpu_bo_free_kernel(&queue->mqd.obj, &queue->mqd.gpu_addr,
565 			      &queue->mqd.cpu_ptr);
566 
567 	amdgpu_bo_unref(&queue->wptr_obj.obj);
568 }
569 
570 static int mes_userq_preempt(struct amdgpu_usermode_queue *queue)
571 {
572 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
573 	struct amdgpu_device *adev = uq_mgr->adev;
574 	struct mes_suspend_gang_input queue_input;
575 	struct amdgpu_userq_obj *ctx = &queue->fw_obj;
576 	signed long timeout = 2100000; /* 2100 ms */
577 	u64 fence_gpu_addr;
578 	u32 fence_offset;
579 	u64 *fence_ptr;
580 	int i, r;
581 
582 	if (queue->state != AMDGPU_USERQ_STATE_MAPPED)
583 		return 0;
584 	r = amdgpu_wb_get(adev, &fence_offset);
585 	if (r)
586 		return r;
587 
588 	fence_gpu_addr = adev->wb.gpu_addr + (fence_offset * 4);
589 	fence_ptr = (u64 *)&adev->wb.wb[fence_offset];
590 	*fence_ptr = 0;
591 
592 	memset(&queue_input, 0x0, sizeof(struct mes_suspend_gang_input));
593 	queue_input.gang_context_addr = ctx->gpu_addr;
594 	queue_input.suspend_fence_addr = fence_gpu_addr;
595 	queue_input.suspend_fence_value = 1;
596 	amdgpu_mes_lock(&adev->mes);
597 	r = adev->mes.funcs->suspend_gang(&adev->mes, &queue_input);
598 	amdgpu_mes_unlock(&adev->mes);
599 	if (r) {
600 		DRM_ERROR("Failed to suspend gang: %d\n", r);
601 		goto out;
602 	}
603 
604 	for (i = 0; i < timeout; i++) {
605 		if (*fence_ptr == 1)
606 			goto out;
607 		udelay(1);
608 	}
609 	r = -ETIMEDOUT;
610 
611 out:
612 	amdgpu_wb_free(adev, fence_offset);
613 	return r;
614 }
615 
616 static int mes_userq_restore(struct amdgpu_usermode_queue *queue)
617 {
618 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
619 	struct amdgpu_device *adev = uq_mgr->adev;
620 	struct mes_resume_gang_input queue_input;
621 	struct amdgpu_userq_obj *ctx = &queue->fw_obj;
622 	int r;
623 
624 	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
625 		return -EINVAL;
626 	if (queue->state != AMDGPU_USERQ_STATE_PREEMPTED)
627 		return 0;
628 
629 	memset(&queue_input, 0x0, sizeof(struct mes_resume_gang_input));
630 	queue_input.gang_context_addr = ctx->gpu_addr;
631 
632 	amdgpu_mes_lock(&adev->mes);
633 	r = adev->mes.funcs->resume_gang(&adev->mes, &queue_input);
634 	amdgpu_mes_unlock(&adev->mes);
635 	if (r)
636 		dev_err(adev->dev, "Failed to resume queue, err (%d)\n", r);
637 	return r;
638 }
639 
640 const struct amdgpu_userq_funcs userq_mes_funcs = {
641 	.mqd_create = mes_userq_mqd_create,
642 	.mqd_destroy = mes_userq_mqd_destroy,
643 	.unmap = mes_userq_unmap,
644 	.map = mes_userq_map,
645 	.preempt = mes_userq_preempt,
646 	.restore = mes_userq_restore,
647 	.reset = mes_userq_reset,
648 };
649