xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c (revision b208e33f6017b5362f7c8192a7e08a8e5c901828)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/firmware.h>
25 #include <drm/drm_exec.h>
26 
27 #include "amdgpu_mes.h"
28 #include "amdgpu.h"
29 #include "soc15_common.h"
30 #include "amdgpu_mes_ctx.h"
31 
32 #define AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
33 #define AMDGPU_ONE_DOORBELL_SIZE 8
34 
35 int amdgpu_mes_doorbell_process_slice(struct amdgpu_device *adev)
36 {
37 	return roundup(AMDGPU_ONE_DOORBELL_SIZE *
38 		       AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
39 		       PAGE_SIZE);
40 }
41 
42 static int amdgpu_mes_doorbell_init(struct amdgpu_device *adev)
43 {
44 	int i;
45 	struct amdgpu_mes *mes = &adev->mes;
46 
47 	/* Bitmap for dynamic allocation of kernel doorbells */
48 	mes->doorbell_bitmap = bitmap_zalloc(PAGE_SIZE / sizeof(u32), GFP_KERNEL);
49 	if (!mes->doorbell_bitmap) {
50 		dev_err(adev->dev, "Failed to allocate MES doorbell bitmap\n");
51 		return -ENOMEM;
52 	}
53 
54 	mes->num_mes_dbs = PAGE_SIZE / AMDGPU_ONE_DOORBELL_SIZE;
55 	for (i = 0; i < AMDGPU_MES_PRIORITY_NUM_LEVELS; i++) {
56 		adev->mes.aggregated_doorbells[i] = mes->db_start_dw_offset + i * 2;
57 		set_bit(i, mes->doorbell_bitmap);
58 	}
59 
60 	return 0;
61 }
62 
63 static int amdgpu_mes_event_log_init(struct amdgpu_device *adev)
64 {
65 	int r;
66 
67 	if (!amdgpu_mes_log_enable)
68 		return 0;
69 
70 	r = amdgpu_bo_create_kernel(adev, adev->mes.event_log_size, PAGE_SIZE,
71 				    AMDGPU_GEM_DOMAIN_VRAM,
72 				    &adev->mes.event_log_gpu_obj,
73 				    &adev->mes.event_log_gpu_addr,
74 				    &adev->mes.event_log_cpu_addr);
75 	if (r) {
76 		dev_warn(adev->dev, "failed to create MES event log buffer (%d)", r);
77 		return r;
78 	}
79 
80 	memset(adev->mes.event_log_cpu_addr, 0, adev->mes.event_log_size);
81 
82 	return  0;
83 
84 }
85 
86 static void amdgpu_mes_doorbell_free(struct amdgpu_device *adev)
87 {
88 	bitmap_free(adev->mes.doorbell_bitmap);
89 }
90 
91 static inline u32 amdgpu_mes_get_hqd_mask(u32 num_pipe,
92 					  u32 num_hqd_per_pipe,
93 					  u32 num_reserved_hqd)
94 {
95 	if (num_pipe == 0)
96 		return 0;
97 
98 	u32 total_hqd_mask = (u32)((1ULL << num_hqd_per_pipe) - 1);
99 	u32 reserved_hqd_mask = (u32)((1ULL << DIV_ROUND_UP(num_reserved_hqd, num_pipe)) - 1);
100 
101 	return (total_hqd_mask & ~reserved_hqd_mask);
102 }
103 
104 int amdgpu_mes_init(struct amdgpu_device *adev)
105 {
106 	int i, r, num_pipes, num_queues = 0;
107 	u32 total_vmid_mask, reserved_vmid_mask;
108 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
109 	u32 gfx_hqd_mask = amdgpu_mes_get_hqd_mask(adev->gfx.me.num_pipe_per_me,
110 				adev->gfx.me.num_queue_per_pipe,
111 				adev->gfx.disable_kq ? 0 : adev->gfx.num_gfx_rings);
112 	u32 compute_hqd_mask = amdgpu_mes_get_hqd_mask(adev->gfx.mec.num_pipe_per_mec,
113 				adev->gfx.mec.num_queue_per_pipe,
114 				adev->gfx.disable_kq ? 0 : adev->gfx.num_compute_rings);
115 
116 	adev->mes.adev = adev;
117 
118 	ida_init(&adev->mes.doorbell_ida);
119 	spin_lock_init(&adev->mes.queue_id_lock);
120 	mutex_init(&adev->mes.mutex_hidden);
121 
122 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++)
123 		spin_lock_init(&adev->mes.ring_lock[i]);
124 
125 	adev->mes.total_max_queue = AMDGPU_FENCE_MES_QUEUE_ID_MASK;
126 	total_vmid_mask = (u32)((1UL << 16) - 1);
127 	reserved_vmid_mask = (u32)((1UL << adev->vm_manager.first_kfd_vmid) - 1);
128 
129 	adev->mes.vmid_mask_mmhub = 0xFF00;
130 	adev->mes.vmid_mask_gfxhub = total_vmid_mask & ~reserved_vmid_mask;
131 
132 	num_pipes = adev->gfx.me.num_pipe_per_me * adev->gfx.me.num_me;
133 	if (num_pipes > AMDGPU_MES_MAX_GFX_PIPES)
134 		dev_warn(adev->dev, "more gfx pipes than supported by MES! (%d vs %d)\n",
135 			 num_pipes, AMDGPU_MES_MAX_GFX_PIPES);
136 
137 	for (i = 0; i < AMDGPU_MES_MAX_GFX_PIPES; i++) {
138 		if (i >= num_pipes)
139 			break;
140 
141 		adev->mes.gfx_hqd_mask[i] = gfx_hqd_mask;
142 	}
143 
144 	num_pipes = adev->gfx.mec.num_pipe_per_mec * adev->gfx.mec.num_mec;
145 	if (num_pipes > AMDGPU_MES_MAX_COMPUTE_PIPES)
146 		dev_warn(adev->dev, "more compute pipes than supported by MES! (%d vs %d)\n",
147 			 num_pipes, AMDGPU_MES_MAX_COMPUTE_PIPES);
148 
149 	for (i = 0; i < AMDGPU_MES_MAX_COMPUTE_PIPES; i++) {
150 		/*
151 		 * Currently, only MEC1 is used for both kernel and user compute queue.
152 		 * To enable other MEC, we need to redistribute queues per pipe and
153 		 * adjust queue resource shared with kfd that needs a separate patch.
154 		 * Skip other MEC for now to avoid potential issues.
155 		 */
156 		if (i >= adev->gfx.mec.num_pipe_per_mec)
157 			break;
158 
159 		adev->mes.compute_hqd_mask[i] = compute_hqd_mask;
160 	}
161 
162 	num_pipes = adev->sdma.num_inst_per_xcc ?
163 		adev->sdma.num_inst_per_xcc : adev->sdma.num_instances;
164 	if (num_pipes > AMDGPU_MES_MAX_SDMA_PIPES)
165 		dev_warn(adev->dev, "more SDMA pipes than supported by MES! (%d vs %d)\n",
166 			 num_pipes, AMDGPU_MES_MAX_SDMA_PIPES);
167 
168 	for (i = 0; i < AMDGPU_MES_MAX_SDMA_PIPES; i++) {
169 		if (i >= num_pipes)
170 			break;
171 		adev->mes.sdma_hqd_mask[i] = 0xfc;
172 	}
173 
174 	dev_info(adev->dev,
175 			 "MES: vmid_mask_mmhub 0x%08x, vmid_mask_gfxhub 0x%08x\n",
176 			 adev->mes.vmid_mask_mmhub,
177 			 adev->mes.vmid_mask_gfxhub);
178 
179 	dev_info(adev->dev,
180 			 "MES: gfx_hqd_mask 0x%08x, compute_hqd_mask 0x%08x, sdma_hqd_mask 0x%08x\n",
181 			 adev->mes.gfx_hqd_mask[0],
182 			 adev->mes.compute_hqd_mask[0],
183 			 adev->mes.sdma_hqd_mask[0]);
184 
185 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++) {
186 		r = amdgpu_wb_get(adev, &adev->mes.sch_ctx_offs[i]);
187 		if (r) {
188 			dev_err(adev->dev,
189 				"(%d) ring trail_fence_offs wb alloc failed\n",
190 				r);
191 			goto error;
192 		}
193 		adev->mes.sch_ctx_gpu_addr[i] =
194 			adev->wb.gpu_addr + (adev->mes.sch_ctx_offs[i] * 4);
195 		adev->mes.sch_ctx_ptr[i] =
196 			(uint64_t *)&adev->wb.wb[adev->mes.sch_ctx_offs[i]];
197 
198 		r = amdgpu_wb_get(adev,
199 				 &adev->mes.query_status_fence_offs[i]);
200 		if (r) {
201 			dev_err(adev->dev,
202 			      "(%d) query_status_fence_offs wb alloc failed\n",
203 			      r);
204 			goto error;
205 		}
206 		adev->mes.query_status_fence_gpu_addr[i] = adev->wb.gpu_addr +
207 			(adev->mes.query_status_fence_offs[i] * 4);
208 		adev->mes.query_status_fence_ptr[i] =
209 			(uint64_t *)&adev->wb.wb[adev->mes.query_status_fence_offs[i]];
210 	}
211 
212 	r = amdgpu_mes_doorbell_init(adev);
213 	if (r)
214 		goto error;
215 
216 	r = amdgpu_mes_event_log_init(adev);
217 	if (r)
218 		goto error_doorbell;
219 
220 	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0)) {
221 		/* When queue/pipe reset is done in MES instead of in the
222 		 * driver, MES passes hung queues information to the driver in
223 		 * hung_queue_hqd_info. Calculate required space to store this
224 		 * information.
225 		 */
226 		for (i = 0; i < AMDGPU_MES_MAX_GFX_PIPES; i++)
227 			num_queues += hweight32(adev->mes.gfx_hqd_mask[i]);
228 
229 		for (i = 0; i < AMDGPU_MES_MAX_COMPUTE_PIPES; i++)
230 			num_queues += hweight32(adev->mes.compute_hqd_mask[i]);
231 
232 		for (i = 0; i < AMDGPU_MES_MAX_SDMA_PIPES; i++)
233 			num_queues += hweight32(adev->mes.sdma_hqd_mask[i]) * num_xcc;
234 
235 		adev->mes.hung_queue_hqd_info_offset = num_queues;
236 		adev->mes.hung_queue_db_array_size = num_queues * 2;
237 	}
238 
239 	if (adev->mes.hung_queue_db_array_size) {
240 		for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++) {
241 			r = amdgpu_bo_create_kernel(adev,
242 						    adev->mes.hung_queue_db_array_size * sizeof(u32),
243 						    PAGE_SIZE,
244 						    AMDGPU_GEM_DOMAIN_GTT,
245 						    &adev->mes.hung_queue_db_array_gpu_obj[i],
246 						    &adev->mes.hung_queue_db_array_gpu_addr[i],
247 						    &adev->mes.hung_queue_db_array_cpu_addr[i]);
248 			if (r) {
249 				dev_warn(adev->dev, "failed to create MES hung db array buffer (%d)", r);
250 				goto error_doorbell;
251 			}
252 		}
253 
254 		adev->gfx.mec.mes_hung_db_array =
255 			kcalloc(amdgpu_mes_get_hung_queue_db_array_size(adev),
256 				sizeof(u32), GFP_KERNEL);
257 
258 		if (!adev->gfx.mec.mes_hung_db_array) {
259 			r = -ENOMEM;
260 			goto error_doorbell;
261 		}
262 	}
263 
264 	return 0;
265 
266 error_doorbell:
267 	amdgpu_mes_doorbell_free(adev);
268 error:
269 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++) {
270 		if (adev->mes.sch_ctx_ptr[i])
271 			amdgpu_wb_free(adev, adev->mes.sch_ctx_offs[i]);
272 		if (adev->mes.query_status_fence_ptr[i])
273 			amdgpu_wb_free(adev,
274 				      adev->mes.query_status_fence_offs[i]);
275 		if (adev->mes.hung_queue_db_array_gpu_obj[i])
276 			amdgpu_bo_free_kernel(&adev->mes.hung_queue_db_array_gpu_obj[i],
277 					      &adev->mes.hung_queue_db_array_gpu_addr[i],
278 					      &adev->mes.hung_queue_db_array_cpu_addr[i]);
279 	}
280 
281 	ida_destroy(&adev->mes.doorbell_ida);
282 	mutex_destroy(&adev->mes.mutex_hidden);
283 	return r;
284 }
285 
286 void amdgpu_mes_fini(struct amdgpu_device *adev)
287 {
288 	int i;
289 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
290 
291 	kfree(adev->gfx.mec.mes_hung_db_array);
292 
293 	amdgpu_bo_free_kernel(&adev->mes.event_log_gpu_obj,
294 			      &adev->mes.event_log_gpu_addr,
295 			      &adev->mes.event_log_cpu_addr);
296 
297 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++) {
298 		if (adev->mes.hung_queue_db_array_gpu_obj[i])
299 			 amdgpu_bo_free_kernel(&adev->mes.hung_queue_db_array_gpu_obj[i],
300 					 &adev->mes.hung_queue_db_array_gpu_addr[i],
301 					 &adev->mes.hung_queue_db_array_cpu_addr[i]);
302 		if (adev->mes.sch_ctx_ptr[i])
303 			amdgpu_wb_free(adev, adev->mes.sch_ctx_offs[i]);
304 		if (adev->mes.query_status_fence_ptr[i])
305 			amdgpu_wb_free(adev,
306 				      adev->mes.query_status_fence_offs[i]);
307 	}
308 
309 	amdgpu_mes_doorbell_free(adev);
310 
311 	ida_destroy(&adev->mes.doorbell_ida);
312 	mutex_destroy(&adev->mes.mutex_hidden);
313 }
314 
315 int amdgpu_mes_suspend(struct amdgpu_device *adev, u32 xcc_id)
316 {
317 	struct mes_suspend_gang_input input;
318 	int r;
319 
320 	if (!amdgpu_mes_suspend_resume_all_supported(adev))
321 		return 0;
322 
323 	memset(&input, 0x0, sizeof(struct mes_suspend_gang_input));
324 	input.suspend_all_gangs = 1;
325 	input.xcc_id = xcc_id;
326 	if ((amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(12, 1, 0)) &&
327 		((adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x71))
328 		input.suspend_all_sdma_gangs = 1;
329 
330 	/*
331 	 * Avoid taking any other locks under MES lock to avoid circular
332 	 * lock dependencies.
333 	 */
334 	amdgpu_mes_lock(&adev->mes);
335 	r = adev->mes.funcs->suspend_gang(&adev->mes, &input);
336 	amdgpu_mes_unlock(&adev->mes);
337 	if (r)
338 		dev_err(adev->dev, "failed to suspend all gangs");
339 
340 	return r;
341 }
342 
343 int amdgpu_mes_resume(struct amdgpu_device *adev, u32 xcc_id)
344 {
345 	struct mes_resume_gang_input input;
346 	int r;
347 
348 	if (!amdgpu_mes_suspend_resume_all_supported(adev))
349 		return 0;
350 
351 	memset(&input, 0x0, sizeof(struct mes_resume_gang_input));
352 	input.resume_all_gangs = 1;
353 	input.xcc_id = xcc_id;
354 
355 	/*
356 	 * Avoid taking any other locks under MES lock to avoid circular
357 	 * lock dependencies.
358 	 */
359 	amdgpu_mes_lock(&adev->mes);
360 	r = adev->mes.funcs->resume_gang(&adev->mes, &input);
361 	amdgpu_mes_unlock(&adev->mes);
362 	if (r)
363 		dev_err(adev->dev, "failed to resume all gangs");
364 
365 	return r;
366 }
367 
368 int amdgpu_mes_map_legacy_queue(struct amdgpu_device *adev,
369 				struct amdgpu_ring *ring, uint32_t xcc_id)
370 {
371 	struct mes_map_legacy_queue_input queue_input;
372 	int r;
373 
374 	memset(&queue_input, 0, sizeof(queue_input));
375 
376 	queue_input.xcc_id = xcc_id;
377 	queue_input.queue_type = ring->funcs->type;
378 	queue_input.doorbell_offset = ring->doorbell_index;
379 	queue_input.pipe_id = ring->pipe;
380 	queue_input.queue_id = ring->queue;
381 	queue_input.mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
382 	queue_input.wptr_addr = ring->wptr_gpu_addr;
383 
384 	amdgpu_mes_lock(&adev->mes);
385 	r = adev->mes.funcs->map_legacy_queue(&adev->mes, &queue_input);
386 	amdgpu_mes_unlock(&adev->mes);
387 	if (r)
388 		dev_err(adev->dev, "failed to map legacy queue\n");
389 
390 	return r;
391 }
392 
393 int amdgpu_mes_unmap_legacy_queue(struct amdgpu_device *adev,
394 				  struct amdgpu_ring *ring,
395 				  enum amdgpu_unmap_queues_action action,
396 				  u64 gpu_addr, u64 seq, uint32_t xcc_id)
397 {
398 	struct mes_unmap_legacy_queue_input queue_input;
399 	int r;
400 
401 	queue_input.xcc_id = xcc_id;
402 	queue_input.action = action;
403 	queue_input.queue_type = ring->funcs->type;
404 	queue_input.doorbell_offset = ring->doorbell_index;
405 	queue_input.pipe_id = ring->pipe;
406 	queue_input.queue_id = ring->queue;
407 	queue_input.trail_fence_addr = gpu_addr;
408 	queue_input.trail_fence_data = seq;
409 
410 	amdgpu_mes_lock(&adev->mes);
411 	r = adev->mes.funcs->unmap_legacy_queue(&adev->mes, &queue_input);
412 	amdgpu_mes_unlock(&adev->mes);
413 	if (r)
414 		dev_err(adev->dev, "failed to unmap legacy queue\n");
415 
416 	return r;
417 }
418 
419 int amdgpu_mes_reset_legacy_queue(struct amdgpu_device *adev,
420 				  struct amdgpu_ring *ring,
421 				  unsigned int vmid,
422 				  bool use_mmio,
423 				  uint32_t xcc_id)
424 {
425 	struct mes_reset_queue_input queue_input;
426 	int r;
427 
428 	memset(&queue_input, 0, sizeof(queue_input));
429 
430 	queue_input.xcc_id = xcc_id;
431 	queue_input.queue_type = ring->funcs->type;
432 	queue_input.doorbell_offset = ring->doorbell_index;
433 	queue_input.me_id = ring->me;
434 	queue_input.pipe_id = ring->pipe;
435 	queue_input.queue_id = ring->queue;
436 	queue_input.mqd_addr = ring->mqd_obj ? amdgpu_bo_gpu_offset(ring->mqd_obj) : 0;
437 	queue_input.wptr_addr = ring->wptr_gpu_addr;
438 	queue_input.vmid = vmid;
439 	queue_input.use_mmio = use_mmio;
440 	queue_input.is_kq = true;
441 	if (ring->funcs->type == AMDGPU_RING_TYPE_GFX)
442 		queue_input.legacy_gfx = true;
443 
444 	amdgpu_mes_lock(&adev->mes);
445 	r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
446 	amdgpu_mes_unlock(&adev->mes);
447 	if (r)
448 		dev_err(adev->dev, "failed to reset legacy queue\n");
449 
450 	return r;
451 }
452 
453 int amdgpu_mes_reset_queue_mmio(struct amdgpu_device *adev,
454 				int queue_type,
455 				unsigned int vmid,
456 				unsigned int me,
457 				unsigned int pipe,
458 				unsigned int queue,
459 				uint32_t xcc_id)
460 {
461 	struct mes_reset_queue_input queue_input;
462 	int r;
463 
464 	memset(&queue_input, 0, sizeof(queue_input));
465 
466 	queue_input.xcc_id = xcc_id;
467 	queue_input.me_id = me;
468 	queue_input.pipe_id = pipe;
469 	queue_input.queue_id = queue;
470 	queue_input.vmid = vmid;
471 	queue_input.queue_type = queue_type;
472 	queue_input.use_mmio = true;
473 
474 	amdgpu_mes_lock(&adev->mes);
475 	r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
476 	amdgpu_mes_unlock(&adev->mes);
477 	if (r)
478 		dev_err(adev->dev, "failed to reset legacy queue\n");
479 
480 	return r;
481 }
482 
483 int amdgpu_mes_reset_user_queue(struct amdgpu_device *adev,
484 				int queue_type,
485 				unsigned int doorbell_index,
486 				unsigned int xcc_id)
487 {
488 	struct mes_reset_queue_input queue_input;
489 	int r;
490 
491 	memset(&queue_input, 0, sizeof(queue_input));
492 
493 	queue_input.xcc_id = xcc_id;
494 	queue_input.queue_type = queue_type;
495 	queue_input.doorbell_offset = doorbell_index;
496 
497 	amdgpu_mes_lock(&adev->mes);
498 	r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
499 	amdgpu_mes_unlock(&adev->mes);
500 	if (r)
501 		dev_err(adev->dev, "failed to reset user queue\n");
502 
503 	return r;
504 }
505 
506 int amdgpu_mes_get_hung_queue_db_array_size(struct amdgpu_device *adev)
507 {
508 	return adev->mes.hung_queue_db_array_size;
509 }
510 
511 int amdgpu_mes_detect_and_reset_hung_queues(struct amdgpu_device *adev,
512 					    int queue_type,
513 					    bool detect_only,
514 					    unsigned int *hung_db_num,
515 					    u32 *hung_db_array,
516 					    uint32_t xcc_id)
517 {
518 	struct mes_detect_and_reset_queue_input input;
519 	u32 *db_array = adev->mes.hung_queue_db_array_cpu_addr[xcc_id];
520 	int hqd_info_offset = adev->mes.hung_queue_hqd_info_offset, r, i;
521 
522 	if (!hung_db_num || !hung_db_array)
523 		return -EINVAL;
524 
525 	if ((queue_type != AMDGPU_RING_TYPE_GFX) &&
526 	    (queue_type != AMDGPU_RING_TYPE_COMPUTE) &&
527 	    (queue_type != AMDGPU_RING_TYPE_SDMA))
528 		return -EINVAL;
529 
530 	/* Clear the doorbell array before detection */
531 	memset(adev->mes.hung_queue_db_array_cpu_addr[xcc_id], AMDGPU_MES_INVALID_DB_OFFSET,
532 		adev->mes.hung_queue_db_array_size * sizeof(u32));
533 	input.queue_type = queue_type;
534 	input.detect_only = detect_only;
535 	input.xcc_id = xcc_id;
536 
537 	r = adev->mes.funcs->detect_and_reset_hung_queues(&adev->mes,
538 							  &input);
539 
540 	if (r && detect_only) {
541 		dev_err(adev->dev, "Failed to detect hung queues\n");
542 		return r;
543 	}
544 
545 	*hung_db_num = 0;
546 	/* MES passes hung queues' doorbell to driver */
547 	for (i = 0; i < adev->mes.hung_queue_hqd_info_offset; i++) {
548 		/* Finding hung queues where db_array[i] is a valid doorbell */
549 		if (db_array[i] != AMDGPU_MES_INVALID_DB_OFFSET) {
550 			hung_db_array[i] = db_array[i];
551 			*hung_db_num += 1;
552 		}
553 	}
554 
555 	if (r && !(*hung_db_num)) {
556 		dev_err(adev->dev, "Failed to detect and reset hung queues\n");
557 		return r;
558 	}
559 
560 	for (i = hqd_info_offset; i < hqd_info_offset + *hung_db_num; i++)
561 		hung_db_array[i] = db_array[i];
562 
563 	return r;
564 }
565 
566 uint32_t amdgpu_mes_rreg(struct amdgpu_device *adev, uint32_t reg,
567 			 uint32_t xcc_id)
568 {
569 	struct mes_misc_op_input op_input;
570 	int r, val = 0;
571 	uint32_t addr_offset = 0;
572 	uint64_t read_val_gpu_addr;
573 	uint32_t *read_val_ptr;
574 
575 	if (amdgpu_wb_get(adev, &addr_offset)) {
576 		dev_err(adev->dev, "critical bug! too many mes readers\n");
577 		goto error;
578 	}
579 	read_val_gpu_addr = adev->wb.gpu_addr + (addr_offset * 4);
580 	read_val_ptr = (uint32_t *)&adev->wb.wb[addr_offset];
581 	op_input.xcc_id = xcc_id;
582 	op_input.op = MES_MISC_OP_READ_REG;
583 	op_input.read_reg.reg_offset = reg;
584 	op_input.read_reg.buffer_addr = read_val_gpu_addr;
585 
586 	if (!adev->mes.funcs->misc_op) {
587 		dev_err(adev->dev, "mes rreg is not supported!\n");
588 		goto error;
589 	}
590 
591 	amdgpu_mes_lock(&adev->mes);
592 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
593 	amdgpu_mes_unlock(&adev->mes);
594 	if (r)
595 		dev_err(adev->dev, "failed to read reg (0x%x)\n", reg);
596 	else
597 		val = *(read_val_ptr);
598 
599 error:
600 	if (addr_offset)
601 		amdgpu_wb_free(adev, addr_offset);
602 	return val;
603 }
604 
605 int amdgpu_mes_wreg(struct amdgpu_device *adev, uint32_t reg,
606 		    uint32_t val, uint32_t xcc_id)
607 {
608 	struct mes_misc_op_input op_input;
609 	int r;
610 
611 	op_input.xcc_id = xcc_id;
612 	op_input.op = MES_MISC_OP_WRITE_REG;
613 	op_input.write_reg.reg_offset = reg;
614 	op_input.write_reg.reg_value = val;
615 
616 	if (!adev->mes.funcs->misc_op) {
617 		dev_err(adev->dev, "mes wreg is not supported!\n");
618 		r = -EINVAL;
619 		goto error;
620 	}
621 
622 	amdgpu_mes_lock(&adev->mes);
623 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
624 	amdgpu_mes_unlock(&adev->mes);
625 	if (r)
626 		dev_err(adev->dev, "failed to write reg (0x%x)\n", reg);
627 
628 error:
629 	return r;
630 }
631 
632 int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,
633 				  uint32_t reg0, uint32_t reg1,
634 				  uint32_t ref, uint32_t mask,
635 				  uint32_t xcc_id)
636 {
637 	struct mes_misc_op_input op_input;
638 	int r;
639 
640 	op_input.xcc_id = xcc_id;
641 	op_input.op = MES_MISC_OP_WRM_REG_WR_WAIT;
642 	op_input.wrm_reg.reg0 = reg0;
643 	op_input.wrm_reg.reg1 = reg1;
644 	op_input.wrm_reg.ref = ref;
645 	op_input.wrm_reg.mask = mask;
646 
647 	if (!adev->mes.funcs->misc_op) {
648 		dev_err(adev->dev, "mes reg_write_reg_wait is not supported!\n");
649 		r = -EINVAL;
650 		goto error;
651 	}
652 
653 	amdgpu_mes_lock(&adev->mes);
654 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
655 	amdgpu_mes_unlock(&adev->mes);
656 	if (r)
657 		dev_err(adev->dev, "failed to reg_write_reg_wait\n");
658 
659 error:
660 	return r;
661 }
662 
663 int amdgpu_mes_hdp_flush(struct amdgpu_device *adev)
664 {
665 	uint32_t hdp_flush_req_offset, hdp_flush_done_offset;
666 	struct amdgpu_ring *mes_ring;
667 	uint32_t ref_and_mask = 0, reg_mem_engine = 0;
668 
669 	if (!adev->gfx.funcs->get_hdp_flush_mask) {
670 		dev_err(adev->dev, "mes hdp flush is not supported.\n");
671 		return -EINVAL;
672 	}
673 
674 	mes_ring = &adev->mes.ring[0];
675 	hdp_flush_req_offset = adev->nbio.funcs->get_hdp_flush_req_offset(adev);
676 	hdp_flush_done_offset = adev->nbio.funcs->get_hdp_flush_done_offset(adev);
677 
678 	adev->gfx.funcs->get_hdp_flush_mask(mes_ring, &ref_and_mask, &reg_mem_engine);
679 
680 	return amdgpu_mes_reg_write_reg_wait(adev, hdp_flush_req_offset, hdp_flush_done_offset,
681 					     ref_and_mask, ref_and_mask, 0);
682 }
683 
684 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
685 				uint64_t process_context_addr,
686 				uint32_t spi_gdbg_per_vmid_cntl,
687 				const uint32_t *tcp_watch_cntl,
688 				uint32_t flags,
689 				bool trap_en,
690 				uint32_t xcc_id)
691 {
692 	struct mes_misc_op_input op_input = {0};
693 	int r;
694 
695 	if (!adev->mes.funcs->misc_op) {
696 		dev_err(adev->dev,
697 			"mes set shader debugger is not supported!\n");
698 		return -EINVAL;
699 	}
700 
701 	op_input.xcc_id = xcc_id;
702 	op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
703 	op_input.set_shader_debugger.process_context_addr = process_context_addr;
704 	op_input.set_shader_debugger.flags.u32all = flags;
705 
706 	/* use amdgpu mes_flush_shader_debugger instead */
707 	if (op_input.set_shader_debugger.flags.process_ctx_flush)
708 		return -EINVAL;
709 
710 	op_input.set_shader_debugger.spi_gdbg_per_vmid_cntl = spi_gdbg_per_vmid_cntl;
711 	memcpy(op_input.set_shader_debugger.tcp_watch_cntl, tcp_watch_cntl,
712 			sizeof(op_input.set_shader_debugger.tcp_watch_cntl));
713 
714 	if (((adev->mes.sched_version & AMDGPU_MES_API_VERSION_MASK) >>
715 			AMDGPU_MES_API_VERSION_SHIFT) >= 14)
716 		op_input.set_shader_debugger.trap_en = trap_en;
717 
718 	amdgpu_mes_lock(&adev->mes);
719 
720 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
721 	if (r)
722 		dev_err(adev->dev, "failed to set_shader_debugger\n");
723 
724 	amdgpu_mes_unlock(&adev->mes);
725 
726 	return r;
727 }
728 
729 int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
730 				     uint64_t process_context_addr,
731 				     uint32_t xcc_id)
732 {
733 	struct mes_misc_op_input op_input = {0};
734 	int r;
735 
736 	if (!adev->mes.funcs->misc_op) {
737 		dev_err(adev->dev,
738 			"mes flush shader debugger is not supported!\n");
739 		return -EINVAL;
740 	}
741 
742 	op_input.xcc_id = xcc_id;
743 	op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
744 	op_input.set_shader_debugger.process_context_addr = process_context_addr;
745 	op_input.set_shader_debugger.flags.process_ctx_flush = true;
746 
747 	amdgpu_mes_lock(&adev->mes);
748 
749 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
750 	if (r)
751 		dev_err(adev->dev, "failed to set_shader_debugger\n");
752 
753 	amdgpu_mes_unlock(&adev->mes);
754 
755 	return r;
756 }
757 
758 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
759 						   enum amdgpu_mes_priority_level prio)
760 {
761 	return adev->mes.aggregated_doorbells[prio];
762 }
763 
764 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe)
765 {
766 	const struct mes_firmware_header_v1_0 *mes_hdr;
767 	struct amdgpu_firmware_info *info;
768 	char ucode_prefix[30];
769 	char fw_name[50];
770 	bool need_retry = false;
771 	u32 *ucode_ptr;
772 	int r;
773 
774 	amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix,
775 				       sizeof(ucode_prefix));
776 	if (adev->enable_uni_mes) {
777 		snprintf(fw_name, sizeof(fw_name),
778 			 "amdgpu/%s_uni_mes.bin", ucode_prefix);
779 	} else if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0) &&
780 	    amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(12, 0, 0)) {
781 		snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
782 			 ucode_prefix,
783 			 pipe == AMDGPU_MES_SCHED_PIPE ? "_2" : "1");
784 		need_retry = true;
785 	} else {
786 		snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
787 			 ucode_prefix,
788 			 pipe == AMDGPU_MES_SCHED_PIPE ? "" : "1");
789 	}
790 
791 	r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe], AMDGPU_UCODE_REQUIRED,
792 				 "%s", fw_name);
793 	if (r && need_retry && pipe == AMDGPU_MES_SCHED_PIPE) {
794 		dev_info(adev->dev, "try to fall back to %s_mes.bin\n", ucode_prefix);
795 		r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe],
796 					 AMDGPU_UCODE_REQUIRED,
797 					 "amdgpu/%s_mes.bin", ucode_prefix);
798 	}
799 
800 	if (r)
801 		goto out;
802 
803 	mes_hdr = (const struct mes_firmware_header_v1_0 *)
804 		adev->mes.fw[pipe]->data;
805 	adev->mes.uc_start_addr[pipe] =
806 		le32_to_cpu(mes_hdr->mes_uc_start_addr_lo) |
807 		((uint64_t)(le32_to_cpu(mes_hdr->mes_uc_start_addr_hi)) << 32);
808 	adev->mes.data_start_addr[pipe] =
809 		le32_to_cpu(mes_hdr->mes_data_start_addr_lo) |
810 		((uint64_t)(le32_to_cpu(mes_hdr->mes_data_start_addr_hi)) << 32);
811 	ucode_ptr = (u32 *)(adev->mes.fw[pipe]->data +
812 			  sizeof(union amdgpu_firmware_header));
813 	adev->mes.fw_version[pipe] =
814 		le32_to_cpu(ucode_ptr[24]) & AMDGPU_MES_VERSION_MASK;
815 
816 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
817 		int ucode, ucode_data;
818 
819 		if (pipe == AMDGPU_MES_SCHED_PIPE) {
820 			ucode = AMDGPU_UCODE_ID_CP_MES;
821 			ucode_data = AMDGPU_UCODE_ID_CP_MES_DATA;
822 		} else {
823 			ucode = AMDGPU_UCODE_ID_CP_MES1;
824 			ucode_data = AMDGPU_UCODE_ID_CP_MES1_DATA;
825 		}
826 
827 		info = &adev->firmware.ucode[ucode];
828 		info->ucode_id = ucode;
829 		info->fw = adev->mes.fw[pipe];
830 		adev->firmware.fw_size +=
831 			ALIGN(le32_to_cpu(mes_hdr->mes_ucode_size_bytes),
832 			      PAGE_SIZE);
833 
834 		info = &adev->firmware.ucode[ucode_data];
835 		info->ucode_id = ucode_data;
836 		info->fw = adev->mes.fw[pipe];
837 		adev->firmware.fw_size +=
838 			ALIGN(le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes),
839 			      PAGE_SIZE);
840 	}
841 
842 	return 0;
843 out:
844 	amdgpu_ucode_release(&adev->mes.fw[pipe]);
845 	return r;
846 }
847 
848 void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev)
849 {
850 	u32 fw_from_ucode = adev->mes.fw_version[AMDGPU_MES_SCHED_PIPE];
851 	u32 fw_from_reg = adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;
852 
853 	if (fw_from_ucode != fw_from_reg)
854 		dev_info(adev->dev,
855 			 "MES firmware reports incorrect version in ucode binary (0x%x vs 0x%x)\n",
856 			 fw_from_ucode, fw_from_reg);
857 }
858 
859 
860 bool amdgpu_mes_suspend_resume_all_supported(struct amdgpu_device *adev)
861 {
862 	uint32_t mes_rev = adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;
863 
864 	return ((amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0) &&
865 		 amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(12, 0, 0) &&
866 		 mes_rev >= 0x63) ||
867 		amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(12, 0, 0));
868 }
869 
870 bool amdgpu_mes_queue_reset_by_mes_supported(struct amdgpu_device *adev)
871 {
872 	u32 ip_maj = IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0));
873 	u32 ip_min = IP_VERSION_MIN(amdgpu_ip_version(adev, GC_HWIP, 0));
874 	u32 mes_sched = adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;
875 
876 	return (ip_maj == 11 && mes_sched >= 0x8c) ||
877 		((ip_maj == 12 && ip_min == 0) && mes_sched >= 0x8d) ||
878 		((ip_maj == 12 && ip_min == 1) && mes_sched >= 0x73);
879 }
880 
881 /* Fix me -- node_id is used to identify the correct MES instances in the future */
882 static int amdgpu_mes_set_enforce_isolation(struct amdgpu_device *adev,
883 					    uint32_t node_id, bool enable)
884 {
885 	struct mes_misc_op_input op_input = {0};
886 	int r;
887 
888 	op_input.op = MES_MISC_OP_CHANGE_CONFIG;
889 	op_input.change_config.option.limit_single_process = enable ? 1 : 0;
890 
891 	if (!adev->mes.funcs->misc_op) {
892 		dev_err(adev->dev, "mes change config is not supported!\n");
893 		r = -EINVAL;
894 		goto error;
895 	}
896 
897 	amdgpu_mes_lock(&adev->mes);
898 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
899 	amdgpu_mes_unlock(&adev->mes);
900 	if (r)
901 		dev_err(adev->dev, "failed to change_config.\n");
902 
903 error:
904 	return r;
905 }
906 
907 int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev)
908 {
909 	int i, r = 0;
910 
911 	if (adev->enable_mes && adev->gfx.enable_cleaner_shader) {
912 		mutex_lock(&adev->enforce_isolation_mutex);
913 		for (i = 0; i < (adev->xcp_mgr ? adev->xcp_mgr->num_xcps : 1); i++) {
914 			if (adev->enforce_isolation[i] == AMDGPU_ENFORCE_ISOLATION_ENABLE)
915 				r |= amdgpu_mes_set_enforce_isolation(adev, i, true);
916 			else
917 				r |= amdgpu_mes_set_enforce_isolation(adev, i, false);
918 		}
919 		mutex_unlock(&adev->enforce_isolation_mutex);
920 	}
921 	return r;
922 }
923 
924 /**
925  * amdgpu_mes_rs64mem_init - initialize RS64 local memory context arrays
926  *
927  * @mes: MES instance
928  *
929  * Returns 0 on success, negative errno on failure.
930  */
931 int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes)
932 {
933 	struct amdgpu_device *adev = container_of(mes, struct amdgpu_device, mes);
934 	int r;
935 
936 	if (!mes->use_rs64mem)
937 		return 0;
938 
939 	r = amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
940 				    AMDGPU_GEM_DOMAIN_GTT,
941 				    &mes->ctx_array_size_bo,
942 				    &mes->ctx_array_size_gpu_addr,
943 				    (void **)&mes->ctx_array_size_cpu_ptr);
944 	if (r) {
945 		dev_err(adev->dev,
946 			"Failed to allocate ctx array size BO, r=%d\n", r);
947 		return r;
948 	}
949 
950 	memset(mes->ctx_array_size_cpu_ptr, 0, PAGE_SIZE);
951 
952 	return 0;
953 }
954 
955  /**
956   * amdgpu_mes_rs64mem_fini - tear down RS64 local memory management
957   *
958   * @mes: MES instance
959   */
960 void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes)
961 {
962 	if (mes->ctx_array_size_bo) {
963 		amdgpu_bo_free_kernel(&mes->ctx_array_size_bo,
964 				      &mes->ctx_array_size_gpu_addr,
965 				      (void **)&mes->ctx_array_size_cpu_ptr);
966 	}
967 	bitmap_free(mes->proc_ctx_bitmap);
968 	bitmap_free(mes->gang_ctx_bitmap);
969 	mes->use_rs64mem = false;
970 }
971 
972 /**
973  * amdgpu_mes_rs64mem_setup_bitmaps - allocate bitmaps after querying MES
974  *
975  * Called after QUERY_SCHEDULER_STATUS returns and MES has written
976  * the array sizes to the GPU buffer. Reads the sizes and allocates
977  * the tracking bitmaps.
978  *
979  * @mes: MES instance
980  *
981  * Returns 0 on success, negative errno on failure.
982  */
983 int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
984 {
985 	struct amdgpu_device *adev = container_of(mes, struct amdgpu_device, mes);
986 
987 	if (!mes->use_rs64mem || !mes->ctx_array_size_cpu_ptr)
988 		return 0;
989 
990 	/*
991 	 * MES FW wrote the sizes to the GPU buffer:
992 	 *   ctx_array_size_cpu_ptr[0] = proc_ctx_array_size (N)
993 	 *   ctx_array_size_cpu_ptr[1] = gang_ctx_array_size (M)
994 	 */
995 	mes->proc_ctx_array_size = mes->ctx_array_size_cpu_ptr[0];
996 	mes->gang_ctx_array_size = mes->ctx_array_size_cpu_ptr[1];
997 
998 	/* Sanity check - MES FW typically returns N=50, M=300 */
999 	if (mes->proc_ctx_array_size == 0 || mes->gang_ctx_array_size == 0) {
1000 		dev_warn(adev->dev,
1001 			 "MES returned zero ctx array sizes (proc=%u, gang=%u), "
1002 			 "disabling RS64 local memory optimization\n",
1003 			 mes->proc_ctx_array_size, mes->gang_ctx_array_size);
1004 		mes->use_rs64mem = false;
1005 		return 0;
1006 	}
1007 
1008 	/* Cap to safety limits */
1009 	if (mes->proc_ctx_array_size > AMDGPU_MES_PROC_CTX_ARRAY_MAX)
1010 		mes->proc_ctx_array_size = AMDGPU_MES_PROC_CTX_ARRAY_MAX;
1011 	if (mes->gang_ctx_array_size > AMDGPU_MES_GANG_CTX_ARRAY_MAX)
1012 		mes->gang_ctx_array_size = AMDGPU_MES_GANG_CTX_ARRAY_MAX;
1013 
1014 	dev_info(adev->dev,
1015 		 "MES RS64 local memory: proc_ctx_array_size:%u, "
1016 		 "gang_ctx_array_size:%u\n",
1017 		 mes->proc_ctx_array_size, mes->gang_ctx_array_size);
1018 
1019 	/* Allocate bitmaps */
1020 	mes->proc_ctx_bitmap = bitmap_zalloc(mes->proc_ctx_array_size,
1021 					     GFP_KERNEL);
1022 	if (!mes->proc_ctx_bitmap) {
1023 		mes->use_rs64mem = false;
1024 		return -ENOMEM;
1025 	}
1026 
1027 	mes->gang_ctx_bitmap = bitmap_zalloc(mes->gang_ctx_array_size,
1028 					     GFP_KERNEL);
1029 	if (!mes->gang_ctx_bitmap) {
1030 		bitmap_free(mes->proc_ctx_bitmap);
1031 		mes->proc_ctx_bitmap = NULL;
1032 		mes->use_rs64mem = false;
1033 		return -ENOMEM;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 /**
1040  * amdgpu_mes_alloc_proc_ctx_index - allocate a process context slot
1041  *
1042  * @mes: MES instance
1043  * @queue: Usermode queue receiving the allocated process context index
1044  *
1045  * Returns 0 on success, -ENOSPC if all slots are used, or
1046  * -EOPNOTSUPP if RS64 local memory is unavailable.
1047  */
1048 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
1049 				    struct amdgpu_usermode_queue *queue)
1050 {
1051 	unsigned long bit;
1052 
1053 	if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
1054 		return -EOPNOTSUPP;
1055 
1056 	amdgpu_mes_lock(mes);
1057 	bit = find_first_zero_bit(mes->proc_ctx_bitmap,
1058 				  mes->proc_ctx_array_size);
1059 	if (bit >= mes->proc_ctx_array_size) {
1060 		amdgpu_mes_unlock(mes);
1061 		return -ENOSPC;
1062 	}
1063 	set_bit(bit, mes->proc_ctx_bitmap);
1064 	queue->proc_ctx_array_index = (uint32_t)bit;
1065 	amdgpu_mes_unlock(mes);
1066 
1067 	return 0;
1068 }
1069 
1070  /**
1071   * amdgpu_mes_free_proc_ctx_index - free a process context slot
1072   *
1073   * @mes: MES instance
1074   * @queue: Usermode queue whose process context index is released
1075   */
1076 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
1077 				    struct amdgpu_usermode_queue *queue)
1078 {
1079 	if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
1080 		return;
1081 	if (queue->proc_ctx_array_index >= mes->proc_ctx_array_size)
1082 		return;
1083 
1084 	amdgpu_mes_lock(mes);
1085 	clear_bit(queue->proc_ctx_array_index, mes->proc_ctx_bitmap);
1086 	amdgpu_mes_unlock(mes);
1087 }
1088 
1089  /**
1090   * amdgpu_mes_alloc_gang_ctx_index - allocate a gang context slot
1091   *
1092   * @mes: MES instance
1093   * @queue: Usermode queue receiving the allocated gang context index
1094   *
1095   * Returns 0 on success, -ENOSPC if all slots are used, or
1096   * -EOPNOTSUPP if RS64 local memory is unavailable.
1097   */
1098 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
1099 				    struct amdgpu_usermode_queue *queue)
1100 {
1101 	unsigned long bit;
1102 
1103 	if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
1104 		return -EOPNOTSUPP;
1105 
1106 	amdgpu_mes_lock(mes);
1107 	bit = find_first_zero_bit(mes->gang_ctx_bitmap,
1108 				  mes->gang_ctx_array_size);
1109 	if (bit >= mes->gang_ctx_array_size) {
1110 		amdgpu_mes_unlock(mes);
1111 		return -ENOSPC;
1112 	}
1113 	set_bit(bit, mes->gang_ctx_bitmap);
1114 	queue->gang_ctx_array_index = bit;
1115 	amdgpu_mes_unlock(mes);
1116 
1117 	return 0;
1118 }
1119 
1120  /**
1121   * amdgpu_mes_free_gang_ctx_index - free a gang context slot
1122   *
1123   * @mes: MES instance
1124   * @queue: Usermode queue whose gang context index is released
1125   */
1126 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
1127 				    struct amdgpu_usermode_queue *queue)
1128 {
1129 	if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
1130 		return;
1131 	if (queue->gang_ctx_array_index >= mes->gang_ctx_array_size)
1132 		return;
1133 
1134 	amdgpu_mes_lock(mes);
1135 	clear_bit(queue->gang_ctx_array_index, mes->gang_ctx_bitmap);
1136 	amdgpu_mes_unlock(mes);
1137 }
1138 
1139 #if defined(CONFIG_DEBUG_FS)
1140 
1141 static int amdgpu_debugfs_mes_event_log_show(struct seq_file *m, void *unused)
1142 {
1143 	struct amdgpu_device *adev = m->private;
1144 	uint32_t *mem = (uint32_t *)(adev->mes.event_log_cpu_addr);
1145 
1146 	seq_hex_dump(m, "", DUMP_PREFIX_OFFSET, 32, 4,
1147 		     mem, adev->mes.event_log_size, false);
1148 
1149 	return 0;
1150 }
1151 
1152 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_mes_event_log);
1153 
1154 #endif
1155 
1156 void amdgpu_debugfs_mes_event_log_init(struct amdgpu_device *adev)
1157 {
1158 
1159 #if defined(CONFIG_DEBUG_FS)
1160 	struct drm_minor *minor = adev_to_drm(adev)->primary;
1161 	struct dentry *root = minor->debugfs_root;
1162 	if (adev->enable_mes && amdgpu_mes_log_enable)
1163 		debugfs_create_file("amdgpu_mes_event_log", 0444, root,
1164 				    adev, &amdgpu_debugfs_mes_event_log_fops);
1165 
1166 #endif
1167 }
1168