xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c (revision 35e86e6a54e82e3624e9abdad61c8d4b0f764396)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/firmware.h>
25 #include <drm/drm_exec.h>
26 
27 #include "amdgpu_mes.h"
28 #include "amdgpu.h"
29 #include "soc15_common.h"
30 #include "amdgpu_mes_ctx.h"
31 
32 #define AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
33 #define AMDGPU_ONE_DOORBELL_SIZE 8
34 
35 int amdgpu_mes_doorbell_process_slice(struct amdgpu_device *adev)
36 {
37 	return roundup(AMDGPU_ONE_DOORBELL_SIZE *
38 		       AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
39 		       PAGE_SIZE);
40 }
41 
42 static int amdgpu_mes_doorbell_init(struct amdgpu_device *adev)
43 {
44 	int i;
45 	struct amdgpu_mes *mes = &adev->mes;
46 
47 	/* Bitmap for dynamic allocation of kernel doorbells */
48 	mes->doorbell_bitmap = bitmap_zalloc(PAGE_SIZE / sizeof(u32), GFP_KERNEL);
49 	if (!mes->doorbell_bitmap) {
50 		dev_err(adev->dev, "Failed to allocate MES doorbell bitmap\n");
51 		return -ENOMEM;
52 	}
53 
54 	mes->num_mes_dbs = PAGE_SIZE / AMDGPU_ONE_DOORBELL_SIZE;
55 	for (i = 0; i < AMDGPU_MES_PRIORITY_NUM_LEVELS; i++) {
56 		adev->mes.aggregated_doorbells[i] = mes->db_start_dw_offset + i * 2;
57 		set_bit(i, mes->doorbell_bitmap);
58 	}
59 
60 	return 0;
61 }
62 
63 static int amdgpu_mes_event_log_init(struct amdgpu_device *adev)
64 {
65 	int r;
66 
67 	if (!amdgpu_mes_log_enable)
68 		return 0;
69 
70 	r = amdgpu_bo_create_kernel(adev, adev->mes.event_log_size, PAGE_SIZE,
71 				    AMDGPU_GEM_DOMAIN_VRAM,
72 				    &adev->mes.event_log_gpu_obj,
73 				    &adev->mes.event_log_gpu_addr,
74 				    &adev->mes.event_log_cpu_addr);
75 	if (r) {
76 		dev_warn(adev->dev, "failed to create MES event log buffer (%d)", r);
77 		return r;
78 	}
79 
80 	memset(adev->mes.event_log_cpu_addr, 0, adev->mes.event_log_size);
81 
82 	return  0;
83 
84 }
85 
86 static void amdgpu_mes_doorbell_free(struct amdgpu_device *adev)
87 {
88 	bitmap_free(adev->mes.doorbell_bitmap);
89 }
90 
91 static inline u32 amdgpu_mes_get_hqd_mask(u32 num_pipe,
92 					  u32 num_hqd_per_pipe,
93 					  u32 num_reserved_hqd)
94 {
95 	if (num_pipe == 0)
96 		return 0;
97 
98 	u32 total_hqd_mask = (u32)((1ULL << num_hqd_per_pipe) - 1);
99 	u32 reserved_hqd_mask = (u32)((1ULL << DIV_ROUND_UP(num_reserved_hqd, num_pipe)) - 1);
100 
101 	return (total_hqd_mask & ~reserved_hqd_mask);
102 }
103 
104 int amdgpu_mes_init(struct amdgpu_device *adev)
105 {
106 	int i, r, num_pipes, num_queues = 0;
107 	u32 total_vmid_mask, reserved_vmid_mask;
108 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
109 	u32 gfx_hqd_mask = amdgpu_mes_get_hqd_mask(adev->gfx.me.num_pipe_per_me,
110 				adev->gfx.me.num_queue_per_pipe,
111 				adev->gfx.disable_kq ? 0 : adev->gfx.num_gfx_rings);
112 	u32 compute_hqd_mask = amdgpu_mes_get_hqd_mask(adev->gfx.mec.num_pipe_per_mec,
113 				adev->gfx.mec.num_queue_per_pipe,
114 				adev->gfx.disable_kq ? 0 : adev->gfx.num_compute_rings);
115 
116 	adev->mes.adev = adev;
117 
118 	ida_init(&adev->mes.doorbell_ida);
119 	spin_lock_init(&adev->mes.queue_id_lock);
120 	mutex_init(&adev->mes.mutex_hidden);
121 
122 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++)
123 		spin_lock_init(&adev->mes.ring_lock[i]);
124 
125 	adev->mes.total_max_queue = AMDGPU_FENCE_MES_QUEUE_ID_MASK;
126 	total_vmid_mask = (u32)((1UL << 16) - 1);
127 	reserved_vmid_mask = (u32)((1UL << adev->vm_manager.first_kfd_vmid) - 1);
128 
129 	adev->mes.vmid_mask_mmhub = 0xFF00;
130 	adev->mes.vmid_mask_gfxhub = total_vmid_mask & ~reserved_vmid_mask;
131 
132 	num_pipes = adev->gfx.me.num_pipe_per_me * adev->gfx.me.num_me;
133 	if (num_pipes > AMDGPU_MES_MAX_GFX_PIPES)
134 		dev_warn(adev->dev, "more gfx pipes than supported by MES! (%d vs %d)\n",
135 			 num_pipes, AMDGPU_MES_MAX_GFX_PIPES);
136 
137 	for (i = 0; i < AMDGPU_MES_MAX_GFX_PIPES; i++) {
138 		if (i >= num_pipes)
139 			break;
140 
141 		adev->mes.gfx_hqd_mask[i] = gfx_hqd_mask;
142 	}
143 
144 	num_pipes = adev->gfx.mec.num_pipe_per_mec * adev->gfx.mec.num_mec;
145 	if (num_pipes > AMDGPU_MES_MAX_COMPUTE_PIPES)
146 		dev_warn(adev->dev, "more compute pipes than supported by MES! (%d vs %d)\n",
147 			 num_pipes, AMDGPU_MES_MAX_COMPUTE_PIPES);
148 
149 	for (i = 0; i < AMDGPU_MES_MAX_COMPUTE_PIPES; i++) {
150 		/*
151 		 * Currently, only MEC1 is used for both kernel and user compute queue.
152 		 * To enable other MEC, we need to redistribute queues per pipe and
153 		 * adjust queue resource shared with kfd that needs a separate patch.
154 		 * Skip other MEC for now to avoid potential issues.
155 		 */
156 		if (i >= adev->gfx.mec.num_pipe_per_mec)
157 			break;
158 
159 		adev->mes.compute_hqd_mask[i] = compute_hqd_mask;
160 	}
161 
162 	num_pipes = adev->sdma.num_inst_per_xcc ?
163 		adev->sdma.num_inst_per_xcc : adev->sdma.num_instances;
164 	if (num_pipes > AMDGPU_MES_MAX_SDMA_PIPES)
165 		dev_warn(adev->dev, "more SDMA pipes than supported by MES! (%d vs %d)\n",
166 			 num_pipes, AMDGPU_MES_MAX_SDMA_PIPES);
167 
168 	for (i = 0; i < AMDGPU_MES_MAX_SDMA_PIPES; i++) {
169 		if (i >= num_pipes)
170 			break;
171 		adev->mes.sdma_hqd_mask[i] = 0xfc;
172 	}
173 
174 	dev_info(adev->dev,
175 			 "MES: vmid_mask_mmhub 0x%08x, vmid_mask_gfxhub 0x%08x\n",
176 			 adev->mes.vmid_mask_mmhub,
177 			 adev->mes.vmid_mask_gfxhub);
178 
179 	dev_info(adev->dev,
180 			 "MES: gfx_hqd_mask 0x%08x, compute_hqd_mask 0x%08x, sdma_hqd_mask 0x%08x\n",
181 			 adev->mes.gfx_hqd_mask[0],
182 			 adev->mes.compute_hqd_mask[0],
183 			 adev->mes.sdma_hqd_mask[0]);
184 
185 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++) {
186 		r = amdgpu_device_wb_get(adev, &adev->mes.sch_ctx_offs[i]);
187 		if (r) {
188 			dev_err(adev->dev,
189 				"(%d) ring trail_fence_offs wb alloc failed\n",
190 				r);
191 			goto error;
192 		}
193 		adev->mes.sch_ctx_gpu_addr[i] =
194 			adev->wb.gpu_addr + (adev->mes.sch_ctx_offs[i] * 4);
195 		adev->mes.sch_ctx_ptr[i] =
196 			(uint64_t *)&adev->wb.wb[adev->mes.sch_ctx_offs[i]];
197 
198 		r = amdgpu_device_wb_get(adev,
199 				 &adev->mes.query_status_fence_offs[i]);
200 		if (r) {
201 			dev_err(adev->dev,
202 			      "(%d) query_status_fence_offs wb alloc failed\n",
203 			      r);
204 			goto error;
205 		}
206 		adev->mes.query_status_fence_gpu_addr[i] = adev->wb.gpu_addr +
207 			(adev->mes.query_status_fence_offs[i] * 4);
208 		adev->mes.query_status_fence_ptr[i] =
209 			(uint64_t *)&adev->wb.wb[adev->mes.query_status_fence_offs[i]];
210 	}
211 
212 	r = amdgpu_mes_doorbell_init(adev);
213 	if (r)
214 		goto error;
215 
216 	r = amdgpu_mes_event_log_init(adev);
217 	if (r)
218 		goto error_doorbell;
219 
220 	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(12, 1, 0)) {
221 		/* When queue/pipe reset is done in MES instead of in the
222 		 * driver, MES passes hung queues information to the driver in
223 		 * hung_queue_hqd_info. Calculate required space to store this
224 		 * information.
225 		 */
226 		for (i = 0; i < AMDGPU_MES_MAX_GFX_PIPES; i++)
227 			num_queues += hweight32(adev->mes.gfx_hqd_mask[i]);
228 
229 		for (i = 0; i < AMDGPU_MES_MAX_COMPUTE_PIPES; i++)
230 			num_queues += hweight32(adev->mes.compute_hqd_mask[i]);
231 
232 		for (i = 0; i < AMDGPU_MES_MAX_SDMA_PIPES; i++)
233 			num_queues += hweight32(adev->mes.sdma_hqd_mask[i]) * num_xcc;
234 
235 		adev->mes.hung_queue_hqd_info_offset = num_queues;
236 		adev->mes.hung_queue_db_array_size = num_queues * 2;
237 	}
238 
239 	if (adev->mes.hung_queue_db_array_size) {
240 		for (i = 0; i < AMDGPU_MAX_MES_PIPES; i++) {
241 			r = amdgpu_bo_create_kernel(adev,
242 						    adev->mes.hung_queue_db_array_size * sizeof(u32),
243 						    PAGE_SIZE,
244 						    AMDGPU_GEM_DOMAIN_GTT,
245 						    &adev->mes.hung_queue_db_array_gpu_obj[i],
246 						    &adev->mes.hung_queue_db_array_gpu_addr[i],
247 						    &adev->mes.hung_queue_db_array_cpu_addr[i]);
248 			if (r) {
249 				dev_warn(adev->dev, "failed to create MES hung db array buffer (%d)", r);
250 				goto error_doorbell;
251 			}
252 		}
253 	}
254 
255 	return 0;
256 
257 error_doorbell:
258 	amdgpu_mes_doorbell_free(adev);
259 error:
260 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++) {
261 		if (adev->mes.sch_ctx_ptr[i])
262 			amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs[i]);
263 		if (adev->mes.query_status_fence_ptr[i])
264 			amdgpu_device_wb_free(adev,
265 				      adev->mes.query_status_fence_offs[i]);
266 		if (adev->mes.hung_queue_db_array_gpu_obj[i])
267 			amdgpu_bo_free_kernel(&adev->mes.hung_queue_db_array_gpu_obj[i],
268 					      &adev->mes.hung_queue_db_array_gpu_addr[i],
269 					      &adev->mes.hung_queue_db_array_cpu_addr[i]);
270 	}
271 
272 	ida_destroy(&adev->mes.doorbell_ida);
273 	mutex_destroy(&adev->mes.mutex_hidden);
274 	return r;
275 }
276 
277 void amdgpu_mes_fini(struct amdgpu_device *adev)
278 {
279 	int i;
280 	int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
281 
282 	amdgpu_bo_free_kernel(&adev->mes.event_log_gpu_obj,
283 			      &adev->mes.event_log_gpu_addr,
284 			      &adev->mes.event_log_cpu_addr);
285 
286 	for (i = 0; i < AMDGPU_MAX_MES_PIPES * num_xcc; i++) {
287 		if (adev->mes.hung_queue_db_array_gpu_obj[i])
288 			 amdgpu_bo_free_kernel(&adev->mes.hung_queue_db_array_gpu_obj[i],
289 					 &adev->mes.hung_queue_db_array_gpu_addr[i],
290 					 &adev->mes.hung_queue_db_array_cpu_addr[i]);
291 		if (adev->mes.sch_ctx_ptr[i])
292 			amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs[i]);
293 		if (adev->mes.query_status_fence_ptr[i])
294 			amdgpu_device_wb_free(adev,
295 				      adev->mes.query_status_fence_offs[i]);
296 	}
297 
298 	amdgpu_mes_doorbell_free(adev);
299 
300 	ida_destroy(&adev->mes.doorbell_ida);
301 	mutex_destroy(&adev->mes.mutex_hidden);
302 }
303 
304 int amdgpu_mes_suspend(struct amdgpu_device *adev)
305 {
306 	struct mes_suspend_gang_input input;
307 	int r;
308 
309 	if (!amdgpu_mes_suspend_resume_all_supported(adev))
310 		return 0;
311 
312 	memset(&input, 0x0, sizeof(struct mes_suspend_gang_input));
313 	input.suspend_all_gangs = 1;
314 
315 	/*
316 	 * Avoid taking any other locks under MES lock to avoid circular
317 	 * lock dependencies.
318 	 */
319 	amdgpu_mes_lock(&adev->mes);
320 	r = adev->mes.funcs->suspend_gang(&adev->mes, &input);
321 	amdgpu_mes_unlock(&adev->mes);
322 	if (r)
323 		dev_err(adev->dev, "failed to suspend all gangs");
324 
325 	return r;
326 }
327 
328 int amdgpu_mes_resume(struct amdgpu_device *adev)
329 {
330 	struct mes_resume_gang_input input;
331 	int r;
332 
333 	if (!amdgpu_mes_suspend_resume_all_supported(adev))
334 		return 0;
335 
336 	memset(&input, 0x0, sizeof(struct mes_resume_gang_input));
337 	input.resume_all_gangs = 1;
338 
339 	/*
340 	 * Avoid taking any other locks under MES lock to avoid circular
341 	 * lock dependencies.
342 	 */
343 	amdgpu_mes_lock(&adev->mes);
344 	r = adev->mes.funcs->resume_gang(&adev->mes, &input);
345 	amdgpu_mes_unlock(&adev->mes);
346 	if (r)
347 		dev_err(adev->dev, "failed to resume all gangs");
348 
349 	return r;
350 }
351 
352 int amdgpu_mes_map_legacy_queue(struct amdgpu_device *adev,
353 				struct amdgpu_ring *ring, uint32_t xcc_id)
354 {
355 	struct mes_map_legacy_queue_input queue_input;
356 	int r;
357 
358 	memset(&queue_input, 0, sizeof(queue_input));
359 
360 	queue_input.xcc_id = xcc_id;
361 	queue_input.queue_type = ring->funcs->type;
362 	queue_input.doorbell_offset = ring->doorbell_index;
363 	queue_input.pipe_id = ring->pipe;
364 	queue_input.queue_id = ring->queue;
365 	queue_input.mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
366 	queue_input.wptr_addr = ring->wptr_gpu_addr;
367 
368 	amdgpu_mes_lock(&adev->mes);
369 	r = adev->mes.funcs->map_legacy_queue(&adev->mes, &queue_input);
370 	amdgpu_mes_unlock(&adev->mes);
371 	if (r)
372 		dev_err(adev->dev, "failed to map legacy queue\n");
373 
374 	return r;
375 }
376 
377 int amdgpu_mes_unmap_legacy_queue(struct amdgpu_device *adev,
378 				  struct amdgpu_ring *ring,
379 				  enum amdgpu_unmap_queues_action action,
380 				  u64 gpu_addr, u64 seq, uint32_t xcc_id)
381 {
382 	struct mes_unmap_legacy_queue_input queue_input;
383 	int r;
384 
385 	queue_input.xcc_id = xcc_id;
386 	queue_input.action = action;
387 	queue_input.queue_type = ring->funcs->type;
388 	queue_input.doorbell_offset = ring->doorbell_index;
389 	queue_input.pipe_id = ring->pipe;
390 	queue_input.queue_id = ring->queue;
391 	queue_input.trail_fence_addr = gpu_addr;
392 	queue_input.trail_fence_data = seq;
393 
394 	amdgpu_mes_lock(&adev->mes);
395 	r = adev->mes.funcs->unmap_legacy_queue(&adev->mes, &queue_input);
396 	amdgpu_mes_unlock(&adev->mes);
397 	if (r)
398 		dev_err(adev->dev, "failed to unmap legacy queue\n");
399 
400 	return r;
401 }
402 
403 int amdgpu_mes_reset_legacy_queue(struct amdgpu_device *adev,
404 				  struct amdgpu_ring *ring,
405 				  unsigned int vmid,
406 				  bool use_mmio,
407 				  uint32_t xcc_id)
408 {
409 	struct mes_reset_queue_input queue_input;
410 	int r;
411 
412 	memset(&queue_input, 0, sizeof(queue_input));
413 
414 	queue_input.xcc_id = xcc_id;
415 	queue_input.queue_type = ring->funcs->type;
416 	queue_input.doorbell_offset = ring->doorbell_index;
417 	queue_input.me_id = ring->me;
418 	queue_input.pipe_id = ring->pipe;
419 	queue_input.queue_id = ring->queue;
420 	queue_input.mqd_addr = ring->mqd_obj ? amdgpu_bo_gpu_offset(ring->mqd_obj) : 0;
421 	queue_input.wptr_addr = ring->wptr_gpu_addr;
422 	queue_input.vmid = vmid;
423 	queue_input.use_mmio = use_mmio;
424 	queue_input.is_kq = true;
425 	if (ring->funcs->type == AMDGPU_RING_TYPE_GFX)
426 		queue_input.legacy_gfx = true;
427 
428 	amdgpu_mes_lock(&adev->mes);
429 	r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
430 	amdgpu_mes_unlock(&adev->mes);
431 	if (r)
432 		dev_err(adev->dev, "failed to reset legacy queue\n");
433 
434 	return r;
435 }
436 
437 int amdgpu_mes_get_hung_queue_db_array_size(struct amdgpu_device *adev)
438 {
439 	return adev->mes.hung_queue_db_array_size;
440 }
441 
442 int amdgpu_mes_detect_and_reset_hung_queues(struct amdgpu_device *adev,
443 					    int queue_type,
444 					    bool detect_only,
445 					    unsigned int *hung_db_num,
446 					    u32 *hung_db_array,
447 					    uint32_t xcc_id)
448 {
449 	struct mes_detect_and_reset_queue_input input;
450 	u32 *db_array = adev->mes.hung_queue_db_array_cpu_addr[xcc_id];
451 	int hqd_info_offset = adev->mes.hung_queue_hqd_info_offset, r, i;
452 
453 	if (!hung_db_num || !hung_db_array)
454 		return -EINVAL;
455 
456 	if ((queue_type != AMDGPU_RING_TYPE_GFX) &&
457 	    (queue_type != AMDGPU_RING_TYPE_COMPUTE) &&
458 	    (queue_type != AMDGPU_RING_TYPE_SDMA))
459 		return -EINVAL;
460 
461 	/* Clear the doorbell array before detection */
462 	memset(adev->mes.hung_queue_db_array_cpu_addr[xcc_id], AMDGPU_MES_INVALID_DB_OFFSET,
463 		adev->mes.hung_queue_db_array_size * sizeof(u32));
464 	input.queue_type = queue_type;
465 	input.detect_only = detect_only;
466 
467 	r = adev->mes.funcs->detect_and_reset_hung_queues(&adev->mes,
468 							  &input);
469 
470 	if (r && detect_only) {
471 		dev_err(adev->dev, "Failed to detect hung queues\n");
472 		return r;
473 	}
474 
475 	*hung_db_num = 0;
476 	/* MES passes hung queues' doorbell to driver */
477 	for (i = 0; i < adev->mes.hung_queue_hqd_info_offset; i++) {
478 		/* Finding hung queues where db_array[i] is a valid doorbell */
479 		if (db_array[i] != AMDGPU_MES_INVALID_DB_OFFSET) {
480 			hung_db_array[i] = db_array[i];
481 			*hung_db_num += 1;
482 		}
483 	}
484 
485 	if (r && !(*hung_db_num)) {
486 		dev_err(adev->dev, "Failed to detect and reset hung queues\n");
487 		return r;
488 	}
489 
490 	for (i = hqd_info_offset; i < hqd_info_offset + *hung_db_num; i++)
491 		hung_db_array[i] = db_array[i];
492 
493 	return r;
494 }
495 
496 uint32_t amdgpu_mes_rreg(struct amdgpu_device *adev, uint32_t reg,
497 			 uint32_t xcc_id)
498 {
499 	struct mes_misc_op_input op_input;
500 	int r, val = 0;
501 	uint32_t addr_offset = 0;
502 	uint64_t read_val_gpu_addr;
503 	uint32_t *read_val_ptr;
504 
505 	if (amdgpu_device_wb_get(adev, &addr_offset)) {
506 		dev_err(adev->dev, "critical bug! too many mes readers\n");
507 		goto error;
508 	}
509 	read_val_gpu_addr = adev->wb.gpu_addr + (addr_offset * 4);
510 	read_val_ptr = (uint32_t *)&adev->wb.wb[addr_offset];
511 	op_input.xcc_id = xcc_id;
512 	op_input.op = MES_MISC_OP_READ_REG;
513 	op_input.read_reg.reg_offset = reg;
514 	op_input.read_reg.buffer_addr = read_val_gpu_addr;
515 
516 	if (!adev->mes.funcs->misc_op) {
517 		dev_err(adev->dev, "mes rreg is not supported!\n");
518 		goto error;
519 	}
520 
521 	amdgpu_mes_lock(&adev->mes);
522 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
523 	amdgpu_mes_unlock(&adev->mes);
524 	if (r)
525 		dev_err(adev->dev, "failed to read reg (0x%x)\n", reg);
526 	else
527 		val = *(read_val_ptr);
528 
529 error:
530 	if (addr_offset)
531 		amdgpu_device_wb_free(adev, addr_offset);
532 	return val;
533 }
534 
535 int amdgpu_mes_wreg(struct amdgpu_device *adev, uint32_t reg,
536 		    uint32_t val, uint32_t xcc_id)
537 {
538 	struct mes_misc_op_input op_input;
539 	int r;
540 
541 	op_input.xcc_id = xcc_id;
542 	op_input.op = MES_MISC_OP_WRITE_REG;
543 	op_input.write_reg.reg_offset = reg;
544 	op_input.write_reg.reg_value = val;
545 
546 	if (!adev->mes.funcs->misc_op) {
547 		dev_err(adev->dev, "mes wreg is not supported!\n");
548 		r = -EINVAL;
549 		goto error;
550 	}
551 
552 	amdgpu_mes_lock(&adev->mes);
553 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
554 	amdgpu_mes_unlock(&adev->mes);
555 	if (r)
556 		dev_err(adev->dev, "failed to write reg (0x%x)\n", reg);
557 
558 error:
559 	return r;
560 }
561 
562 int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,
563 				  uint32_t reg0, uint32_t reg1,
564 				  uint32_t ref, uint32_t mask,
565 				  uint32_t xcc_id)
566 {
567 	struct mes_misc_op_input op_input;
568 	int r;
569 
570 	op_input.xcc_id = xcc_id;
571 	op_input.op = MES_MISC_OP_WRM_REG_WR_WAIT;
572 	op_input.wrm_reg.reg0 = reg0;
573 	op_input.wrm_reg.reg1 = reg1;
574 	op_input.wrm_reg.ref = ref;
575 	op_input.wrm_reg.mask = mask;
576 
577 	if (!adev->mes.funcs->misc_op) {
578 		dev_err(adev->dev, "mes reg_write_reg_wait is not supported!\n");
579 		r = -EINVAL;
580 		goto error;
581 	}
582 
583 	amdgpu_mes_lock(&adev->mes);
584 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
585 	amdgpu_mes_unlock(&adev->mes);
586 	if (r)
587 		dev_err(adev->dev, "failed to reg_write_reg_wait\n");
588 
589 error:
590 	return r;
591 }
592 
593 int amdgpu_mes_hdp_flush(struct amdgpu_device *adev)
594 {
595 	uint32_t hdp_flush_req_offset, hdp_flush_done_offset;
596 	struct amdgpu_ring *mes_ring;
597 	uint32_t ref_and_mask = 0, reg_mem_engine = 0;
598 
599 	if (!adev->gfx.funcs->get_hdp_flush_mask) {
600 		dev_err(adev->dev, "mes hdp flush is not supported.\n");
601 		return -EINVAL;
602 	}
603 
604 	mes_ring = &adev->mes.ring[0];
605 	hdp_flush_req_offset = adev->nbio.funcs->get_hdp_flush_req_offset(adev);
606 	hdp_flush_done_offset = adev->nbio.funcs->get_hdp_flush_done_offset(adev);
607 
608 	adev->gfx.funcs->get_hdp_flush_mask(mes_ring, &ref_and_mask, &reg_mem_engine);
609 
610 	return amdgpu_mes_reg_write_reg_wait(adev, hdp_flush_req_offset, hdp_flush_done_offset,
611 					     ref_and_mask, ref_and_mask, 0);
612 }
613 
614 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
615 				uint64_t process_context_addr,
616 				uint32_t spi_gdbg_per_vmid_cntl,
617 				const uint32_t *tcp_watch_cntl,
618 				uint32_t flags,
619 				bool trap_en,
620 				uint32_t xcc_id)
621 {
622 	struct mes_misc_op_input op_input = {0};
623 	int r;
624 
625 	if (!adev->mes.funcs->misc_op) {
626 		dev_err(adev->dev,
627 			"mes set shader debugger is not supported!\n");
628 		return -EINVAL;
629 	}
630 
631 	op_input.xcc_id = xcc_id;
632 	op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
633 	op_input.set_shader_debugger.process_context_addr = process_context_addr;
634 	op_input.set_shader_debugger.flags.u32all = flags;
635 
636 	/* use amdgpu mes_flush_shader_debugger instead */
637 	if (op_input.set_shader_debugger.flags.process_ctx_flush)
638 		return -EINVAL;
639 
640 	op_input.set_shader_debugger.spi_gdbg_per_vmid_cntl = spi_gdbg_per_vmid_cntl;
641 	memcpy(op_input.set_shader_debugger.tcp_watch_cntl, tcp_watch_cntl,
642 			sizeof(op_input.set_shader_debugger.tcp_watch_cntl));
643 
644 	if (((adev->mes.sched_version & AMDGPU_MES_API_VERSION_MASK) >>
645 			AMDGPU_MES_API_VERSION_SHIFT) >= 14)
646 		op_input.set_shader_debugger.trap_en = trap_en;
647 
648 	amdgpu_mes_lock(&adev->mes);
649 
650 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
651 	if (r)
652 		dev_err(adev->dev, "failed to set_shader_debugger\n");
653 
654 	amdgpu_mes_unlock(&adev->mes);
655 
656 	return r;
657 }
658 
659 int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
660 				     uint64_t process_context_addr,
661 				     uint32_t xcc_id)
662 {
663 	struct mes_misc_op_input op_input = {0};
664 	int r;
665 
666 	if (!adev->mes.funcs->misc_op) {
667 		dev_err(adev->dev,
668 			"mes flush shader debugger is not supported!\n");
669 		return -EINVAL;
670 	}
671 
672 	op_input.xcc_id = xcc_id;
673 	op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
674 	op_input.set_shader_debugger.process_context_addr = process_context_addr;
675 	op_input.set_shader_debugger.flags.process_ctx_flush = true;
676 
677 	amdgpu_mes_lock(&adev->mes);
678 
679 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
680 	if (r)
681 		dev_err(adev->dev, "failed to set_shader_debugger\n");
682 
683 	amdgpu_mes_unlock(&adev->mes);
684 
685 	return r;
686 }
687 
688 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
689 						   enum amdgpu_mes_priority_level prio)
690 {
691 	return adev->mes.aggregated_doorbells[prio];
692 }
693 
694 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe)
695 {
696 	const struct mes_firmware_header_v1_0 *mes_hdr;
697 	struct amdgpu_firmware_info *info;
698 	char ucode_prefix[30];
699 	char fw_name[50];
700 	bool need_retry = false;
701 	u32 *ucode_ptr;
702 	int r;
703 
704 	amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix,
705 				       sizeof(ucode_prefix));
706 	if (adev->enable_uni_mes) {
707 		snprintf(fw_name, sizeof(fw_name),
708 			 "amdgpu/%s_uni_mes.bin", ucode_prefix);
709 	} else if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0) &&
710 	    amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(12, 0, 0)) {
711 		snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
712 			 ucode_prefix,
713 			 pipe == AMDGPU_MES_SCHED_PIPE ? "_2" : "1");
714 		need_retry = true;
715 	} else {
716 		snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
717 			 ucode_prefix,
718 			 pipe == AMDGPU_MES_SCHED_PIPE ? "" : "1");
719 	}
720 
721 	r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe], AMDGPU_UCODE_REQUIRED,
722 				 "%s", fw_name);
723 	if (r && need_retry && pipe == AMDGPU_MES_SCHED_PIPE) {
724 		dev_info(adev->dev, "try to fall back to %s_mes.bin\n", ucode_prefix);
725 		r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe],
726 					 AMDGPU_UCODE_REQUIRED,
727 					 "amdgpu/%s_mes.bin", ucode_prefix);
728 	}
729 
730 	if (r)
731 		goto out;
732 
733 	mes_hdr = (const struct mes_firmware_header_v1_0 *)
734 		adev->mes.fw[pipe]->data;
735 	adev->mes.uc_start_addr[pipe] =
736 		le32_to_cpu(mes_hdr->mes_uc_start_addr_lo) |
737 		((uint64_t)(le32_to_cpu(mes_hdr->mes_uc_start_addr_hi)) << 32);
738 	adev->mes.data_start_addr[pipe] =
739 		le32_to_cpu(mes_hdr->mes_data_start_addr_lo) |
740 		((uint64_t)(le32_to_cpu(mes_hdr->mes_data_start_addr_hi)) << 32);
741 	ucode_ptr = (u32 *)(adev->mes.fw[pipe]->data +
742 			  sizeof(union amdgpu_firmware_header));
743 	adev->mes.fw_version[pipe] =
744 		le32_to_cpu(ucode_ptr[24]) & AMDGPU_MES_VERSION_MASK;
745 
746 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
747 		int ucode, ucode_data;
748 
749 		if (pipe == AMDGPU_MES_SCHED_PIPE) {
750 			ucode = AMDGPU_UCODE_ID_CP_MES;
751 			ucode_data = AMDGPU_UCODE_ID_CP_MES_DATA;
752 		} else {
753 			ucode = AMDGPU_UCODE_ID_CP_MES1;
754 			ucode_data = AMDGPU_UCODE_ID_CP_MES1_DATA;
755 		}
756 
757 		info = &adev->firmware.ucode[ucode];
758 		info->ucode_id = ucode;
759 		info->fw = adev->mes.fw[pipe];
760 		adev->firmware.fw_size +=
761 			ALIGN(le32_to_cpu(mes_hdr->mes_ucode_size_bytes),
762 			      PAGE_SIZE);
763 
764 		info = &adev->firmware.ucode[ucode_data];
765 		info->ucode_id = ucode_data;
766 		info->fw = adev->mes.fw[pipe];
767 		adev->firmware.fw_size +=
768 			ALIGN(le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes),
769 			      PAGE_SIZE);
770 	}
771 
772 	return 0;
773 out:
774 	amdgpu_ucode_release(&adev->mes.fw[pipe]);
775 	return r;
776 }
777 
778 bool amdgpu_mes_suspend_resume_all_supported(struct amdgpu_device *adev)
779 {
780 	uint32_t mes_rev = adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;
781 
782 	return ((amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0) &&
783 		 amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(12, 0, 0) &&
784 		 mes_rev >= 0x63) ||
785 		amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(12, 0, 0));
786 }
787 
788 /* Fix me -- node_id is used to identify the correct MES instances in the future */
789 static int amdgpu_mes_set_enforce_isolation(struct amdgpu_device *adev,
790 					    uint32_t node_id, bool enable)
791 {
792 	struct mes_misc_op_input op_input = {0};
793 	int r;
794 
795 	op_input.op = MES_MISC_OP_CHANGE_CONFIG;
796 	op_input.change_config.option.limit_single_process = enable ? 1 : 0;
797 
798 	if (!adev->mes.funcs->misc_op) {
799 		dev_err(adev->dev, "mes change config is not supported!\n");
800 		r = -EINVAL;
801 		goto error;
802 	}
803 
804 	amdgpu_mes_lock(&adev->mes);
805 	r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
806 	amdgpu_mes_unlock(&adev->mes);
807 	if (r)
808 		dev_err(adev->dev, "failed to change_config.\n");
809 
810 error:
811 	return r;
812 }
813 
814 int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev)
815 {
816 	int i, r = 0;
817 
818 	if (adev->enable_mes && adev->gfx.enable_cleaner_shader) {
819 		mutex_lock(&adev->enforce_isolation_mutex);
820 		for (i = 0; i < (adev->xcp_mgr ? adev->xcp_mgr->num_xcps : 1); i++) {
821 			if (adev->enforce_isolation[i] == AMDGPU_ENFORCE_ISOLATION_ENABLE)
822 				r |= amdgpu_mes_set_enforce_isolation(adev, i, true);
823 			else
824 				r |= amdgpu_mes_set_enforce_isolation(adev, i, false);
825 		}
826 		mutex_unlock(&adev->enforce_isolation_mutex);
827 	}
828 	return r;
829 }
830 
831 #if defined(CONFIG_DEBUG_FS)
832 
833 static int amdgpu_debugfs_mes_event_log_show(struct seq_file *m, void *unused)
834 {
835 	struct amdgpu_device *adev = m->private;
836 	uint32_t *mem = (uint32_t *)(adev->mes.event_log_cpu_addr);
837 
838 	seq_hex_dump(m, "", DUMP_PREFIX_OFFSET, 32, 4,
839 		     mem, adev->mes.event_log_size, false);
840 
841 	return 0;
842 }
843 
844 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_mes_event_log);
845 
846 #endif
847 
848 void amdgpu_debugfs_mes_event_log_init(struct amdgpu_device *adev)
849 {
850 
851 #if defined(CONFIG_DEBUG_FS)
852 	struct drm_minor *minor = adev_to_drm(adev)->primary;
853 	struct dentry *root = minor->debugfs_root;
854 	if (adev->enable_mes && amdgpu_mes_log_enable)
855 		debugfs_create_file("amdgpu_mes_event_log", 0444, root,
856 				    adev, &amdgpu_debugfs_mes_event_log_fops);
857 
858 #endif
859 }
860