xref: /linux/drivers/gpu/drm/amd/amdgpu/mes_v12_1.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /*
2  * Copyright 2025 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 <linux/module.h>
26 #include "amdgpu.h"
27 #include "soc15_common.h"
28 #include "soc_v1_0.h"
29 #include "gc/gc_12_1_0_offset.h"
30 #include "gc/gc_12_1_0_sh_mask.h"
31 #include "gc/gc_11_0_0_default.h"
32 #include "v12_structs.h"
33 #include "mes_v12_api_def.h"
34 #include "gfx_v12_1_pkt.h"
35 #include "sdma_v7_1_0_pkt_open.h"
36 
37 MODULE_FIRMWARE("amdgpu/gc_12_1_0_uni_mes.bin");
38 
39 static int mes_v12_1_hw_init(struct amdgpu_ip_block *ip_block);
40 static int mes_v12_1_xcc_hw_init(struct amdgpu_ip_block *ip_block, int xcc_id);
41 static int mes_v12_1_hw_fini(struct amdgpu_ip_block *ip_block);
42 static int mes_v12_1_kiq_hw_init(struct amdgpu_device *adev, uint32_t xcc_id);
43 static int mes_v12_1_kiq_hw_fini(struct amdgpu_device *adev, uint32_t xcc_id);
44 static int mes_v12_1_setup_coop_mode(struct amdgpu_device *adev, int xcc_id);
45 
46 #define MES_EOP_SIZE   2048
47 #define MES12_HUNG_DB_OFFSET_ARRAY_SIZE 8 /* [0:3] = db offset [4:7] hqd info */
48 #define MES12_HUNG_HQD_INFO_OFFSET      4
49 
50 #define regCP_HQD_IB_CONTROL_MES_12_1_DEFAULT 0x100000
51 #define XCC_MID_MASK 0x41000000
52 
53 static void mes_v12_1_ring_set_wptr(struct amdgpu_ring *ring)
54 {
55 	struct amdgpu_device *adev = ring->adev;
56 
57 	if (ring->use_doorbell) {
58 		atomic64_set((atomic64_t *)ring->wptr_cpu_addr,
59 			     ring->wptr);
60 		WDOORBELL64(ring->doorbell_index, ring->wptr);
61 	} else {
62 		dev_warn_once(adev->dev, "%s requires doorbell!\n", __func__);
63 	}
64 }
65 
66 static u64 mes_v12_1_ring_get_rptr(struct amdgpu_ring *ring)
67 {
68 	return *ring->rptr_cpu_addr;
69 }
70 
71 static u64 mes_v12_1_ring_get_wptr(struct amdgpu_ring *ring)
72 {
73 	struct amdgpu_device *adev = ring->adev;
74 	u64 wptr;
75 
76 	if (ring->use_doorbell) {
77 		wptr = atomic64_read((atomic64_t *)ring->wptr_cpu_addr);
78 	} else {
79 		dev_warn_once(adev->dev, "%s requires doorbell!\n", __func__);
80 		wptr = 0;
81 
82 	}
83 	return wptr;
84 }
85 
86 static const struct amdgpu_ring_funcs mes_v12_1_ring_funcs = {
87 	.type = AMDGPU_RING_TYPE_MES,
88 	.align_mask = 1,
89 	.nop = 0,
90 	.support_64bit_ptrs = true,
91 	.get_rptr = mes_v12_1_ring_get_rptr,
92 	.get_wptr = mes_v12_1_ring_get_wptr,
93 	.set_wptr = mes_v12_1_ring_set_wptr,
94 	.insert_nop = amdgpu_ring_insert_nop,
95 };
96 
97 static const char *mes_v12_1_opcodes[] = {
98 	"SET_HW_RSRC",
99 	"SET_SCHEDULING_CONFIG",
100 	"ADD_QUEUE",
101 	"REMOVE_QUEUE",
102 	"PERFORM_YIELD",
103 	"SET_GANG_PRIORITY_LEVEL",
104 	"SUSPEND",
105 	"RESUME",
106 	"RESET",
107 	"SET_LOG_BUFFER",
108 	"CHANGE_GANG_PRORITY",
109 	"QUERY_SCHEDULER_STATUS",
110 	"unused",
111 	"SET_DEBUG_VMID",
112 	"MISC",
113 	"UPDATE_ROOT_PAGE_TABLE",
114 	"AMD_LOG",
115 	"SET_SE_MODE",
116 	"SET_GANG_SUBMIT",
117 	"SET_HW_RSRC_1",
118 	"INVALIDATE_TLBS",
119 };
120 
121 static const char *mes_v12_1_misc_opcodes[] = {
122 	"WRITE_REG",
123 	"INV_GART",
124 	"QUERY_STATUS",
125 	"READ_REG",
126 	"WAIT_REG_MEM",
127 	"SET_SHADER_DEBUGGER",
128 	"NOTIFY_WORK_ON_UNMAPPED_QUEUE",
129 	"NOTIFY_TO_UNMAP_PROCESSES",
130 };
131 
132 static const char *mes_v12_1_get_op_string(union MESAPI__MISC *x_pkt)
133 {
134 	const char *op_str = NULL;
135 
136 	if (x_pkt->header.opcode < ARRAY_SIZE(mes_v12_1_opcodes))
137 		op_str = mes_v12_1_opcodes[x_pkt->header.opcode];
138 
139 	return op_str;
140 }
141 
142 static const char *mes_v12_1_get_misc_op_string(union MESAPI__MISC *x_pkt)
143 {
144 	const char *op_str = NULL;
145 
146 	if ((x_pkt->header.opcode == MES_SCH_API_MISC) &&
147 	    (x_pkt->opcode < ARRAY_SIZE(mes_v12_1_misc_opcodes)))
148 		op_str = mes_v12_1_misc_opcodes[x_pkt->opcode];
149 
150 	return op_str;
151 }
152 
153 static int mes_v12_1_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
154 					    int xcc_id, int pipe, void *pkt,
155 					    int size, int api_status_off)
156 {
157 	union MESAPI__QUERY_MES_STATUS mes_status_pkt;
158 	signed long timeout = 2100000; /* 2100 ms */
159 	struct amdgpu_device *adev = mes->adev;
160 	struct amdgpu_ring *ring = &mes->ring[MES_PIPE_INST(xcc_id, pipe)];
161 	spinlock_t *ring_lock = &mes->ring_lock[MES_PIPE_INST(xcc_id, pipe)];
162 	struct MES_API_STATUS *api_status;
163 	union MESAPI__MISC *x_pkt = pkt;
164 	const char *op_str, *misc_op_str;
165 	unsigned long flags;
166 	u64 status_gpu_addr;
167 	u32 seq, status_offset;
168 	u32 *status_ptr;
169 	signed long r;
170 	int ret;
171 
172 	if (x_pkt->header.opcode >= MES_SCH_API_MAX)
173 		return -EINVAL;
174 
175 	if (amdgpu_emu_mode) {
176 		timeout *= 1000;
177 	} else if (amdgpu_sriov_vf(adev)) {
178 		/* Worst case in sriov where all other 15 VF timeout, each VF needs about 600ms */
179 		timeout = 15 * 600 * 1000;
180 	}
181 
182 	ret = amdgpu_wb_get(adev, &status_offset);
183 	if (ret)
184 		return ret;
185 
186 	status_gpu_addr = adev->wb.gpu_addr + (status_offset * 4);
187 	status_ptr = &adev->wb.wb[status_offset];
188 	*status_ptr = 0;
189 
190 	spin_lock_irqsave(ring_lock, flags);
191 	r = amdgpu_ring_alloc(ring, (size + sizeof(mes_status_pkt)) / 4);
192 	if (r)
193 		goto error_unlock_free;
194 
195 	seq = ++ring->fence_drv.sync_seq;
196 	r = amdgpu_fence_wait_polling(ring,
197 				      seq - ring->fence_drv.num_fences_mask,
198 				      timeout);
199 	if (r < 1)
200 		goto error_undo;
201 
202 	api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
203 	api_status->api_completion_fence_addr = status_gpu_addr;
204 	api_status->api_completion_fence_value = 1;
205 
206 	amdgpu_ring_write_multiple(ring, pkt, size / 4);
207 
208 	memset(&mes_status_pkt, 0, sizeof(mes_status_pkt));
209 	mes_status_pkt.header.type = MES_API_TYPE_SCHEDULER;
210 	mes_status_pkt.header.opcode = MES_SCH_API_QUERY_SCHEDULER_STATUS;
211 	mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
212 	mes_status_pkt.api_status.api_completion_fence_addr =
213 		ring->fence_drv.gpu_addr;
214 	mes_status_pkt.api_status.api_completion_fence_value = seq;
215 
216 	amdgpu_ring_write_multiple(ring, &mes_status_pkt,
217 				   sizeof(mes_status_pkt) / 4);
218 
219 	amdgpu_ring_commit(ring);
220 	spin_unlock_irqrestore(ring_lock, flags);
221 
222 	op_str = mes_v12_1_get_op_string(x_pkt);
223 	misc_op_str = mes_v12_1_get_misc_op_string(x_pkt);
224 
225 	if (misc_op_str)
226 		dev_dbg(adev->dev, "MES(%d, %d) msg=%s (%s) was emitted\n",
227 			xcc_id, pipe, op_str, misc_op_str);
228 	else if (op_str)
229 		dev_dbg(adev->dev, "MES(%d, %d) msg=%s was emitted\n",
230 			xcc_id, pipe, op_str);
231 	else
232 		dev_dbg(adev->dev, "MES(%d, %d) msg=%d was emitted\n",
233 			xcc_id, pipe, x_pkt->header.opcode);
234 
235 	r = amdgpu_fence_wait_polling(ring, seq, timeout);
236 	if (r < 1 || !*status_ptr) {
237 		if (misc_op_str)
238 			dev_err(adev->dev,
239 				"MES(%d, %d) failed to respond to msg=%s (%s) fence_wait_ret=%ld status=0x%x\n",
240 				xcc_id, pipe, op_str, misc_op_str, r, *status_ptr);
241 		else if (op_str)
242 			dev_err(adev->dev,
243 				"MES(%d, %d) failed to respond to msg=%s fence_wait_ret=%ld status=0x%x\n",
244 				xcc_id, pipe, op_str, r, *status_ptr);
245 		else
246 			dev_err(adev->dev,
247 				"MES(%d, %d) failed to respond to msg=%d fence_wait_ret=%ld status=0x%x\n",
248 				xcc_id, pipe, x_pkt->header.opcode, r, *status_ptr);
249 
250 		while (halt_if_hws_hang)
251 			schedule();
252 
253 		r = -ETIMEDOUT;
254 		goto error_wb_free;
255 	}
256 
257 	amdgpu_wb_free(adev, status_offset);
258 	return 0;
259 
260 error_undo:
261 	dev_err(adev->dev, "MES(%d, %d) ring buffer is full.\n", xcc_id, pipe);
262 	amdgpu_ring_undo(ring);
263 
264 error_unlock_free:
265 	spin_unlock_irqrestore(ring_lock, flags);
266 
267 error_wb_free:
268 	amdgpu_wb_free(adev, status_offset);
269 	return r;
270 }
271 
272 static int convert_to_mes_queue_type(int queue_type)
273 {
274 	if (queue_type == AMDGPU_RING_TYPE_GFX)
275 		return MES_QUEUE_TYPE_GFX;
276 	else if (queue_type == AMDGPU_RING_TYPE_COMPUTE)
277 		return MES_QUEUE_TYPE_COMPUTE;
278 	else if (queue_type == AMDGPU_RING_TYPE_SDMA)
279 		return MES_QUEUE_TYPE_SDMA;
280 	else if (queue_type == AMDGPU_RING_TYPE_MES)
281 		return MES_QUEUE_TYPE_SCHQ;
282 	else
283 		WARN(1, "Invalid queue type %d\n", queue_type);
284 	return MES_QUEUE_TYPE_GFX;
285 }
286 
287 static int mes_v12_1_add_hw_queue(struct amdgpu_mes *mes,
288 				  struct mes_add_queue_input *input)
289 {
290 	union MESAPI__ADD_QUEUE mes_add_queue_pkt;
291 	int xcc_id = input->xcc_id;
292 
293 	if (mes->enable_coop_mode)
294 		xcc_id = mes->master_xcc_ids[xcc_id];
295 
296 	memset(&mes_add_queue_pkt, 0, sizeof(mes_add_queue_pkt));
297 
298 	mes_add_queue_pkt.header.type = MES_API_TYPE_SCHEDULER;
299 	mes_add_queue_pkt.header.opcode = MES_SCH_API_ADD_QUEUE;
300 	mes_add_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
301 
302 	mes_add_queue_pkt.process_id = input->process_id;
303 	mes_add_queue_pkt.page_table_base_addr = input->page_table_base_addr;
304 	mes_add_queue_pkt.process_va_start = input->process_va_start;
305 	mes_add_queue_pkt.process_va_end = input->process_va_end;
306 	mes_add_queue_pkt.process_quantum = input->process_quantum;
307 	mes_add_queue_pkt.process_context_addr = input->process_context_addr;
308 	mes_add_queue_pkt.gang_quantum = input->gang_quantum;
309 	mes_add_queue_pkt.gang_context_addr = input->gang_context_addr;
310 	mes_add_queue_pkt.inprocess_gang_priority =
311 		input->inprocess_gang_priority;
312 	mes_add_queue_pkt.gang_global_priority_level =
313 		input->gang_global_priority_level;
314 	mes_add_queue_pkt.doorbell_offset = input->doorbell_offset;
315 	mes_add_queue_pkt.mqd_addr = input->mqd_addr;
316 
317 	mes_add_queue_pkt.wptr_addr = input->wptr_mc_addr;
318 
319 	mes_add_queue_pkt.queue_type =
320 		convert_to_mes_queue_type(input->queue_type);
321 	mes_add_queue_pkt.paging = input->paging;
322 	mes_add_queue_pkt.vm_context_cntl = input->vm_cntx_cntl;
323 	mes_add_queue_pkt.gws_base = input->gws_base;
324 	mes_add_queue_pkt.gws_size = input->gws_size;
325 	mes_add_queue_pkt.trap_handler_addr = input->tba_addr;
326 	mes_add_queue_pkt.tma_addr = input->tma_addr;
327 	mes_add_queue_pkt.trap_en = input->trap_en;
328 	mes_add_queue_pkt.skip_process_ctx_clear = input->skip_process_ctx_clear;
329 	mes_add_queue_pkt.is_kfd_process = input->is_kfd_process;
330 
331 	/* For KFD, gds_size is re-used for queue size (needed in MES for AQL queues) */
332 	mes_add_queue_pkt.is_aql_queue = input->is_aql_queue;
333 	mes_add_queue_pkt.gds_size = input->queue_size;
334 
335 	/* For KFD, gds_size is re-used for queue size (needed in MES for AQL queues) */
336 	mes_add_queue_pkt.is_aql_queue = input->is_aql_queue;
337 	mes_add_queue_pkt.gds_size = input->queue_size;
338 
339 	mes_add_queue_pkt.full_sh_mem_config_data = input->sh_mem_config_data;
340 
341 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
342 			xcc_id, AMDGPU_MES_SCHED_PIPE,
343 			&mes_add_queue_pkt, sizeof(mes_add_queue_pkt),
344 			offsetof(union MESAPI__ADD_QUEUE, api_status));
345 }
346 
347 static int mes_v12_1_remove_hw_queue(struct amdgpu_mes *mes,
348 				     struct mes_remove_queue_input *input)
349 {
350 	union MESAPI__REMOVE_QUEUE mes_remove_queue_pkt;
351 	int xcc_id = input->xcc_id;
352 
353 	if (mes->enable_coop_mode)
354 		xcc_id = mes->master_xcc_ids[xcc_id];
355 
356 	memset(&mes_remove_queue_pkt, 0, sizeof(mes_remove_queue_pkt));
357 
358 	mes_remove_queue_pkt.header.type = MES_API_TYPE_SCHEDULER;
359 	mes_remove_queue_pkt.header.opcode = MES_SCH_API_REMOVE_QUEUE;
360 	mes_remove_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
361 
362 	mes_remove_queue_pkt.doorbell_offset = input->doorbell_offset;
363 	mes_remove_queue_pkt.gang_context_addr = input->gang_context_addr;
364 	mes_remove_queue_pkt.queue_type =
365 		convert_to_mes_queue_type(input->queue_type);
366 
367 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
368 			xcc_id, AMDGPU_MES_SCHED_PIPE,
369 			&mes_remove_queue_pkt, sizeof(mes_remove_queue_pkt),
370 			offsetof(union MESAPI__REMOVE_QUEUE, api_status));
371 }
372 
373 static int mes_v12_1_reset_hw_queue(struct amdgpu_mes *mes,
374 				    struct mes_reset_queue_input *input)
375 {
376 	union MESAPI__RESET mes_reset_queue_pkt;
377 
378 	memset(&mes_reset_queue_pkt, 0, sizeof(mes_reset_queue_pkt));
379 
380 	mes_reset_queue_pkt.header.type = MES_API_TYPE_SCHEDULER;
381 	mes_reset_queue_pkt.header.opcode = MES_SCH_API_RESET;
382 	mes_reset_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
383 
384 	mes_reset_queue_pkt.doorbell_offset = input->doorbell_offset;
385 	/* mes_reset_queue_pkt.gang_context_addr = input->gang_context_addr; */
386 	mes_reset_queue_pkt.reset_queue_only = 1;
387 
388 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
389 			input->xcc_id, AMDGPU_MES_SCHED_PIPE,
390 			&mes_reset_queue_pkt, sizeof(mes_reset_queue_pkt),
391 			offsetof(union MESAPI__RESET, api_status));
392 }
393 
394 static int mes_v12_1_map_legacy_queue(struct amdgpu_mes *mes,
395 				      struct mes_map_legacy_queue_input *input)
396 {
397 	union MESAPI__ADD_QUEUE mes_add_queue_pkt;
398 	int pipe;
399 
400 	memset(&mes_add_queue_pkt, 0, sizeof(mes_add_queue_pkt));
401 
402 	mes_add_queue_pkt.header.type = MES_API_TYPE_SCHEDULER;
403 	mes_add_queue_pkt.header.opcode = MES_SCH_API_ADD_QUEUE;
404 	mes_add_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
405 
406 	mes_add_queue_pkt.pipe_id = input->pipe_id;
407 	mes_add_queue_pkt.queue_id = input->queue_id;
408 	mes_add_queue_pkt.doorbell_offset = input->doorbell_offset;
409 	mes_add_queue_pkt.mqd_addr = input->mqd_addr;
410 	mes_add_queue_pkt.wptr_addr = input->wptr_addr;
411 	mes_add_queue_pkt.queue_type =
412 		convert_to_mes_queue_type(input->queue_type);
413 	mes_add_queue_pkt.map_legacy_kq = 1;
414 
415 	/* Keep scheduler queue on KIQ pipe; map all other kernel queues on sched pipe. */
416 	if (input->queue_type == AMDGPU_RING_TYPE_MES)
417 		pipe = AMDGPU_MES_KIQ_PIPE;
418 	else
419 		pipe = AMDGPU_MES_SCHED_PIPE;
420 
421 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
422 			input->xcc_id, pipe,
423 			&mes_add_queue_pkt, sizeof(mes_add_queue_pkt),
424 			offsetof(union MESAPI__ADD_QUEUE, api_status));
425 }
426 
427 static int mes_v12_1_unmap_legacy_queue(struct amdgpu_mes *mes,
428 			struct mes_unmap_legacy_queue_input *input)
429 {
430 	union MESAPI__REMOVE_QUEUE mes_remove_queue_pkt;
431 	int pipe;
432 
433 	memset(&mes_remove_queue_pkt, 0, sizeof(mes_remove_queue_pkt));
434 
435 	mes_remove_queue_pkt.header.type = MES_API_TYPE_SCHEDULER;
436 	mes_remove_queue_pkt.header.opcode = MES_SCH_API_REMOVE_QUEUE;
437 	mes_remove_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
438 
439 	mes_remove_queue_pkt.doorbell_offset = input->doorbell_offset;
440 	mes_remove_queue_pkt.gang_context_addr = 0;
441 
442 	mes_remove_queue_pkt.pipe_id = input->pipe_id;
443 	mes_remove_queue_pkt.queue_id = input->queue_id;
444 
445 	if (input->action == PREEMPT_QUEUES_NO_UNMAP) {
446 		mes_remove_queue_pkt.preempt_legacy_gfx_queue = 1;
447 		mes_remove_queue_pkt.tf_addr = input->trail_fence_addr;
448 		mes_remove_queue_pkt.tf_data =
449 			lower_32_bits(input->trail_fence_data);
450 	} else {
451 		mes_remove_queue_pkt.unmap_legacy_queue = 1;
452 		mes_remove_queue_pkt.queue_type =
453 			convert_to_mes_queue_type(input->queue_type);
454 	}
455 
456 	/* Keep scheduler queue on KIQ pipe; map all other kernel queues on sched pipe. */
457 	if (input->queue_type == AMDGPU_RING_TYPE_MES)
458 		pipe = AMDGPU_MES_KIQ_PIPE;
459 	else
460 		pipe = AMDGPU_MES_SCHED_PIPE;
461 
462 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
463 			input->xcc_id, pipe,
464 			&mes_remove_queue_pkt, sizeof(mes_remove_queue_pkt),
465 			offsetof(union MESAPI__REMOVE_QUEUE, api_status));
466 }
467 
468 static int mes_v12_1_suspend_gang(struct amdgpu_mes *mes,
469 				  struct mes_suspend_gang_input *input)
470 {
471 	union MESAPI__SUSPEND mes_suspend_gang_pkt;
472 
473 	memset(&mes_suspend_gang_pkt, 0, sizeof(mes_suspend_gang_pkt));
474 
475 	mes_suspend_gang_pkt.header.type = MES_API_TYPE_SCHEDULER;
476 	mes_suspend_gang_pkt.header.opcode = MES_SCH_API_SUSPEND;
477 	mes_suspend_gang_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
478 
479 	mes_suspend_gang_pkt.suspend_all_gangs = input->suspend_all_gangs;
480 	mes_suspend_gang_pkt.suspend_all_sdma_gangs = input->suspend_all_sdma_gangs;
481 	mes_suspend_gang_pkt.gang_context_addr = input->gang_context_addr;
482 	mes_suspend_gang_pkt.suspend_fence_addr = input->suspend_fence_addr;
483 	mes_suspend_gang_pkt.suspend_fence_value = input->suspend_fence_value;
484 	mes_suspend_gang_pkt.doorbell_offset = input->doorbell_offset;
485 
486 	/* Suspend gang is handled by master MES */
487 	return mes_v12_1_submit_pkt_and_poll_completion(mes, input->xcc_id, AMDGPU_MES_SCHED_PIPE,
488 			&mes_suspend_gang_pkt, sizeof(mes_suspend_gang_pkt),
489 			offsetof(union MESAPI__SUSPEND, api_status));
490 }
491 
492 static int mes_v12_1_resume_gang(struct amdgpu_mes *mes,
493 				 struct mes_resume_gang_input *input)
494 {
495 	union MESAPI__RESUME mes_resume_gang_pkt;
496 
497 	memset(&mes_resume_gang_pkt, 0, sizeof(mes_resume_gang_pkt));
498 
499 	mes_resume_gang_pkt.header.type = MES_API_TYPE_SCHEDULER;
500 	mes_resume_gang_pkt.header.opcode = MES_SCH_API_RESUME;
501 	mes_resume_gang_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
502 
503 	mes_resume_gang_pkt.resume_all_gangs = input->resume_all_gangs;
504 	mes_resume_gang_pkt.gang_context_addr = input->gang_context_addr;
505 	mes_resume_gang_pkt.doorbell_offset = input->doorbell_offset;
506 
507 	/* Resume gang is handled by master MES */
508 	return mes_v12_1_submit_pkt_and_poll_completion(mes, input->xcc_id, AMDGPU_MES_SCHED_PIPE,
509 			&mes_resume_gang_pkt, sizeof(mes_resume_gang_pkt),
510 			offsetof(union MESAPI__RESUME, api_status));
511 }
512 
513 static int mes_v12_1_query_sched_status(struct amdgpu_mes *mes,
514 					  int pipe, int xcc_id)
515 {
516 	union MESAPI__QUERY_MES_STATUS mes_status_pkt;
517 
518 	memset(&mes_status_pkt, 0, sizeof(mes_status_pkt));
519 
520 	mes_status_pkt.header.type = MES_API_TYPE_SCHEDULER;
521 	mes_status_pkt.header.opcode = MES_SCH_API_QUERY_SCHEDULER_STATUS;
522 	mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
523 
524 	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
525 			&mes_status_pkt, sizeof(mes_status_pkt),
526 			offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
527 }
528 static uint32_t mes_v12_1_get_xcc_from_reg(uint32_t reg_offset)
529 {
530 	return ((reg_offset >> 16) & 0x7);
531 }
532 
533 static void mes_v12_1_get_rrmt(uint32_t reg, uint32_t xcc_id,
534 			       struct RRMT_OPTION *rrmt_opt,
535 			       uint32_t *out_reg)
536 {
537 	uint32_t normalized_reg = soc_v1_0_normalize_xcc_reg_offset(reg);
538 
539 	if (soc_v1_0_normalize_xcc_reg_range(normalized_reg)) {
540 		rrmt_opt->xcd_die_id = mes_v12_1_get_xcc_from_reg(reg);
541 		rrmt_opt->mode = (xcc_id == rrmt_opt->xcd_die_id) ?
542 			 MES_RRMT_MODE_LOCAL_XCD : MES_RRMT_MODE_REMOTE_XCD;
543 	} else {
544 		rrmt_opt->mode = MES_RRMT_MODE_REMOTE_MID;
545 		if (soc_v1_0_mid1_reg_range(reg))
546 			rrmt_opt->mid_die_id = 1;
547 	}
548 
549 	*out_reg = soc_v1_0_normalize_reg_offset(reg);
550 }
551 
552 static int mes_v12_1_misc_op(struct amdgpu_mes *mes,
553 			     struct mes_misc_op_input *input)
554 {
555 	struct amdgpu_device *adev = mes->adev;
556 	union MESAPI__MISC misc_pkt;
557 	int pipe;
558 
559 	/*OP_WRM_REG_WR_WAIT is used to do tlb invalidation which need to be handled in sched pipe for gfx_12_1.*/
560 	if (input->op != MES_MISC_OP_WRM_REG_WR_WAIT)
561 		pipe = AMDGPU_MES_KIQ_PIPE;
562 	else
563 		pipe = AMDGPU_MES_SCHED_PIPE;
564 
565 	memset(&misc_pkt, 0, sizeof(misc_pkt));
566 
567 	misc_pkt.header.type = MES_API_TYPE_SCHEDULER;
568 	misc_pkt.header.opcode = MES_SCH_API_MISC;
569 	misc_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
570 
571 	switch (input->op) {
572 	case MES_MISC_OP_READ_REG:
573 		misc_pkt.opcode = MESAPI_MISC__READ_REG;
574 		misc_pkt.read_reg.buffer_addr = input->read_reg.buffer_addr;
575 		mes_v12_1_get_rrmt(input->read_reg.reg_offset,
576 				   GET_INST(GC, input->xcc_id),
577 				   &misc_pkt.read_reg.rrmt_opt,
578 				   &misc_pkt.read_reg.reg_offset);
579 		break;
580 	case MES_MISC_OP_WRITE_REG:
581 		misc_pkt.opcode = MESAPI_MISC__WRITE_REG;
582 		misc_pkt.write_reg.reg_value = input->write_reg.reg_value;
583 		mes_v12_1_get_rrmt(input->write_reg.reg_offset,
584 				   GET_INST(GC, input->xcc_id),
585 				   &misc_pkt.write_reg.rrmt_opt,
586 				   &misc_pkt.write_reg.reg_offset);
587 		break;
588 	case MES_MISC_OP_WRM_REG_WAIT:
589 		misc_pkt.opcode = MESAPI_MISC__WAIT_REG_MEM;
590 		misc_pkt.wait_reg_mem.op = WRM_OPERATION__WAIT_REG_MEM;
591 		misc_pkt.wait_reg_mem.reference = input->wrm_reg.ref;
592 		misc_pkt.wait_reg_mem.mask = input->wrm_reg.mask;
593 		misc_pkt.wait_reg_mem.reg_offset2 = 0;
594 		mes_v12_1_get_rrmt(input->wrm_reg.reg0,
595 				   GET_INST(GC, input->xcc_id),
596 				   &misc_pkt.wait_reg_mem.rrmt_opt1,
597 				   &misc_pkt.wait_reg_mem.reg_offset1);
598 		break;
599 	case MES_MISC_OP_WRM_REG_WR_WAIT:
600 		misc_pkt.opcode = MESAPI_MISC__WAIT_REG_MEM;
601 		misc_pkt.wait_reg_mem.op = WRM_OPERATION__WR_WAIT_WR_REG;
602 		misc_pkt.wait_reg_mem.reference = input->wrm_reg.ref;
603 		misc_pkt.wait_reg_mem.mask = input->wrm_reg.mask;
604 		mes_v12_1_get_rrmt(input->wrm_reg.reg0,
605 				   GET_INST(GC, input->xcc_id),
606 				   &misc_pkt.wait_reg_mem.rrmt_opt1,
607 				   &misc_pkt.wait_reg_mem.reg_offset1);
608 		mes_v12_1_get_rrmt(input->wrm_reg.reg1,
609 				   GET_INST(GC, input->xcc_id),
610 				   &misc_pkt.wait_reg_mem.rrmt_opt2,
611 				   &misc_pkt.wait_reg_mem.reg_offset2);
612 		break;
613 	case MES_MISC_OP_SET_SHADER_DEBUGGER:
614 		pipe = AMDGPU_MES_SCHED_PIPE;
615 		misc_pkt.opcode = MESAPI_MISC__SET_SHADER_DEBUGGER;
616 		misc_pkt.set_shader_debugger.process_context_addr =
617 				input->set_shader_debugger.process_context_addr;
618 		misc_pkt.set_shader_debugger.flags.u32all =
619 				input->set_shader_debugger.flags.u32all;
620 		misc_pkt.set_shader_debugger.spi_gdbg_per_vmid_cntl =
621 				input->set_shader_debugger.spi_gdbg_per_vmid_cntl;
622 		memcpy(misc_pkt.set_shader_debugger.tcp_watch_cntl,
623 				input->set_shader_debugger.tcp_watch_cntl,
624 				sizeof(misc_pkt.set_shader_debugger.tcp_watch_cntl));
625 		misc_pkt.set_shader_debugger.trap_en = input->set_shader_debugger.trap_en;
626 		break;
627 	case MES_MISC_OP_CHANGE_CONFIG:
628 		misc_pkt.opcode = MESAPI_MISC__CHANGE_CONFIG;
629 		misc_pkt.change_config.opcode =
630 			MESAPI_MISC__CHANGE_CONFIG_OPTION_LIMIT_SINGLE_PROCESS;
631 		misc_pkt.change_config.option.bits.limit_single_process =
632 			input->change_config.option.limit_single_process;
633 		break;
634 	default:
635 		DRM_ERROR("unsupported misc op (%d) \n", input->op);
636 		return -EINVAL;
637 	}
638 
639 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
640 			input->xcc_id, pipe,
641 			&misc_pkt, sizeof(misc_pkt),
642 			offsetof(union MESAPI__MISC, api_status));
643 }
644 
645 static int mes_v12_1_set_hw_resources_1(struct amdgpu_mes *mes,
646 					  int pipe, int xcc_id)
647 {
648 	union MESAPI_SET_HW_RESOURCES_1 mes_set_hw_res_1_pkt;
649 	int master_xcc_id;
650 
651 	memset(&mes_set_hw_res_1_pkt, 0, sizeof(mes_set_hw_res_1_pkt));
652 
653 	mes_set_hw_res_1_pkt.header.type = MES_API_TYPE_SCHEDULER;
654 	mes_set_hw_res_1_pkt.header.opcode = MES_SCH_API_SET_HW_RSRC_1;
655 	mes_set_hw_res_1_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
656 	mes_set_hw_res_1_pkt.mes_kiq_unmap_timeout = 100;
657 
658 	/* From version 0x74 above, pipe1 support use shared command buffer
659  	   to distribute some tasks on individual XCCs*/
660 	if (mes->enable_coop_mode &&
661 	    ((pipe == AMDGPU_MES_SCHED_PIPE) ||
662 	    ((mes->kiq_version & AMDGPU_MES_VERSION_MASK) >= 0x74))) {
663 		master_xcc_id = mes->master_xcc_ids[xcc_id];
664 		mes_set_hw_res_1_pkt.mes_coop_mode = 1;
665 		mes_set_hw_res_1_pkt.coop_sch_shared_mc_addr =
666 			mes->shared_cmd_buf_gpu_addr[MES_PIPE_INST(master_xcc_id, pipe)];
667 	}
668 
669 	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
670 			&mes_set_hw_res_1_pkt, sizeof(mes_set_hw_res_1_pkt),
671 			offsetof(union MESAPI_SET_HW_RESOURCES_1, api_status));
672 }
673 
674 static int mes_v12_1_set_hw_resources(struct amdgpu_mes *mes,
675 					int pipe, int xcc_id)
676 {
677 	int i, status;
678 	struct amdgpu_device *adev = mes->adev;
679 	union MESAPI_SET_HW_RESOURCES mes_set_hw_res_pkt;
680 
681 	memset(&mes_set_hw_res_pkt, 0, sizeof(mes_set_hw_res_pkt));
682 
683 	mes_set_hw_res_pkt.header.type = MES_API_TYPE_SCHEDULER;
684 	mes_set_hw_res_pkt.header.opcode = MES_SCH_API_SET_HW_RSRC;
685 	mes_set_hw_res_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
686 
687 	if (pipe == AMDGPU_MES_SCHED_PIPE) {
688 		mes_set_hw_res_pkt.vmid_mask_mmhub = adev->vm_manager.vmid_uq_mask_mmhub;
689 		mes_set_hw_res_pkt.vmid_mask_gfxhub = adev->vm_manager.vmid_uq_mask_gfxhub;
690 		mes_set_hw_res_pkt.gds_size = adev->gds.gds_size;
691 		mes_set_hw_res_pkt.paging_vmid = 0;
692 
693 		for (i = 0; i < MAX_COMPUTE_PIPES; i++)
694 			mes_set_hw_res_pkt.compute_hqd_mask[i] =
695 				mes->compute_hqd_mask[i];
696 
697 		for (i = 0; i < MAX_GFX_PIPES; i++)
698 			mes_set_hw_res_pkt.gfx_hqd_mask[i] =
699 				mes->gfx_hqd_mask[i];
700 
701 		for (i = 0; i < MAX_SDMA_PIPES; i++)
702 			mes_set_hw_res_pkt.sdma_hqd_mask[i] =
703 				mes->sdma_hqd_mask[i];
704 
705 		for (i = 0; i < AMD_PRIORITY_NUM_LEVELS; i++)
706 			mes_set_hw_res_pkt.aggregated_doorbells[i] =
707 				mes->aggregated_doorbells[i];
708 	}
709 
710 	mes_set_hw_res_pkt.g_sch_ctx_gpu_mc_ptr =
711 		mes->sch_ctx_gpu_addr[pipe];
712 	mes_set_hw_res_pkt.query_status_fence_gpu_mc_ptr =
713 		mes->query_status_fence_gpu_addr[pipe];
714 
715 	for (i = 0; i < 5; i++) {
716 		mes_set_hw_res_pkt.gc_base[i] =
717 			adev->reg_offset[GC_HWIP][0][i];
718 		mes_set_hw_res_pkt.mmhub_base[i] =
719 				adev->reg_offset[MMHUB_HWIP][0][i];
720 		mes_set_hw_res_pkt.osssys_base[i] =
721 		adev->reg_offset[OSSSYS_HWIP][0][i];
722 	}
723 
724 	mes_set_hw_res_pkt.disable_reset = 1;
725 	mes_set_hw_res_pkt.disable_mes_log = 1;
726 	mes_set_hw_res_pkt.use_different_vmid_compute = 1;
727 	mes_set_hw_res_pkt.enable_reg_active_poll = 1;
728 	mes_set_hw_res_pkt.enable_level_process_quantum_check = 1;
729 	/* proceeds pipe reset if queue reset fails */
730 	mes_set_hw_res_pkt.enable_compute_pipe_reset = 1;
731 
732 	/*
733 	 * Keep oversubscribe timer for sdma . When we have unmapped doorbell
734 	 * handling support, other queue will not use the oversubscribe timer.
735 	 * handling  mode - 0: disabled; 1: basic version; 2: basic+ version
736 	 */
737 	mes_set_hw_res_pkt.oversubscription_timer = 50;
738 	mes_set_hw_res_pkt.unmapped_doorbell_handling = 1;
739 
740 	if (amdgpu_mes_log_enable) {
741 		mes_set_hw_res_pkt.enable_mes_event_int_logging = 1;
742 		mes_set_hw_res_pkt.event_intr_history_gpu_mc_ptr =
743 			mes->event_log_gpu_addr + MES_PIPE_INST(xcc_id, pipe) * AMDGPU_MES_LOG_BUFFER_SIZE;
744 	}
745 
746 	if (adev->enforce_isolation[0] == AMDGPU_ENFORCE_ISOLATION_ENABLE)
747 		mes_set_hw_res_pkt.limit_single_process = 1;
748 
749 	status = mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
750 			&mes_set_hw_res_pkt, sizeof(mes_set_hw_res_pkt),
751 			offsetof(union MESAPI_SET_HW_RESOURCES, api_status));
752 
753 	/* get MES scheduler versions */
754 	mutex_lock(&adev->srbm_mutex);
755 	soc_v1_0_grbm_select(adev, 3, pipe, 0, 0, GET_INST(GC, xcc_id));
756 
757 	if (pipe == AMDGPU_MES_SCHED_PIPE)
758 		adev->mes.sched_version = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_GP3_LO);
759 	else if (pipe == AMDGPU_MES_KIQ_PIPE)
760 		adev->mes.kiq_version = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_GP3_LO);
761 
762 	soc_v1_0_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
763 	mutex_unlock(&adev->srbm_mutex);
764 
765 	return status;
766 }
767 
768 static void mes_v12_1_init_aggregated_doorbell(struct amdgpu_mes *mes,
769 						 int xcc_id)
770 {
771 	struct amdgpu_device *adev = mes->adev;
772 	uint32_t data;
773 
774 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL1);
775 	data &= ~(CP_MES_DOORBELL_CONTROL1__DOORBELL_OFFSET_MASK |
776 		  CP_MES_DOORBELL_CONTROL1__DOORBELL_EN_MASK |
777 		  CP_MES_DOORBELL_CONTROL1__DOORBELL_HIT_MASK);
778 	data |= mes->aggregated_doorbells[AMDGPU_MES_PRIORITY_LEVEL_LOW] <<
779 		CP_MES_DOORBELL_CONTROL1__DOORBELL_OFFSET__SHIFT;
780 	data |= 1 << CP_MES_DOORBELL_CONTROL1__DOORBELL_EN__SHIFT;
781 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL1, data);
782 
783 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL2);
784 	data &= ~(CP_MES_DOORBELL_CONTROL2__DOORBELL_OFFSET_MASK |
785 		  CP_MES_DOORBELL_CONTROL2__DOORBELL_EN_MASK |
786 		  CP_MES_DOORBELL_CONTROL2__DOORBELL_HIT_MASK);
787 	data |= mes->aggregated_doorbells[AMDGPU_MES_PRIORITY_LEVEL_NORMAL] <<
788 		CP_MES_DOORBELL_CONTROL2__DOORBELL_OFFSET__SHIFT;
789 	data |= 1 << CP_MES_DOORBELL_CONTROL2__DOORBELL_EN__SHIFT;
790 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL2, data);
791 
792 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL3);
793 	data &= ~(CP_MES_DOORBELL_CONTROL3__DOORBELL_OFFSET_MASK |
794 		  CP_MES_DOORBELL_CONTROL3__DOORBELL_EN_MASK |
795 		  CP_MES_DOORBELL_CONTROL3__DOORBELL_HIT_MASK);
796 	data |= mes->aggregated_doorbells[AMDGPU_MES_PRIORITY_LEVEL_MEDIUM] <<
797 		CP_MES_DOORBELL_CONTROL3__DOORBELL_OFFSET__SHIFT;
798 	data |= 1 << CP_MES_DOORBELL_CONTROL3__DOORBELL_EN__SHIFT;
799 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL3, data);
800 
801 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL4);
802 	data &= ~(CP_MES_DOORBELL_CONTROL4__DOORBELL_OFFSET_MASK |
803 		  CP_MES_DOORBELL_CONTROL4__DOORBELL_EN_MASK |
804 		  CP_MES_DOORBELL_CONTROL4__DOORBELL_HIT_MASK);
805 	data |= mes->aggregated_doorbells[AMDGPU_MES_PRIORITY_LEVEL_HIGH] <<
806 		CP_MES_DOORBELL_CONTROL4__DOORBELL_OFFSET__SHIFT;
807 	data |= 1 << CP_MES_DOORBELL_CONTROL4__DOORBELL_EN__SHIFT;
808 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL4, data);
809 
810 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL5);
811 	data &= ~(CP_MES_DOORBELL_CONTROL5__DOORBELL_OFFSET_MASK |
812 		  CP_MES_DOORBELL_CONTROL5__DOORBELL_EN_MASK |
813 		  CP_MES_DOORBELL_CONTROL5__DOORBELL_HIT_MASK);
814 	data |= mes->aggregated_doorbells[AMDGPU_MES_PRIORITY_LEVEL_REALTIME] <<
815 		CP_MES_DOORBELL_CONTROL5__DOORBELL_OFFSET__SHIFT;
816 	data |= 1 << CP_MES_DOORBELL_CONTROL5__DOORBELL_EN__SHIFT;
817 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_DOORBELL_CONTROL5, data);
818 
819 	data = 1 << CP_HQD_GFX_CONTROL__DB_UPDATED_MSG_EN__SHIFT;
820 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_GFX_CONTROL, data);
821 }
822 
823 
824 static void mes_v12_1_enable_unmapped_doorbell_handling(
825 	struct amdgpu_mes *mes, bool enable, int xcc_id)
826 {
827 	struct amdgpu_device *adev = mes->adev;
828 	uint32_t data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_UNMAPPED_DOORBELL);
829 
830 	/*
831 	 * The default PROC_LSB settng is 0xc which means doorbell
832 	 * addr[16:12] gives the doorbell page number. For kfd, each
833 	 * process will use 2 pages of doorbell, we need to change the
834 	 * setting to 0xd
835 	 */
836 	data &= ~CP_UNMAPPED_DOORBELL__PROC_LSB_MASK;
837 	data |= 0xd <<  CP_UNMAPPED_DOORBELL__PROC_LSB__SHIFT;
838 
839 	data |= (enable ? 1 : 0) << CP_UNMAPPED_DOORBELL__ENABLE__SHIFT;
840 
841 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_UNMAPPED_DOORBELL, data);
842 }
843 
844 #if 0
845 static int mes_v12_1_reset_legacy_queue(struct amdgpu_mes *mes,
846 					struct mes_reset_legacy_queue_input *input)
847 {
848 	union MESAPI__RESET mes_reset_queue_pkt;
849 	int pipe;
850 
851 	memset(&mes_reset_queue_pkt, 0, sizeof(mes_reset_queue_pkt));
852 
853 	mes_reset_queue_pkt.header.type = MES_API_TYPE_SCHEDULER;
854 	mes_reset_queue_pkt.header.opcode = MES_SCH_API_RESET;
855 	mes_reset_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
856 
857 	mes_reset_queue_pkt.queue_type =
858 		convert_to_mes_queue_type(input->queue_type);
859 
860 	if (mes_reset_queue_pkt.queue_type == MES_QUEUE_TYPE_GFX) {
861 		mes_reset_queue_pkt.reset_legacy_gfx = 1;
862 		mes_reset_queue_pkt.pipe_id_lp = input->pipe_id;
863 		mes_reset_queue_pkt.queue_id_lp = input->queue_id;
864 		mes_reset_queue_pkt.mqd_mc_addr_lp = input->mqd_addr;
865 		mes_reset_queue_pkt.doorbell_offset_lp = input->doorbell_offset;
866 		mes_reset_queue_pkt.wptr_addr_lp = input->wptr_addr;
867 		mes_reset_queue_pkt.vmid_id_lp = input->vmid;
868 	} else {
869 		mes_reset_queue_pkt.reset_queue_only = 1;
870 		mes_reset_queue_pkt.doorbell_offset = input->doorbell_offset;
871 	}
872 
873 	pipe = AMDGPU_MES_KIQ_PIPE;
874 
875 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
876 			input->xcc_id, pipe,
877 			&mes_reset_queue_pkt, sizeof(mes_reset_queue_pkt),
878 			offsetof(union MESAPI__RESET, api_status));
879 }
880 #endif
881 
882 static int mes_v12_1_detect_and_reset_hung_queues(struct amdgpu_mes *mes,
883 						  struct mes_detect_and_reset_queue_input *input)
884 {
885 	union MESAPI__RESET mes_reset_queue_pkt;
886 
887 	memset(&mes_reset_queue_pkt, 0, sizeof(mes_reset_queue_pkt));
888 
889 	mes_reset_queue_pkt.header.type = MES_API_TYPE_SCHEDULER;
890 	mes_reset_queue_pkt.header.opcode = MES_SCH_API_RESET;
891 	mes_reset_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
892 
893 	mes_reset_queue_pkt.queue_type =
894 		convert_to_mes_queue_type(input->queue_type);
895 	mes_reset_queue_pkt.doorbell_offset_addr =
896 		mes->hung_queue_db_array_gpu_addr[0];
897 
898 	if (input->detect_only)
899 		mes_reset_queue_pkt.hang_detect_only = 1;
900 	else
901 		mes_reset_queue_pkt.hang_detect_then_reset = 1;
902 
903 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
904 			input->xcc_id, AMDGPU_MES_SCHED_PIPE,
905 			&mes_reset_queue_pkt, sizeof(mes_reset_queue_pkt),
906 			offsetof(union MESAPI__RESET, api_status));
907 }
908 
909 static int mes_v12_inv_tlb_convert_hub_id(uint8_t id)
910 {
911 	/*
912 	 * MES doesn't support invalidate gc_hub on slave xcc individually
913 	 * master xcc will invalidate all gc_hub for the partition
914 	 */
915 	if (AMDGPU_IS_GFXHUB(id))
916 		return 0;
917 	else if (AMDGPU_IS_MMHUB0(id))
918 		return 1;
919 	else if (AMDGPU_IS_MMHUB1(id))
920 		return 2;
921 	return -EINVAL;
922 
923 }
924 
925 static int mes_v12_1_inv_tlbs_pasid(struct amdgpu_mes *mes,
926 				    struct mes_inv_tlbs_pasid_input *input)
927 {
928 	union MESAPI__INV_TLBS mes_inv_tlbs;
929 	int xcc_id = input->xcc_id;
930 	int ret;
931 
932 	/* See mes_v12_1_add_hw_queue() for the indexing rationale. */
933 	if (mes->enable_coop_mode)
934 		xcc_id = mes->master_xcc_ids[xcc_id];
935 
936 	memset(&mes_inv_tlbs, 0, sizeof(mes_inv_tlbs));
937 
938 	mes_inv_tlbs.header.type = MES_API_TYPE_SCHEDULER;
939 	mes_inv_tlbs.header.opcode = MES_SCH_API_INV_TLBS;
940 	mes_inv_tlbs.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
941 
942 	mes_inv_tlbs.invalidate_tlbs.inv_sel = 0;
943 	mes_inv_tlbs.invalidate_tlbs.flush_type = input->flush_type;
944 	mes_inv_tlbs.invalidate_tlbs.inv_sel_id = input->pasid;
945 
946 	/*convert amdgpu_mes_hub_id to mes expected hub_id */
947 	ret = mes_v12_inv_tlb_convert_hub_id(input->hub_id);
948 	if (ret < 0)
949 		return -EINVAL;
950 	mes_inv_tlbs.invalidate_tlbs.hub_id = ret;
951 	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, AMDGPU_MES_SCHED_PIPE,
952 			&mes_inv_tlbs, sizeof(mes_inv_tlbs),
953 			offsetof(union MESAPI__INV_TLBS, api_status));
954 
955 }
956 
957 static const struct amdgpu_mes_funcs mes_v12_1_funcs = {
958 	.add_hw_queue = mes_v12_1_add_hw_queue,
959 	.remove_hw_queue = mes_v12_1_remove_hw_queue,
960 	.map_legacy_queue = mes_v12_1_map_legacy_queue,
961 	.unmap_legacy_queue = mes_v12_1_unmap_legacy_queue,
962 	.suspend_gang = mes_v12_1_suspend_gang,
963 	.resume_gang = mes_v12_1_resume_gang,
964 	.misc_op = mes_v12_1_misc_op,
965 	.reset_hw_queue = mes_v12_1_reset_hw_queue,
966 	.detect_and_reset_hung_queues = mes_v12_1_detect_and_reset_hung_queues,
967 	.invalidate_tlbs_pasid = mes_v12_1_inv_tlbs_pasid,
968 };
969 
970 static int mes_v12_1_allocate_ucode_buffer(struct amdgpu_device *adev,
971 					     enum amdgpu_mes_pipe pipe,
972 					     int xcc_id)
973 {
974 	int r, inst = MES_PIPE_INST(xcc_id, pipe);
975 	const struct mes_firmware_header_v1_0 *mes_hdr;
976 	const __le32 *fw_data;
977 	unsigned fw_size;
978 
979 	mes_hdr = (const struct mes_firmware_header_v1_0 *)
980 		adev->mes.fw[pipe]->data;
981 
982 	fw_data = (const __le32 *)(adev->mes.fw[pipe]->data +
983 		   le32_to_cpu(mes_hdr->mes_ucode_offset_bytes));
984 	fw_size = le32_to_cpu(mes_hdr->mes_ucode_size_bytes);
985 
986 	r = amdgpu_bo_create_reserved(adev, fw_size,
987 				      PAGE_SIZE,
988 				      AMDGPU_GEM_DOMAIN_VRAM,
989 				      &adev->mes.ucode_fw_obj[inst],
990 				      &adev->mes.ucode_fw_gpu_addr[inst],
991 				      (void **)&adev->mes.ucode_fw_ptr[inst]);
992 	if (r) {
993 		dev_err(adev->dev, "(%d) failed to create mes fw bo\n", r);
994 		return r;
995 	}
996 
997 	memcpy(adev->mes.ucode_fw_ptr[inst], fw_data, fw_size);
998 
999 	amdgpu_bo_kunmap(adev->mes.ucode_fw_obj[inst]);
1000 	amdgpu_bo_unreserve(adev->mes.ucode_fw_obj[inst]);
1001 
1002 	return 0;
1003 }
1004 
1005 static int mes_v12_1_allocate_ucode_data_buffer(struct amdgpu_device *adev,
1006 						  enum amdgpu_mes_pipe pipe,
1007 						  int xcc_id)
1008 {
1009 	int r, inst = MES_PIPE_INST(xcc_id, pipe);
1010 	const struct mes_firmware_header_v1_0 *mes_hdr;
1011 	const __le32 *fw_data;
1012 	unsigned fw_size;
1013 
1014 	mes_hdr = (const struct mes_firmware_header_v1_0 *)
1015 		adev->mes.fw[pipe]->data;
1016 
1017 	fw_data = (const __le32 *)(adev->mes.fw[pipe]->data +
1018 		   le32_to_cpu(mes_hdr->mes_ucode_data_offset_bytes));
1019 	fw_size = le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes);
1020 
1021 	r = amdgpu_bo_create_reserved(adev, fw_size,
1022 				      64 * 1024,
1023 				      AMDGPU_GEM_DOMAIN_VRAM,
1024 				      &adev->mes.data_fw_obj[inst],
1025 				      &adev->mes.data_fw_gpu_addr[inst],
1026 				      (void **)&adev->mes.data_fw_ptr[inst]);
1027 	if (r) {
1028 		dev_err(adev->dev, "(%d) failed to create mes data fw bo\n", r);
1029 		return r;
1030 	}
1031 
1032 	memcpy(adev->mes.data_fw_ptr[inst], fw_data, fw_size);
1033 
1034 	amdgpu_bo_kunmap(adev->mes.data_fw_obj[inst]);
1035 	amdgpu_bo_unreserve(adev->mes.data_fw_obj[inst]);
1036 
1037 	return 0;
1038 }
1039 
1040 static void mes_v12_1_free_ucode_buffers(struct amdgpu_device *adev,
1041 					   enum amdgpu_mes_pipe pipe,
1042 					   int xcc_id)
1043 {
1044 	int inst = MES_PIPE_INST(xcc_id, pipe);
1045 
1046 	amdgpu_bo_free_kernel(&adev->mes.data_fw_obj[inst],
1047 			      &adev->mes.data_fw_gpu_addr[inst],
1048 			      (void **)&adev->mes.data_fw_ptr[inst]);
1049 
1050 	amdgpu_bo_free_kernel(&adev->mes.ucode_fw_obj[inst],
1051 			      &adev->mes.ucode_fw_gpu_addr[inst],
1052 			      (void **)&adev->mes.ucode_fw_ptr[inst]);
1053 }
1054 
1055 static void mes_v12_1_enable(struct amdgpu_device *adev,
1056 			       bool enable, int xcc_id)
1057 {
1058 	uint64_t ucode_addr;
1059 	uint32_t pipe, data = 0;
1060 
1061 	if (enable) {
1062 		data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_CNTL);
1063 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_PIPE0_RESET, 1);
1064 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_PIPE1_RESET, 1);
1065 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_CNTL, data);
1066 
1067 		mutex_lock(&adev->srbm_mutex);
1068 		for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++) {
1069 			soc_v1_0_grbm_select(adev, 3, pipe, 0, 0,
1070 					     GET_INST(GC, xcc_id));
1071 
1072 			ucode_addr = adev->mes.uc_start_addr[pipe] >> 2;
1073 			WREG32_SOC15(GC, GET_INST(GC, xcc_id),
1074 				     regCP_MES_PRGRM_CNTR_START,
1075 				     lower_32_bits(ucode_addr));
1076 			WREG32_SOC15(GC, GET_INST(GC, xcc_id),
1077 				     regCP_MES_PRGRM_CNTR_START_HI,
1078 				     upper_32_bits(ucode_addr));
1079 		}
1080 		soc_v1_0_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
1081 		mutex_unlock(&adev->srbm_mutex);
1082 
1083 		/* unhalt MES and activate pipe0 */
1084 		data = REG_SET_FIELD(0, CP_MES_CNTL, MES_PIPE0_ACTIVE, 1);
1085 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_PIPE1_ACTIVE, 1);
1086 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_CNTL, data);
1087 
1088 		if (amdgpu_emu_mode)
1089 			msleep(500);
1090 		else
1091 			udelay(500);
1092 	} else {
1093 		data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_CNTL);
1094 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_PIPE0_ACTIVE, 0);
1095 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_PIPE1_ACTIVE, 0);
1096 		data = REG_SET_FIELD(data, CP_MES_CNTL,
1097 				     MES_INVALIDATE_ICACHE, 1);
1098 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_PIPE0_RESET, 1);
1099 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_PIPE1_RESET, 1);
1100 		data = REG_SET_FIELD(data, CP_MES_CNTL, MES_HALT, 1);
1101 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_CNTL, data);
1102 	}
1103 }
1104 
1105 static void mes_v12_1_set_ucode_start_addr(struct amdgpu_device *adev,
1106 					     int xcc_id)
1107 {
1108 	uint64_t ucode_addr;
1109 	int pipe;
1110 
1111 	mes_v12_1_enable(adev, false, xcc_id);
1112 
1113 	mutex_lock(&adev->srbm_mutex);
1114 	for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++) {
1115 		/* me=3, queue=0 */
1116 		soc_v1_0_grbm_select(adev, 3, pipe, 0, 0, GET_INST(GC, xcc_id));
1117 
1118 		/* set ucode start address */
1119 		ucode_addr = adev->mes.uc_start_addr[pipe] >> 2;
1120 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_PRGRM_CNTR_START,
1121 				lower_32_bits(ucode_addr));
1122 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_PRGRM_CNTR_START_HI,
1123 				upper_32_bits(ucode_addr));
1124 
1125 		soc_v1_0_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
1126 	}
1127 	mutex_unlock(&adev->srbm_mutex);
1128 }
1129 
1130 /* This function is for backdoor MES firmware */
1131 static int mes_v12_1_load_microcode(struct amdgpu_device *adev,
1132 				      enum amdgpu_mes_pipe pipe,
1133 				      bool prime_icache, int xcc_id)
1134 {
1135 	int r, inst = MES_PIPE_INST(xcc_id, pipe);
1136 	uint32_t data;
1137 
1138 	mes_v12_1_enable(adev, false, xcc_id);
1139 
1140 	if (!adev->mes.fw[pipe])
1141 		return -EINVAL;
1142 
1143 	r = mes_v12_1_allocate_ucode_buffer(adev, pipe, xcc_id);
1144 	if (r)
1145 		return r;
1146 
1147 	r = mes_v12_1_allocate_ucode_data_buffer(adev, pipe, xcc_id);
1148 	if (r) {
1149 		mes_v12_1_free_ucode_buffers(adev, pipe, xcc_id);
1150 		return r;
1151 	}
1152 
1153 	mutex_lock(&adev->srbm_mutex);
1154 	/* me=3, pipe=0, queue=0 */
1155 	soc_v1_0_grbm_select(adev, 3, pipe, 0, 0, GET_INST(GC, xcc_id));
1156 
1157 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_IC_BASE_CNTL, 0);
1158 
1159 	/* set ucode fimrware address */
1160 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_IC_BASE_LO,
1161 		     lower_32_bits(adev->mes.ucode_fw_gpu_addr[inst]));
1162 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_IC_BASE_HI,
1163 		     upper_32_bits(adev->mes.ucode_fw_gpu_addr[inst]));
1164 
1165 	/* set ucode instruction cache boundary to 2M-1 */
1166 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_MIBOUND_LO, 0x1FFFFF);
1167 
1168 	/* set ucode data firmware address */
1169 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_MDBASE_LO,
1170 		     lower_32_bits(adev->mes.data_fw_gpu_addr[inst]));
1171 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_MDBASE_HI,
1172 		     upper_32_bits(adev->mes.data_fw_gpu_addr[inst]));
1173 
1174 	/* Set data cache boundary CP_MES_MDBOUND_LO */
1175 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_MDBOUND_LO, 0x7FFFF);
1176 
1177 	if (prime_icache) {
1178 		/* invalidate ICACHE */
1179 		data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_IC_OP_CNTL);
1180 		data = REG_SET_FIELD(data, CP_MES_IC_OP_CNTL, PRIME_ICACHE, 0);
1181 		data = REG_SET_FIELD(data, CP_MES_IC_OP_CNTL, INVALIDATE_CACHE, 1);
1182 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_IC_OP_CNTL, data);
1183 
1184 		/* prime the ICACHE. */
1185 		data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_IC_OP_CNTL);
1186 		data = REG_SET_FIELD(data, CP_MES_IC_OP_CNTL, PRIME_ICACHE, 1);
1187 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MES_IC_OP_CNTL, data);
1188 	}
1189 
1190 	soc_v1_0_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
1191 	mutex_unlock(&adev->srbm_mutex);
1192 
1193 	return 0;
1194 }
1195 
1196 static int mes_v12_1_allocate_eop_buf(struct amdgpu_device *adev,
1197 					enum amdgpu_mes_pipe pipe,
1198 					int xcc_id)
1199 {
1200 	int r, inst = MES_PIPE_INST(xcc_id, pipe);
1201 	u32 *eop;
1202 
1203 	r = amdgpu_bo_create_reserved(adev, MES_EOP_SIZE, PAGE_SIZE,
1204 			      AMDGPU_GEM_DOMAIN_GTT,
1205 			      &adev->mes.eop_gpu_obj[inst],
1206 			      &adev->mes.eop_gpu_addr[inst],
1207 			      (void **)&eop);
1208 	if (r) {
1209 		dev_warn(adev->dev, "(%d) create EOP bo failed\n", r);
1210 		return r;
1211 	}
1212 
1213 	memset(eop, 0,
1214 	       adev->mes.eop_gpu_obj[inst]->tbo.base.size);
1215 
1216 	amdgpu_bo_kunmap(adev->mes.eop_gpu_obj[inst]);
1217 	amdgpu_bo_unreserve(adev->mes.eop_gpu_obj[inst]);
1218 
1219 	return 0;
1220 }
1221 
1222 static int mes_v12_1_allocate_shared_cmd_buf(struct amdgpu_device *adev,
1223 					     enum amdgpu_mes_pipe pipe,
1224 					     int xcc_id)
1225 {
1226 	int r, inst = MES_PIPE_INST(xcc_id, pipe);
1227 
1228 	r = amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
1229 				    AMDGPU_GEM_DOMAIN_VRAM,
1230 				    &adev->mes.shared_cmd_buf_obj[inst],
1231 				    &adev->mes.shared_cmd_buf_gpu_addr[inst],
1232 				    NULL);
1233 	if (r) {
1234 		dev_err(adev->dev,
1235 			"(%d) failed to create shared cmd buf bo\n", r);
1236 		return r;
1237 	}
1238 
1239 	return 0;
1240 }
1241 
1242 static int mes_v12_1_mqd_init(struct amdgpu_ring *ring)
1243 {
1244 	struct v12_1_mes_mqd *mqd = ring->mqd_ptr;
1245 	uint64_t hqd_gpu_addr, wb_gpu_addr, eop_base_addr;
1246 	uint32_t tmp;
1247 
1248 	mqd->header = 0xC0310800;
1249 	mqd->compute_pipelinestat_enable = 0x00000001;
1250 	mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
1251 	mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
1252 	mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
1253 	mqd->compute_static_thread_mgmt_se3 = 0xffffffff;
1254 	mqd->compute_misc_reserved = 0x00000007;
1255 
1256 	eop_base_addr = ring->eop_gpu_addr >> 8;
1257 
1258 	/* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
1259 	tmp = regCP_HQD_EOP_CONTROL_DEFAULT;
1260 	tmp = REG_SET_FIELD(tmp, CP_HQD_EOP_CONTROL, EOP_SIZE,
1261 			(order_base_2(MES_EOP_SIZE / 4) - 1));
1262 
1263 	mqd->cp_hqd_eop_base_addr_lo = lower_32_bits(eop_base_addr);
1264 	mqd->cp_hqd_eop_base_addr_hi = upper_32_bits(eop_base_addr);
1265 	mqd->cp_hqd_eop_control = tmp;
1266 
1267 	/* disable the queue if it's active */
1268 	ring->wptr = 0;
1269 	mqd->cp_hqd_pq_rptr = 0;
1270 	mqd->cp_hqd_pq_wptr_lo = 0;
1271 	mqd->cp_hqd_pq_wptr_hi = 0;
1272 
1273 	/* set the pointer to the MQD */
1274 	mqd->cp_mqd_base_addr_lo = ring->mqd_gpu_addr & 0xfffffffc;
1275 	mqd->cp_mqd_base_addr_hi = upper_32_bits(ring->mqd_gpu_addr);
1276 
1277 	/* set MQD vmid to 0 */
1278 	tmp = regCP_MQD_CONTROL_DEFAULT;
1279 	tmp = REG_SET_FIELD(tmp, CP_MQD_CONTROL, VMID, 0);
1280 	mqd->cp_mqd_control = tmp;
1281 
1282 	/* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
1283 	hqd_gpu_addr = ring->gpu_addr >> 8;
1284 	mqd->cp_hqd_pq_base_lo = lower_32_bits(hqd_gpu_addr);
1285 	mqd->cp_hqd_pq_base_hi = upper_32_bits(hqd_gpu_addr);
1286 
1287 	/* set the wb address whether it's enabled or not */
1288 	wb_gpu_addr = ring->rptr_gpu_addr;
1289 	mqd->cp_hqd_pq_rptr_report_addr_lo = wb_gpu_addr & 0xfffffffc;
1290 	mqd->cp_hqd_pq_rptr_report_addr_hi =
1291 		upper_32_bits(wb_gpu_addr) & 0xffff;
1292 
1293 	/* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
1294 	wb_gpu_addr = ring->wptr_gpu_addr;
1295 	mqd->cp_hqd_pq_wptr_poll_addr_lo = wb_gpu_addr & 0xfffffff8;
1296 	mqd->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr) & 0xffff;
1297 
1298 	/* set up the HQD, this is similar to CP_RB0_CNTL */
1299 	tmp = regCP_HQD_PQ_CONTROL_DEFAULT;
1300 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, QUEUE_SIZE,
1301 			    (order_base_2(ring->ring_size / 4) - 1));
1302 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, RPTR_BLOCK_SIZE,
1303 			    ((order_base_2(AMDGPU_GPU_PAGE_SIZE / 4) - 1) << 8));
1304 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, UNORD_DISPATCH, 1);
1305 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, SCOPE, 3);
1306 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, TUNNEL_DISPATCH, 0);
1307 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, PRIV_STATE, 1);
1308 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, KMD_QUEUE, 1);
1309 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, NO_UPDATE_RPTR, 1);
1310 	mqd->cp_hqd_pq_control = tmp;
1311 
1312 	/* enable doorbell */
1313 	tmp = 0;
1314 	if (ring->use_doorbell) {
1315 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1316 				    DOORBELL_OFFSET, ring->doorbell_index);
1317 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1318 				    DOORBELL_EN, 1);
1319 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1320 				    DOORBELL_SOURCE, 0);
1321 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1322 				    DOORBELL_HIT, 0);
1323 	} else {
1324 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1325 				    DOORBELL_EN, 0);
1326 	}
1327 	mqd->cp_hqd_pq_doorbell_control = tmp;
1328 
1329 	mqd->cp_hqd_vmid = 0;
1330 	/* activate the queue */
1331 	mqd->cp_hqd_active = 1;
1332 
1333 	tmp = regCP_HQD_PERSISTENT_STATE_DEFAULT;
1334 	tmp = REG_SET_FIELD(tmp, CP_HQD_PERSISTENT_STATE,
1335 			    PRELOAD_SIZE, 0x63);
1336 	mqd->cp_hqd_persistent_state = tmp;
1337 
1338 	mqd->cp_hqd_ib_control = regCP_HQD_IB_CONTROL_MES_12_1_DEFAULT;
1339 	mqd->cp_hqd_iq_timer = regCP_HQD_IQ_TIMER_DEFAULT;
1340 	mqd->cp_hqd_quantum = regCP_HQD_QUANTUM_DEFAULT;
1341 
1342 	/*
1343 	 * Set CP_HQD_GFX_CONTROL.DB_UPDATED_MSG_EN[15] to enable unmapped
1344 	 * doorbell handling. This is a reserved CP internal register can
1345 	 * not be accesss by others
1346 	 */
1347 	mqd->cp_hqd_gfx_control = BIT(15);
1348 
1349 	return 0;
1350 }
1351 
1352 static void mes_v12_1_queue_init_register(struct amdgpu_ring *ring,
1353 					    int xcc_id)
1354 {
1355 	struct v12_1_mes_mqd *mqd = ring->mqd_ptr;
1356 	struct amdgpu_device *adev = ring->adev;
1357 	uint32_t data = 0;
1358 
1359 	mutex_lock(&adev->srbm_mutex);
1360 	soc_v1_0_grbm_select(adev, 3, ring->pipe, 0, 0, GET_INST(GC, xcc_id));
1361 
1362 	/* set CP_HQD_VMID.VMID = 0. */
1363 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_VMID);
1364 	data = REG_SET_FIELD(data, CP_HQD_VMID, VMID, 0);
1365 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_VMID, data);
1366 
1367 	/* set CP_HQD_PQ_DOORBELL_CONTROL.DOORBELL_EN=0 */
1368 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL);
1369 	data = REG_SET_FIELD(data, CP_HQD_PQ_DOORBELL_CONTROL,
1370 			     DOORBELL_EN, 0);
1371 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL, data);
1372 
1373 	/* set CP_MQD_BASE_ADDR/HI with the MQD base address */
1374 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MQD_BASE_ADDR, mqd->cp_mqd_base_addr_lo);
1375 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MQD_BASE_ADDR_HI, mqd->cp_mqd_base_addr_hi);
1376 
1377 	/* set CP_MQD_CONTROL.VMID=0 */
1378 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MQD_CONTROL);
1379 	data = REG_SET_FIELD(data, CP_MQD_CONTROL, VMID, 0);
1380 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MQD_CONTROL, 0);
1381 
1382 	/* set CP_HQD_PQ_BASE/HI with the ring buffer base address */
1383 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_BASE, mqd->cp_hqd_pq_base_lo);
1384 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_BASE_HI, mqd->cp_hqd_pq_base_hi);
1385 
1386 	/* set CP_HQD_PQ_RPTR_REPORT_ADDR/HI */
1387 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR_REPORT_ADDR,
1388 		     mqd->cp_hqd_pq_rptr_report_addr_lo);
1389 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR_REPORT_ADDR_HI,
1390 		     mqd->cp_hqd_pq_rptr_report_addr_hi);
1391 
1392 	/* set CP_HQD_PQ_CONTROL */
1393 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_CONTROL, mqd->cp_hqd_pq_control);
1394 
1395 	/* set CP_HQD_PQ_WPTR_POLL_ADDR/HI */
1396 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_POLL_ADDR,
1397 		     mqd->cp_hqd_pq_wptr_poll_addr_lo);
1398 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_POLL_ADDR_HI,
1399 		     mqd->cp_hqd_pq_wptr_poll_addr_hi);
1400 
1401 	/* set CP_HQD_PQ_DOORBELL_CONTROL */
1402 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL,
1403 		     mqd->cp_hqd_pq_doorbell_control);
1404 
1405 	/* set CP_HQD_PERSISTENT_STATE.PRELOAD_SIZE=0x53 */
1406 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PERSISTENT_STATE, mqd->cp_hqd_persistent_state);
1407 
1408 	/* set CP_HQD_ACTIVE.ACTIVE=1 */
1409 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE, mqd->cp_hqd_active);
1410 
1411 	soc_v1_0_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
1412 	mutex_unlock(&adev->srbm_mutex);
1413 }
1414 
1415 static int mes_v12_1_queue_init(struct amdgpu_device *adev,
1416 				  enum amdgpu_mes_pipe pipe,
1417 				  int xcc_id)
1418 {
1419 	struct amdgpu_ring *ring;
1420 	int r;
1421 
1422 	ring = &adev->mes.ring[MES_PIPE_INST(xcc_id, pipe)];
1423 
1424 	if (amdgpu_in_reset(adev) || adev->in_suspend) {
1425 		*(ring->wptr_cpu_addr) = 0;
1426 		*(ring->rptr_cpu_addr) = 0;
1427 		amdgpu_ring_clear_ring(ring);
1428 	}
1429 
1430 	r = mes_v12_1_mqd_init(ring);
1431 	if (r)
1432 		return r;
1433 
1434 	if (pipe == AMDGPU_MES_SCHED_PIPE) {
1435 		r = amdgpu_mes_map_legacy_queue(adev, ring, xcc_id);
1436 		if (r)
1437 			return r;
1438 	} else {
1439 		mes_v12_1_queue_init_register(ring, xcc_id);
1440 	}
1441 
1442 	return 0;
1443 }
1444 
1445 static int mes_v12_1_ring_init(struct amdgpu_device *adev,
1446 				 int xcc_id, int pipe)
1447 {
1448 	struct amdgpu_ring *ring;
1449 	int inst = MES_PIPE_INST(xcc_id, pipe);
1450 
1451 	ring = &adev->mes.ring[inst];
1452 
1453 	ring->funcs = &mes_v12_1_ring_funcs;
1454 
1455 	ring->me = 3;
1456 	ring->pipe = pipe;
1457 	ring->queue = 0;
1458 	ring->xcc_id = xcc_id;
1459 	ring->vm_hub = AMDGPU_GFXHUB(xcc_id);
1460 
1461 	ring->ring_obj = NULL;
1462 	ring->use_doorbell = true;
1463 	ring->eop_gpu_addr = adev->mes.eop_gpu_addr[inst];
1464 	ring->no_scheduler = true;
1465 	snprintf(ring->name, sizeof(ring->name), "mes_%hhu.%hhu.%hhu.%hhu",
1466 		 (unsigned char)xcc_id, (unsigned char)ring->me,
1467 		 (unsigned char)ring->pipe, (unsigned char)ring->queue);
1468 
1469 	if (pipe == AMDGPU_MES_SCHED_PIPE)
1470 		ring->doorbell_index =
1471 			(adev->doorbell_index.mes_ring0 +
1472 			 xcc_id * adev->doorbell_index.xcc_doorbell_range)
1473 			<< 1;
1474 	else
1475 		ring->doorbell_index =
1476 			(adev->doorbell_index.mes_ring1 +
1477 			 xcc_id * adev->doorbell_index.xcc_doorbell_range)
1478 			<< 1;
1479 
1480 	return amdgpu_ring_init(adev, ring, 1024, NULL, 0,
1481 				AMDGPU_RING_PRIO_DEFAULT, NULL);
1482 }
1483 
1484 static int mes_v12_1_mqd_sw_init(struct amdgpu_device *adev,
1485 				   enum amdgpu_mes_pipe pipe,
1486 				   int xcc_id)
1487 {
1488 	int r, mqd_size = sizeof(struct v12_1_mes_mqd);
1489 	struct amdgpu_ring *ring;
1490 	int inst = MES_PIPE_INST(xcc_id, pipe);
1491 
1492 	ring = &adev->mes.ring[inst];
1493 
1494 	if (ring->mqd_obj)
1495 		return 0;
1496 
1497 	r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
1498 				    AMDGPU_GEM_DOMAIN_GTT, &ring->mqd_obj,
1499 				    &ring->mqd_gpu_addr, &ring->mqd_ptr);
1500 	if (r) {
1501 		dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
1502 		return r;
1503 	}
1504 
1505 	memset(ring->mqd_ptr, 0, mqd_size);
1506 
1507 	/* prepare MQD backup */
1508 	adev->mes.mqd_backup[inst] = kmalloc(mqd_size, GFP_KERNEL);
1509 	if (!adev->mes.mqd_backup[inst])
1510 		dev_warn(adev->dev,
1511 			 "no memory to create MQD backup for ring %s\n",
1512 			 ring->name);
1513 
1514 	return 0;
1515 }
1516 
1517 static int mes_v12_1_sw_init(struct amdgpu_ip_block *ip_block)
1518 {
1519 	struct amdgpu_device *adev = ip_block->adev;
1520 	int pipe, r, xcc_id, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1521 
1522 	adev->mes.funcs = &mes_v12_1_funcs;
1523 	adev->mes.kiq_hw_init = &mes_v12_1_kiq_hw_init;
1524 	adev->mes.kiq_hw_fini = &mes_v12_1_kiq_hw_fini;
1525 	adev->mes.enable_legacy_queue_map = true;
1526 
1527 	adev->mes.event_log_size = AMDGPU_MAX_MES_PIPES *
1528 				   AMDGPU_MES_LOG_BUFFER_SIZE * num_xcc;
1529 
1530 	r = amdgpu_mes_init(adev);
1531 	if (r)
1532 		return r;
1533 
1534 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1535 		for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++) {
1536 			r = mes_v12_1_allocate_eop_buf(adev, pipe, xcc_id);
1537 			if (r)
1538 				return r;
1539 
1540 			r = mes_v12_1_mqd_sw_init(adev, pipe, xcc_id);
1541 			if (r)
1542 				return r;
1543 
1544 			r = mes_v12_1_ring_init(adev, xcc_id, pipe);
1545 			if (r)
1546 				return r;
1547 
1548 			if (num_xcc > 1) {
1549 				r = mes_v12_1_allocate_shared_cmd_buf(adev,
1550 							      pipe, xcc_id);
1551 				if (r)
1552 					return r;
1553 			}
1554 		}
1555 	}
1556 
1557 	return 0;
1558 }
1559 
1560 static int mes_v12_1_sw_fini(struct amdgpu_ip_block *ip_block)
1561 {
1562 	struct amdgpu_device *adev = ip_block->adev;
1563 	int pipe, inst, xcc_id, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1564 
1565 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1566 		for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++) {
1567 			inst = MES_PIPE_INST(xcc_id, pipe);
1568 
1569 			amdgpu_bo_free_kernel(&adev->mes.shared_cmd_buf_obj[inst],
1570 					      &adev->mes.shared_cmd_buf_gpu_addr[inst],
1571 					      NULL);
1572 
1573 			kfree(adev->mes.mqd_backup[inst]);
1574 
1575 			amdgpu_bo_free_kernel(&adev->mes.eop_gpu_obj[inst],
1576 					      &adev->mes.eop_gpu_addr[inst],
1577 					      NULL);
1578 
1579 			amdgpu_bo_free_kernel(&adev->mes.ring[inst].mqd_obj,
1580 					      &adev->mes.ring[inst].mqd_gpu_addr,
1581 					      &adev->mes.ring[inst].mqd_ptr);
1582 			amdgpu_ring_fini(&adev->mes.ring[inst]);
1583 		}
1584 	}
1585 
1586 	for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++)
1587 		amdgpu_ucode_release(&adev->mes.fw[pipe]);
1588 
1589 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1590 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
1591 			mes_v12_1_free_ucode_buffers(adev,
1592 				       AMDGPU_MES_KIQ_PIPE, xcc_id);
1593 			mes_v12_1_free_ucode_buffers(adev,
1594 				       AMDGPU_MES_SCHED_PIPE, xcc_id);
1595 		}
1596 	}
1597 
1598 	amdgpu_mes_fini(adev);
1599 	return 0;
1600 }
1601 
1602 static void mes_v12_1_kiq_dequeue(struct amdgpu_device *adev, int xcc_id)
1603 {
1604 	int i;
1605 
1606 	mutex_lock(&adev->srbm_mutex);
1607 	soc_v1_0_grbm_select(adev, 3, AMDGPU_MES_KIQ_PIPE, 0, 0,
1608 			     GET_INST(GC, xcc_id));
1609 
1610 	/* disable the queue if it's active */
1611 	if (RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1) {
1612 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_DEQUEUE_REQUEST, 1);
1613 		for (i = 0; i < adev->usec_timeout; i++) {
1614 			if (!(RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1))
1615 				break;
1616 			udelay(1);
1617 		}
1618 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_DEQUEUE_REQUEST, 0);
1619 	}
1620 
1621 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL, 0);
1622 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_LO, 0);
1623 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_HI, 0);
1624 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR, 0);
1625 
1626 	soc_v1_0_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
1627 	mutex_unlock(&adev->srbm_mutex);
1628 }
1629 
1630 static void mes_v12_1_kiq_setting(struct amdgpu_ring *ring, int xcc_id)
1631 {
1632 	uint32_t tmp;
1633 	struct amdgpu_device *adev = ring->adev;
1634 
1635 	/* tell RLC which is KIQ queue */
1636 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CP_SCHEDULERS);
1637 	tmp &= 0xffffff00;
1638 	tmp |= (ring->me << 5) | (ring->pipe << 3) | (ring->queue);
1639 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CP_SCHEDULERS, tmp);
1640 	tmp |= 0x80;
1641 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CP_SCHEDULERS, tmp);
1642 }
1643 
1644 static int mes_v12_1_kiq_hw_init(struct amdgpu_device *adev, uint32_t xcc_id)
1645 {
1646 	int inst = MES_PIPE_INST(xcc_id, AMDGPU_MES_KIQ_PIPE);
1647 	int r = 0;
1648 	struct amdgpu_ip_block *ip_block;
1649 
1650 	mes_v12_1_kiq_setting(&adev->mes.ring[inst], xcc_id);
1651 
1652 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
1653 
1654 		r = mes_v12_1_load_microcode(adev, AMDGPU_MES_SCHED_PIPE,
1655 					       false, xcc_id);
1656 		if (r) {
1657 			DRM_ERROR("failed to load MES fw, r=%d\n", r);
1658 			return r;
1659 		}
1660 
1661 		r = mes_v12_1_load_microcode(adev, AMDGPU_MES_KIQ_PIPE,
1662 					       true, xcc_id);
1663 		if (r) {
1664 			DRM_ERROR("failed to load MES kiq fw, r=%d\n", r);
1665 			return r;
1666 		}
1667 
1668 		mes_v12_1_set_ucode_start_addr(adev, xcc_id);
1669 
1670 	} else if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO)
1671 		mes_v12_1_set_ucode_start_addr(adev, xcc_id);
1672 
1673 	mes_v12_1_enable(adev, true, xcc_id);
1674 
1675 	ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_MES);
1676 	if (unlikely(!ip_block)) {
1677 		dev_err(adev->dev, "Failed to get MES handle\n");
1678 		return -EINVAL;
1679 	}
1680 
1681 	r = mes_v12_1_queue_init(adev, AMDGPU_MES_KIQ_PIPE, xcc_id);
1682 	if (r)
1683 		goto failure;
1684 
1685 	r = mes_v12_1_setup_coop_mode(adev, xcc_id);
1686 	if (r)
1687 		goto failure;
1688 
1689 	r = mes_v12_1_set_hw_resources(&adev->mes,
1690 				       AMDGPU_MES_KIQ_PIPE, xcc_id);
1691 	if (r)
1692 		goto failure;
1693 
1694 	mes_v12_1_set_hw_resources_1(&adev->mes,
1695 				     AMDGPU_MES_KIQ_PIPE, xcc_id);
1696 
1697 	if (adev->mes.enable_legacy_queue_map) {
1698 		r = mes_v12_1_xcc_hw_init(ip_block, xcc_id);
1699 		if (r)
1700 			goto failure;
1701 	}
1702 
1703 	return r;
1704 
1705 failure:
1706 	mes_v12_1_hw_fini(ip_block);
1707 	return r;
1708 }
1709 
1710 static int mes_v12_1_kiq_hw_fini(struct amdgpu_device *adev, uint32_t xcc_id)
1711 {
1712 	int inst = MES_PIPE_INST(xcc_id, AMDGPU_MES_SCHED_PIPE);
1713 
1714 	if (adev->mes.ring[inst].sched.ready) {
1715 		amdgpu_mes_unmap_legacy_queue(adev, &adev->mes.ring[inst],
1716 					      RESET_QUEUES, 0, 0, xcc_id);
1717 
1718 		adev->mes.ring[inst].sched.ready = false;
1719 	}
1720 
1721 	/* dequeue KIQ HQD on unbind to clear stale rptr/wptr on rebind */
1722 	if (!amdgpu_in_reset(adev) && !adev->in_suspend)
1723 		mes_v12_1_kiq_dequeue(adev, xcc_id);
1724 
1725 	mes_v12_1_enable(adev, false, xcc_id);
1726 
1727 	return 0;
1728 }
1729 
1730 static int mes_v12_1_setup_coop_mode(struct amdgpu_device *adev, int xcc_id)
1731 {
1732 	u32 num_xcc_per_xcp, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1733 	int r = 0;
1734 
1735 	if (num_xcc == 1)
1736 		return r;
1737 
1738 	if (adev->gfx.funcs &&
1739 	    adev->gfx.funcs->get_xccs_per_xcp)
1740 		num_xcc_per_xcp = adev->gfx.funcs->get_xccs_per_xcp(adev);
1741 	else
1742 		return -EINVAL;
1743 
1744 	switch (adev->xcp_mgr->mode) {
1745 	case AMDGPU_SPX_PARTITION_MODE:
1746 		adev->mes.enable_coop_mode = 1;
1747 		adev->mes.master_xcc_ids[xcc_id] = 0;
1748 		break;
1749 	case AMDGPU_DPX_PARTITION_MODE:
1750 		adev->mes.enable_coop_mode = 1;
1751 		adev->mes.master_xcc_ids[xcc_id] =
1752 			(xcc_id/num_xcc_per_xcp) * (num_xcc / 2);
1753 		break;
1754 	case AMDGPU_QPX_PARTITION_MODE:
1755 		adev->mes.enable_coop_mode = 1;
1756 		adev->mes.master_xcc_ids[xcc_id] =
1757 			(xcc_id/num_xcc_per_xcp) * (num_xcc / 4);
1758 		break;
1759 	case AMDGPU_CPX_PARTITION_MODE:
1760 		adev->mes.enable_coop_mode = 0;
1761 		break;
1762 	default:
1763 		r = -EINVAL;
1764 		break;
1765 	}
1766 	return r;
1767 }
1768 
1769 static int mes_v12_1_xcc_hw_init(struct amdgpu_ip_block *ip_block, int xcc_id)
1770 {
1771 	int r;
1772 	struct amdgpu_device *adev = ip_block->adev;
1773 
1774 	if (adev->mes.ring[MES_PIPE_INST(xcc_id, 0)].sched.ready)
1775 		goto out;
1776 
1777 	/* Enable the MES to handle doorbell ring on unmapped queue */
1778 	mes_v12_1_enable_unmapped_doorbell_handling(&adev->mes, true, xcc_id);
1779 
1780 	r = mes_v12_1_queue_init(adev, AMDGPU_MES_SCHED_PIPE, xcc_id);
1781 	if (r)
1782 		goto failure;
1783 
1784 	r = mes_v12_1_set_hw_resources(&adev->mes,
1785 					 AMDGPU_MES_SCHED_PIPE, xcc_id);
1786 	if (r)
1787 		goto failure;
1788 
1789 	mes_v12_1_set_hw_resources_1(&adev->mes, AMDGPU_MES_SCHED_PIPE, xcc_id);
1790 	mes_v12_1_init_aggregated_doorbell(&adev->mes, xcc_id);
1791 
1792 	r = mes_v12_1_query_sched_status(&adev->mes,
1793 					   AMDGPU_MES_SCHED_PIPE, xcc_id);
1794 	if (r) {
1795 		DRM_ERROR("MES is busy\n");
1796 		goto failure;
1797 	}
1798 
1799 	amdgpu_mes_validate_fw_version(adev);
1800 out:
1801 	adev->mes.ring[MES_PIPE_INST(xcc_id, 0)].sched.ready = true;
1802 
1803 	return 0;
1804 
1805 failure:
1806 	mes_v12_1_hw_fini(ip_block);
1807 	return r;
1808 }
1809 
1810 static int mes_v12_1_hw_init(struct amdgpu_ip_block *ip_block)
1811 {
1812 	struct amdgpu_device *adev = ip_block->adev;
1813 	int r, xcc_id, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1814 
1815 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1816 		r = mes_v12_1_xcc_hw_init(ip_block, xcc_id);
1817 		if (r)
1818 			return r;
1819 	}
1820 
1821 	return 0;
1822 }
1823 
1824 static int mes_v12_1_hw_fini(struct amdgpu_ip_block *ip_block)
1825 {
1826 	return 0;
1827 }
1828 
1829 static int mes_v12_1_suspend(struct amdgpu_ip_block *ip_block)
1830 {
1831 	return mes_v12_1_hw_fini(ip_block);
1832 }
1833 
1834 static int mes_v12_1_resume(struct amdgpu_ip_block *ip_block)
1835 {
1836 	return mes_v12_1_hw_init(ip_block);
1837 }
1838 
1839 static int mes_v12_1_early_init(struct amdgpu_ip_block *ip_block)
1840 {
1841 	struct amdgpu_device *adev = ip_block->adev;
1842 	int pipe, r;
1843 
1844 	adev->mes.hung_queue_db_array_size = MES12_HUNG_DB_OFFSET_ARRAY_SIZE;
1845 	adev->mes.hung_queue_hqd_info_offset = MES12_HUNG_HQD_INFO_OFFSET;
1846 
1847 	for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++) {
1848 		r = amdgpu_mes_init_microcode(adev, pipe);
1849 		if (r)
1850 			return r;
1851 	}
1852 
1853 	return 0;
1854 }
1855 
1856 static const struct amd_ip_funcs mes_v12_1_ip_funcs = {
1857 	.name = "mes_v12_1",
1858 	.early_init = mes_v12_1_early_init,
1859 	.late_init = NULL,
1860 	.sw_init = mes_v12_1_sw_init,
1861 	.sw_fini = mes_v12_1_sw_fini,
1862 	.hw_init = mes_v12_1_hw_init,
1863 	.hw_fini = mes_v12_1_hw_fini,
1864 	.suspend = mes_v12_1_suspend,
1865 	.resume = mes_v12_1_resume,
1866 };
1867 
1868 const struct amdgpu_ip_block_version mes_v12_1_ip_block = {
1869 	.type = AMD_IP_BLOCK_TYPE_MES,
1870 	.major = 12,
1871 	.minor = 1,
1872 	.rev = 0,
1873 	.funcs = &mes_v12_1_ip_funcs,
1874 };
1875