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