2 * Copyright 2019 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/firmware.h>
26 #include "amdgpu_mes.h"
28 #include "soc15_common.h"
29 #include "amdgpu_mes_ctx.h"
31 #define AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
32 #define AMDGPU_ONE_DOORBELL_SIZE 8
34 int amdgpu_mes_doorbell_process_slice(struct amdgpu_device *adev)
36 return roundup(AMDGPU_ONE_DOORBELL_SIZE *
37 AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
41 int amdgpu_mes_alloc_process_doorbells(struct amdgpu_device *adev,
42 unsigned int *doorbell_index)
44 int r = ida_simple_get(&adev->mes.doorbell_ida, 2,
45 adev->mes.max_doorbell_slices,
53 void amdgpu_mes_free_process_doorbells(struct amdgpu_device *adev,
54 unsigned int doorbell_index)
57 ida_simple_remove(&adev->mes.doorbell_ida, doorbell_index);
60 unsigned int amdgpu_mes_get_doorbell_dw_offset_in_bar(
61 struct amdgpu_device *adev,
62 uint32_t doorbell_index,
63 unsigned int doorbell_id)
65 return ((doorbell_index *
66 amdgpu_mes_doorbell_process_slice(adev)) / sizeof(u32) +
70 static int amdgpu_mes_queue_doorbell_get(struct amdgpu_device *adev,
71 struct amdgpu_mes_process *process,
72 int ip_type, uint64_t *doorbell_index)
74 unsigned int offset, found;
76 if (ip_type == AMDGPU_RING_TYPE_SDMA) {
77 offset = adev->doorbell_index.sdma_engine[0];
78 found = find_next_zero_bit(process->doorbell_bitmap,
79 AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
82 found = find_first_zero_bit(process->doorbell_bitmap,
83 AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS);
86 if (found >= AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS) {
87 DRM_WARN("No doorbell available\n");
91 set_bit(found, process->doorbell_bitmap);
93 *doorbell_index = amdgpu_mes_get_doorbell_dw_offset_in_bar(adev,
94 process->doorbell_index, found);
99 static void amdgpu_mes_queue_doorbell_free(struct amdgpu_device *adev,
100 struct amdgpu_mes_process *process,
101 uint32_t doorbell_index)
103 unsigned int old, doorbell_id;
105 doorbell_id = doorbell_index -
106 (process->doorbell_index *
107 amdgpu_mes_doorbell_process_slice(adev)) / sizeof(u32);
110 old = test_and_clear_bit(doorbell_id, process->doorbell_bitmap);
114 static int amdgpu_mes_doorbell_init(struct amdgpu_device *adev)
116 size_t doorbell_start_offset;
117 size_t doorbell_aperture_size;
118 size_t doorbell_process_limit;
119 size_t aggregated_doorbell_start;
122 aggregated_doorbell_start = (adev->doorbell_index.max_assignment + 1) * sizeof(u32);
123 aggregated_doorbell_start =
124 roundup(aggregated_doorbell_start, PAGE_SIZE);
126 doorbell_start_offset = aggregated_doorbell_start + PAGE_SIZE;
127 doorbell_start_offset =
128 roundup(doorbell_start_offset,
129 amdgpu_mes_doorbell_process_slice(adev));
131 doorbell_aperture_size = adev->doorbell.size;
132 doorbell_aperture_size =
133 rounddown(doorbell_aperture_size,
134 amdgpu_mes_doorbell_process_slice(adev));
136 if (doorbell_aperture_size > doorbell_start_offset)
137 doorbell_process_limit =
138 (doorbell_aperture_size - doorbell_start_offset) /
139 amdgpu_mes_doorbell_process_slice(adev);
143 adev->mes.doorbell_id_offset = doorbell_start_offset / sizeof(u32);
144 adev->mes.max_doorbell_slices = doorbell_process_limit;
146 /* allocate Qword range for aggregated doorbell */
147 for (i = 0; i < AMDGPU_MES_PRIORITY_NUM_LEVELS; i++)
148 adev->mes.aggregated_doorbells[i] =
149 aggregated_doorbell_start / sizeof(u32) + i * 2;
151 DRM_INFO("max_doorbell_slices=%zu\n", doorbell_process_limit);
155 int amdgpu_mes_init(struct amdgpu_device *adev)
159 adev->mes.adev = adev;
161 idr_init(&adev->mes.pasid_idr);
162 idr_init(&adev->mes.gang_id_idr);
163 idr_init(&adev->mes.queue_id_idr);
164 ida_init(&adev->mes.doorbell_ida);
165 spin_lock_init(&adev->mes.queue_id_lock);
166 spin_lock_init(&adev->mes.ring_lock);
167 mutex_init(&adev->mes.mutex_hidden);
169 adev->mes.total_max_queue = AMDGPU_FENCE_MES_QUEUE_ID_MASK;
170 adev->mes.vmid_mask_mmhub = 0xffffff00;
171 adev->mes.vmid_mask_gfxhub = 0xffffff00;
173 for (i = 0; i < AMDGPU_MES_MAX_COMPUTE_PIPES; i++) {
174 /* use only 1st MEC pipes */
177 adev->mes.compute_hqd_mask[i] = 0xc;
180 for (i = 0; i < AMDGPU_MES_MAX_GFX_PIPES; i++)
181 adev->mes.gfx_hqd_mask[i] = i ? 0 : 0xfffffffe;
183 for (i = 0; i < AMDGPU_MES_MAX_SDMA_PIPES; i++) {
184 if (adev->ip_versions[SDMA0_HWIP][0] < IP_VERSION(6, 0, 0))
185 adev->mes.sdma_hqd_mask[i] = i ? 0 : 0x3fc;
186 /* zero sdma_hqd_mask for non-existent engine */
187 else if (adev->sdma.num_instances == 1)
188 adev->mes.sdma_hqd_mask[i] = i ? 0 : 0xfc;
190 adev->mes.sdma_hqd_mask[i] = 0xfc;
193 r = amdgpu_device_wb_get(adev, &adev->mes.sch_ctx_offs);
196 "(%d) ring trail_fence_offs wb alloc failed\n", r);
199 adev->mes.sch_ctx_gpu_addr =
200 adev->wb.gpu_addr + (adev->mes.sch_ctx_offs * 4);
201 adev->mes.sch_ctx_ptr =
202 (uint64_t *)&adev->wb.wb[adev->mes.sch_ctx_offs];
204 r = amdgpu_device_wb_get(adev, &adev->mes.query_status_fence_offs);
206 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
208 "(%d) query_status_fence_offs wb alloc failed\n", r);
211 adev->mes.query_status_fence_gpu_addr =
212 adev->wb.gpu_addr + (adev->mes.query_status_fence_offs * 4);
213 adev->mes.query_status_fence_ptr =
214 (uint64_t *)&adev->wb.wb[adev->mes.query_status_fence_offs];
216 r = amdgpu_device_wb_get(adev, &adev->mes.read_val_offs);
218 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
219 amdgpu_device_wb_free(adev, adev->mes.query_status_fence_offs);
221 "(%d) read_val_offs alloc failed\n", r);
224 adev->mes.read_val_gpu_addr =
225 adev->wb.gpu_addr + (adev->mes.read_val_offs * 4);
226 adev->mes.read_val_ptr =
227 (uint32_t *)&adev->wb.wb[adev->mes.read_val_offs];
229 r = amdgpu_mes_doorbell_init(adev);
236 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
237 amdgpu_device_wb_free(adev, adev->mes.query_status_fence_offs);
238 amdgpu_device_wb_free(adev, adev->mes.read_val_offs);
240 idr_destroy(&adev->mes.pasid_idr);
241 idr_destroy(&adev->mes.gang_id_idr);
242 idr_destroy(&adev->mes.queue_id_idr);
243 ida_destroy(&adev->mes.doorbell_ida);
244 mutex_destroy(&adev->mes.mutex_hidden);
248 void amdgpu_mes_fini(struct amdgpu_device *adev)
250 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
251 amdgpu_device_wb_free(adev, adev->mes.query_status_fence_offs);
252 amdgpu_device_wb_free(adev, adev->mes.read_val_offs);
254 idr_destroy(&adev->mes.pasid_idr);
255 idr_destroy(&adev->mes.gang_id_idr);
256 idr_destroy(&adev->mes.queue_id_idr);
257 ida_destroy(&adev->mes.doorbell_ida);
258 mutex_destroy(&adev->mes.mutex_hidden);
261 static void amdgpu_mes_queue_free_mqd(struct amdgpu_mes_queue *q)
263 amdgpu_bo_free_kernel(&q->mqd_obj,
268 int amdgpu_mes_create_process(struct amdgpu_device *adev, int pasid,
269 struct amdgpu_vm *vm)
271 struct amdgpu_mes_process *process;
274 /* allocate the mes process buffer */
275 process = kzalloc(sizeof(struct amdgpu_mes_process), GFP_KERNEL);
277 DRM_ERROR("no more memory to create mes process\n");
281 process->doorbell_bitmap =
282 kzalloc(DIV_ROUND_UP(AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
283 BITS_PER_BYTE), GFP_KERNEL);
284 if (!process->doorbell_bitmap) {
285 DRM_ERROR("failed to allocate doorbell bitmap\n");
290 /* allocate the process context bo and map it */
291 r = amdgpu_bo_create_kernel(adev, AMDGPU_MES_PROC_CTX_SIZE, PAGE_SIZE,
292 AMDGPU_GEM_DOMAIN_GTT,
293 &process->proc_ctx_bo,
294 &process->proc_ctx_gpu_addr,
295 &process->proc_ctx_cpu_ptr);
297 DRM_ERROR("failed to allocate process context bo\n");
298 goto clean_up_memory;
300 memset(process->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
303 * Avoid taking any other locks under MES lock to avoid circular
306 amdgpu_mes_lock(&adev->mes);
308 /* add the mes process to idr list */
309 r = idr_alloc(&adev->mes.pasid_idr, process, pasid, pasid + 1,
312 DRM_ERROR("failed to lock pasid=%d\n", pasid);
316 /* allocate the starting doorbell index of the process */
317 r = amdgpu_mes_alloc_process_doorbells(adev, &process->doorbell_index);
319 DRM_ERROR("failed to allocate doorbell for process\n");
323 DRM_DEBUG("process doorbell index = %d\n", process->doorbell_index);
325 INIT_LIST_HEAD(&process->gang_list);
327 process->pasid = pasid;
328 process->process_quantum = adev->mes.default_process_quantum;
329 process->pd_gpu_addr = amdgpu_bo_gpu_offset(vm->root.bo);
331 amdgpu_mes_unlock(&adev->mes);
335 idr_remove(&adev->mes.pasid_idr, pasid);
336 amdgpu_mes_unlock(&adev->mes);
338 amdgpu_bo_free_kernel(&process->proc_ctx_bo,
339 &process->proc_ctx_gpu_addr,
340 &process->proc_ctx_cpu_ptr);
342 kfree(process->doorbell_bitmap);
347 void amdgpu_mes_destroy_process(struct amdgpu_device *adev, int pasid)
349 struct amdgpu_mes_process *process;
350 struct amdgpu_mes_gang *gang, *tmp1;
351 struct amdgpu_mes_queue *queue, *tmp2;
352 struct mes_remove_queue_input queue_input;
357 * Avoid taking any other locks under MES lock to avoid circular
360 amdgpu_mes_lock(&adev->mes);
362 process = idr_find(&adev->mes.pasid_idr, pasid);
364 DRM_WARN("pasid %d doesn't exist\n", pasid);
365 amdgpu_mes_unlock(&adev->mes);
369 /* Remove all queues from hardware */
370 list_for_each_entry_safe(gang, tmp1, &process->gang_list, list) {
371 list_for_each_entry_safe(queue, tmp2, &gang->queue_list, list) {
372 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
373 idr_remove(&adev->mes.queue_id_idr, queue->queue_id);
374 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
376 queue_input.doorbell_offset = queue->doorbell_off;
377 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
379 r = adev->mes.funcs->remove_hw_queue(&adev->mes,
382 DRM_WARN("failed to remove hardware queue\n");
385 idr_remove(&adev->mes.gang_id_idr, gang->gang_id);
388 amdgpu_mes_free_process_doorbells(adev, process->doorbell_index);
389 idr_remove(&adev->mes.pasid_idr, pasid);
390 amdgpu_mes_unlock(&adev->mes);
392 /* free all memory allocated by the process */
393 list_for_each_entry_safe(gang, tmp1, &process->gang_list, list) {
394 /* free all queues in the gang */
395 list_for_each_entry_safe(queue, tmp2, &gang->queue_list, list) {
396 amdgpu_mes_queue_free_mqd(queue);
397 list_del(&queue->list);
400 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
401 &gang->gang_ctx_gpu_addr,
402 &gang->gang_ctx_cpu_ptr);
403 list_del(&gang->list);
407 amdgpu_bo_free_kernel(&process->proc_ctx_bo,
408 &process->proc_ctx_gpu_addr,
409 &process->proc_ctx_cpu_ptr);
410 kfree(process->doorbell_bitmap);
414 int amdgpu_mes_add_gang(struct amdgpu_device *adev, int pasid,
415 struct amdgpu_mes_gang_properties *gprops,
418 struct amdgpu_mes_process *process;
419 struct amdgpu_mes_gang *gang;
422 /* allocate the mes gang buffer */
423 gang = kzalloc(sizeof(struct amdgpu_mes_gang), GFP_KERNEL);
428 /* allocate the gang context bo and map it to cpu space */
429 r = amdgpu_bo_create_kernel(adev, AMDGPU_MES_GANG_CTX_SIZE, PAGE_SIZE,
430 AMDGPU_GEM_DOMAIN_GTT,
432 &gang->gang_ctx_gpu_addr,
433 &gang->gang_ctx_cpu_ptr);
435 DRM_ERROR("failed to allocate process context bo\n");
438 memset(gang->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
441 * Avoid taking any other locks under MES lock to avoid circular
444 amdgpu_mes_lock(&adev->mes);
446 process = idr_find(&adev->mes.pasid_idr, pasid);
448 DRM_ERROR("pasid %d doesn't exist\n", pasid);
453 /* add the mes gang to idr list */
454 r = idr_alloc(&adev->mes.gang_id_idr, gang, 1, 0,
457 DRM_ERROR("failed to allocate idr for gang\n");
464 INIT_LIST_HEAD(&gang->queue_list);
465 gang->process = process;
466 gang->priority = gprops->priority;
467 gang->gang_quantum = gprops->gang_quantum ?
468 gprops->gang_quantum : adev->mes.default_gang_quantum;
469 gang->global_priority_level = gprops->global_priority_level;
470 gang->inprocess_gang_priority = gprops->inprocess_gang_priority;
471 list_add_tail(&gang->list, &process->gang_list);
473 amdgpu_mes_unlock(&adev->mes);
477 amdgpu_mes_unlock(&adev->mes);
478 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
479 &gang->gang_ctx_gpu_addr,
480 &gang->gang_ctx_cpu_ptr);
486 int amdgpu_mes_remove_gang(struct amdgpu_device *adev, int gang_id)
488 struct amdgpu_mes_gang *gang;
491 * Avoid taking any other locks under MES lock to avoid circular
494 amdgpu_mes_lock(&adev->mes);
496 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
498 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
499 amdgpu_mes_unlock(&adev->mes);
503 if (!list_empty(&gang->queue_list)) {
504 DRM_ERROR("queue list is not empty\n");
505 amdgpu_mes_unlock(&adev->mes);
509 idr_remove(&adev->mes.gang_id_idr, gang->gang_id);
510 list_del(&gang->list);
511 amdgpu_mes_unlock(&adev->mes);
513 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
514 &gang->gang_ctx_gpu_addr,
515 &gang->gang_ctx_cpu_ptr);
522 int amdgpu_mes_suspend(struct amdgpu_device *adev)
525 struct amdgpu_mes_process *process;
526 struct amdgpu_mes_gang *gang;
527 struct mes_suspend_gang_input input;
531 * Avoid taking any other locks under MES lock to avoid circular
534 amdgpu_mes_lock(&adev->mes);
536 idp = &adev->mes.pasid_idr;
538 idr_for_each_entry(idp, process, pasid) {
539 list_for_each_entry(gang, &process->gang_list, list) {
540 r = adev->mes.funcs->suspend_gang(&adev->mes, &input);
542 DRM_ERROR("failed to suspend pasid %d gangid %d",
543 pasid, gang->gang_id);
547 amdgpu_mes_unlock(&adev->mes);
551 int amdgpu_mes_resume(struct amdgpu_device *adev)
554 struct amdgpu_mes_process *process;
555 struct amdgpu_mes_gang *gang;
556 struct mes_resume_gang_input input;
560 * Avoid taking any other locks under MES lock to avoid circular
563 amdgpu_mes_lock(&adev->mes);
565 idp = &adev->mes.pasid_idr;
567 idr_for_each_entry(idp, process, pasid) {
568 list_for_each_entry(gang, &process->gang_list, list) {
569 r = adev->mes.funcs->resume_gang(&adev->mes, &input);
571 DRM_ERROR("failed to resume pasid %d gangid %d",
572 pasid, gang->gang_id);
576 amdgpu_mes_unlock(&adev->mes);
580 static int amdgpu_mes_queue_alloc_mqd(struct amdgpu_device *adev,
581 struct amdgpu_mes_queue *q,
582 struct amdgpu_mes_queue_properties *p)
584 struct amdgpu_mqd *mqd_mgr = &adev->mqds[p->queue_type];
585 u32 mqd_size = mqd_mgr->mqd_size;
588 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
589 AMDGPU_GEM_DOMAIN_GTT,
591 &q->mqd_gpu_addr, &q->mqd_cpu_ptr);
593 dev_warn(adev->dev, "failed to create queue mqd bo (%d)", r);
596 memset(q->mqd_cpu_ptr, 0, mqd_size);
598 r = amdgpu_bo_reserve(q->mqd_obj, false);
599 if (unlikely(r != 0))
605 amdgpu_bo_free_kernel(&q->mqd_obj,
611 static void amdgpu_mes_queue_init_mqd(struct amdgpu_device *adev,
612 struct amdgpu_mes_queue *q,
613 struct amdgpu_mes_queue_properties *p)
615 struct amdgpu_mqd *mqd_mgr = &adev->mqds[p->queue_type];
616 struct amdgpu_mqd_prop mqd_prop = {0};
618 mqd_prop.mqd_gpu_addr = q->mqd_gpu_addr;
619 mqd_prop.hqd_base_gpu_addr = p->hqd_base_gpu_addr;
620 mqd_prop.rptr_gpu_addr = p->rptr_gpu_addr;
621 mqd_prop.wptr_gpu_addr = p->wptr_gpu_addr;
622 mqd_prop.queue_size = p->queue_size;
623 mqd_prop.use_doorbell = true;
624 mqd_prop.doorbell_index = p->doorbell_off;
625 mqd_prop.eop_gpu_addr = p->eop_gpu_addr;
626 mqd_prop.hqd_pipe_priority = p->hqd_pipe_priority;
627 mqd_prop.hqd_queue_priority = p->hqd_queue_priority;
628 mqd_prop.hqd_active = false;
630 mqd_mgr->init_mqd(adev, q->mqd_cpu_ptr, &mqd_prop);
632 amdgpu_bo_unreserve(q->mqd_obj);
635 int amdgpu_mes_add_hw_queue(struct amdgpu_device *adev, int gang_id,
636 struct amdgpu_mes_queue_properties *qprops,
639 struct amdgpu_mes_queue *queue;
640 struct amdgpu_mes_gang *gang;
641 struct mes_add_queue_input queue_input;
645 /* allocate the mes queue buffer */
646 queue = kzalloc(sizeof(struct amdgpu_mes_queue), GFP_KERNEL);
648 DRM_ERROR("Failed to allocate memory for queue\n");
652 /* Allocate the queue mqd */
653 r = amdgpu_mes_queue_alloc_mqd(adev, queue, qprops);
655 goto clean_up_memory;
658 * Avoid taking any other locks under MES lock to avoid circular
661 amdgpu_mes_lock(&adev->mes);
663 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
665 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
670 /* add the mes gang to idr list */
671 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
672 r = idr_alloc(&adev->mes.queue_id_idr, queue, 1, 0,
675 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
678 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
679 *queue_id = queue->queue_id = r;
681 /* allocate a doorbell index for the queue */
682 r = amdgpu_mes_queue_doorbell_get(adev, gang->process,
684 &qprops->doorbell_off);
686 goto clean_up_queue_id;
688 /* initialize the queue mqd */
689 amdgpu_mes_queue_init_mqd(adev, queue, qprops);
691 /* add hw queue to mes */
692 queue_input.process_id = gang->process->pasid;
694 queue_input.page_table_base_addr =
695 adev->vm_manager.vram_base_offset + gang->process->pd_gpu_addr -
696 adev->gmc.vram_start;
698 queue_input.process_va_start = 0;
699 queue_input.process_va_end =
700 (adev->vm_manager.max_pfn - 1) << AMDGPU_GPU_PAGE_SHIFT;
701 queue_input.process_quantum = gang->process->process_quantum;
702 queue_input.process_context_addr = gang->process->proc_ctx_gpu_addr;
703 queue_input.gang_quantum = gang->gang_quantum;
704 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
705 queue_input.inprocess_gang_priority = gang->inprocess_gang_priority;
706 queue_input.gang_global_priority_level = gang->global_priority_level;
707 queue_input.doorbell_offset = qprops->doorbell_off;
708 queue_input.mqd_addr = queue->mqd_gpu_addr;
709 queue_input.wptr_addr = qprops->wptr_gpu_addr;
710 queue_input.wptr_mc_addr = qprops->wptr_mc_addr;
711 queue_input.queue_type = qprops->queue_type;
712 queue_input.paging = qprops->paging;
713 queue_input.is_kfd_process = 0;
715 r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
717 DRM_ERROR("failed to add hardware queue to MES, doorbell=0x%llx\n",
718 qprops->doorbell_off);
719 goto clean_up_doorbell;
722 DRM_DEBUG("MES hw queue was added, pasid=%d, gang id=%d, "
723 "queue type=%d, doorbell=0x%llx\n",
724 gang->process->pasid, gang_id, qprops->queue_type,
725 qprops->doorbell_off);
727 queue->ring = qprops->ring;
728 queue->doorbell_off = qprops->doorbell_off;
729 queue->wptr_gpu_addr = qprops->wptr_gpu_addr;
730 queue->queue_type = qprops->queue_type;
731 queue->paging = qprops->paging;
733 queue->ring->mqd_ptr = queue->mqd_cpu_ptr;
734 list_add_tail(&queue->list, &gang->queue_list);
736 amdgpu_mes_unlock(&adev->mes);
740 amdgpu_mes_queue_doorbell_free(adev, gang->process,
741 qprops->doorbell_off);
743 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
744 idr_remove(&adev->mes.queue_id_idr, queue->queue_id);
745 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
747 amdgpu_mes_unlock(&adev->mes);
748 amdgpu_mes_queue_free_mqd(queue);
754 int amdgpu_mes_remove_hw_queue(struct amdgpu_device *adev, int queue_id)
757 struct amdgpu_mes_queue *queue;
758 struct amdgpu_mes_gang *gang;
759 struct mes_remove_queue_input queue_input;
763 * Avoid taking any other locks under MES lock to avoid circular
766 amdgpu_mes_lock(&adev->mes);
768 /* remove the mes gang from idr list */
769 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
771 queue = idr_find(&adev->mes.queue_id_idr, queue_id);
773 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
774 amdgpu_mes_unlock(&adev->mes);
775 DRM_ERROR("queue id %d doesn't exist\n", queue_id);
779 idr_remove(&adev->mes.queue_id_idr, queue_id);
780 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
782 DRM_DEBUG("try to remove queue, doorbell off = 0x%llx\n",
783 queue->doorbell_off);
786 queue_input.doorbell_offset = queue->doorbell_off;
787 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
789 r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
791 DRM_ERROR("failed to remove hardware queue, queue id = %d\n",
794 list_del(&queue->list);
795 amdgpu_mes_queue_doorbell_free(adev, gang->process,
796 queue->doorbell_off);
797 amdgpu_mes_unlock(&adev->mes);
799 amdgpu_mes_queue_free_mqd(queue);
804 int amdgpu_mes_unmap_legacy_queue(struct amdgpu_device *adev,
805 struct amdgpu_ring *ring,
806 enum amdgpu_unmap_queues_action action,
807 u64 gpu_addr, u64 seq)
809 struct mes_unmap_legacy_queue_input queue_input;
812 queue_input.action = action;
813 queue_input.queue_type = ring->funcs->type;
814 queue_input.doorbell_offset = ring->doorbell_index;
815 queue_input.pipe_id = ring->pipe;
816 queue_input.queue_id = ring->queue;
817 queue_input.trail_fence_addr = gpu_addr;
818 queue_input.trail_fence_data = seq;
820 r = adev->mes.funcs->unmap_legacy_queue(&adev->mes, &queue_input);
822 DRM_ERROR("failed to unmap legacy queue\n");
827 uint32_t amdgpu_mes_rreg(struct amdgpu_device *adev, uint32_t reg)
829 struct mes_misc_op_input op_input;
832 op_input.op = MES_MISC_OP_READ_REG;
833 op_input.read_reg.reg_offset = reg;
834 op_input.read_reg.buffer_addr = adev->mes.read_val_gpu_addr;
836 if (!adev->mes.funcs->misc_op) {
837 DRM_ERROR("mes rreg is not supported!\n");
841 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
843 DRM_ERROR("failed to read reg (0x%x)\n", reg);
845 val = *(adev->mes.read_val_ptr);
851 int amdgpu_mes_wreg(struct amdgpu_device *adev,
852 uint32_t reg, uint32_t val)
854 struct mes_misc_op_input op_input;
857 op_input.op = MES_MISC_OP_WRITE_REG;
858 op_input.write_reg.reg_offset = reg;
859 op_input.write_reg.reg_value = val;
861 if (!adev->mes.funcs->misc_op) {
862 DRM_ERROR("mes wreg is not supported!\n");
867 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
869 DRM_ERROR("failed to write reg (0x%x)\n", reg);
875 int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,
876 uint32_t reg0, uint32_t reg1,
877 uint32_t ref, uint32_t mask)
879 struct mes_misc_op_input op_input;
882 op_input.op = MES_MISC_OP_WRM_REG_WR_WAIT;
883 op_input.wrm_reg.reg0 = reg0;
884 op_input.wrm_reg.reg1 = reg1;
885 op_input.wrm_reg.ref = ref;
886 op_input.wrm_reg.mask = mask;
888 if (!adev->mes.funcs->misc_op) {
889 DRM_ERROR("mes reg_write_reg_wait is not supported!\n");
894 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
896 DRM_ERROR("failed to reg_write_reg_wait\n");
902 int amdgpu_mes_reg_wait(struct amdgpu_device *adev, uint32_t reg,
903 uint32_t val, uint32_t mask)
905 struct mes_misc_op_input op_input;
908 op_input.op = MES_MISC_OP_WRM_REG_WAIT;
909 op_input.wrm_reg.reg0 = reg;
910 op_input.wrm_reg.ref = val;
911 op_input.wrm_reg.mask = mask;
913 if (!adev->mes.funcs->misc_op) {
914 DRM_ERROR("mes reg wait is not supported!\n");
919 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
921 DRM_ERROR("failed to reg_write_reg_wait\n");
927 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
928 uint64_t process_context_addr,
929 uint32_t spi_gdbg_per_vmid_cntl,
930 const uint32_t *tcp_watch_cntl,
934 struct mes_misc_op_input op_input = {0};
937 if (!adev->mes.funcs->misc_op) {
938 DRM_ERROR("mes set shader debugger is not supported!\n");
942 op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
943 op_input.set_shader_debugger.process_context_addr = process_context_addr;
944 op_input.set_shader_debugger.flags.u32all = flags;
945 op_input.set_shader_debugger.spi_gdbg_per_vmid_cntl = spi_gdbg_per_vmid_cntl;
946 memcpy(op_input.set_shader_debugger.tcp_watch_cntl, tcp_watch_cntl,
947 sizeof(op_input.set_shader_debugger.tcp_watch_cntl));
949 if (((adev->mes.sched_version & AMDGPU_MES_API_VERSION_MASK) >>
950 AMDGPU_MES_API_VERSION_SHIFT) >= 14)
951 op_input.set_shader_debugger.trap_en = trap_en;
953 amdgpu_mes_lock(&adev->mes);
955 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
957 DRM_ERROR("failed to set_shader_debugger\n");
959 amdgpu_mes_unlock(&adev->mes);
965 amdgpu_mes_ring_to_queue_props(struct amdgpu_device *adev,
966 struct amdgpu_ring *ring,
967 struct amdgpu_mes_queue_properties *props)
969 props->queue_type = ring->funcs->type;
970 props->hqd_base_gpu_addr = ring->gpu_addr;
971 props->rptr_gpu_addr = ring->rptr_gpu_addr;
972 props->wptr_gpu_addr = ring->wptr_gpu_addr;
973 props->wptr_mc_addr =
974 ring->mes_ctx->meta_data_mc_addr + ring->wptr_offs;
975 props->queue_size = ring->ring_size;
976 props->eop_gpu_addr = ring->eop_gpu_addr;
977 props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
978 props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
979 props->paging = false;
983 #define DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(_eng) \
985 if (id_offs < AMDGPU_MES_CTX_MAX_OFFS) \
986 return offsetof(struct amdgpu_mes_ctx_meta_data, \
987 _eng[ring->idx].slots[id_offs]); \
988 else if (id_offs == AMDGPU_MES_CTX_RING_OFFS) \
989 return offsetof(struct amdgpu_mes_ctx_meta_data, \
990 _eng[ring->idx].ring); \
991 else if (id_offs == AMDGPU_MES_CTX_IB_OFFS) \
992 return offsetof(struct amdgpu_mes_ctx_meta_data, \
993 _eng[ring->idx].ib); \
994 else if (id_offs == AMDGPU_MES_CTX_PADDING_OFFS) \
995 return offsetof(struct amdgpu_mes_ctx_meta_data, \
996 _eng[ring->idx].padding); \
999 int amdgpu_mes_ctx_get_offs(struct amdgpu_ring *ring, unsigned int id_offs)
1001 switch (ring->funcs->type) {
1002 case AMDGPU_RING_TYPE_GFX:
1003 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(gfx);
1005 case AMDGPU_RING_TYPE_COMPUTE:
1006 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(compute);
1008 case AMDGPU_RING_TYPE_SDMA:
1009 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(sdma);
1019 int amdgpu_mes_add_ring(struct amdgpu_device *adev, int gang_id,
1020 int queue_type, int idx,
1021 struct amdgpu_mes_ctx_data *ctx_data,
1022 struct amdgpu_ring **out)
1024 struct amdgpu_ring *ring;
1025 struct amdgpu_mes_gang *gang;
1026 struct amdgpu_mes_queue_properties qprops = {0};
1027 int r, queue_id, pasid;
1030 * Avoid taking any other locks under MES lock to avoid circular
1031 * lock dependencies.
1033 amdgpu_mes_lock(&adev->mes);
1034 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
1036 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
1037 amdgpu_mes_unlock(&adev->mes);
1040 pasid = gang->process->pasid;
1042 ring = kzalloc(sizeof(struct amdgpu_ring), GFP_KERNEL);
1044 amdgpu_mes_unlock(&adev->mes);
1048 ring->ring_obj = NULL;
1049 ring->use_doorbell = true;
1050 ring->is_mes_queue = true;
1051 ring->mes_ctx = ctx_data;
1053 ring->no_scheduler = true;
1055 if (queue_type == AMDGPU_RING_TYPE_COMPUTE) {
1056 int offset = offsetof(struct amdgpu_mes_ctx_meta_data,
1057 compute[ring->idx].mec_hpd);
1058 ring->eop_gpu_addr =
1059 amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
1062 switch (queue_type) {
1063 case AMDGPU_RING_TYPE_GFX:
1064 ring->funcs = adev->gfx.gfx_ring[0].funcs;
1066 case AMDGPU_RING_TYPE_COMPUTE:
1067 ring->funcs = adev->gfx.compute_ring[0].funcs;
1069 case AMDGPU_RING_TYPE_SDMA:
1070 ring->funcs = adev->sdma.instance[0].ring.funcs;
1076 r = amdgpu_ring_init(adev, ring, 1024, NULL, 0,
1077 AMDGPU_RING_PRIO_DEFAULT, NULL);
1079 goto clean_up_memory;
1081 amdgpu_mes_ring_to_queue_props(adev, ring, &qprops);
1083 dma_fence_wait(gang->process->vm->last_update, false);
1084 dma_fence_wait(ctx_data->meta_data_va->last_pt_update, false);
1085 amdgpu_mes_unlock(&adev->mes);
1087 r = amdgpu_mes_add_hw_queue(adev, gang_id, &qprops, &queue_id);
1091 ring->hw_queue_id = queue_id;
1092 ring->doorbell_index = qprops.doorbell_off;
1094 if (queue_type == AMDGPU_RING_TYPE_GFX)
1095 sprintf(ring->name, "gfx_%d.%d.%d", pasid, gang_id, queue_id);
1096 else if (queue_type == AMDGPU_RING_TYPE_COMPUTE)
1097 sprintf(ring->name, "compute_%d.%d.%d", pasid, gang_id,
1099 else if (queue_type == AMDGPU_RING_TYPE_SDMA)
1100 sprintf(ring->name, "sdma_%d.%d.%d", pasid, gang_id,
1109 amdgpu_ring_fini(ring);
1112 amdgpu_mes_unlock(&adev->mes);
1116 void amdgpu_mes_remove_ring(struct amdgpu_device *adev,
1117 struct amdgpu_ring *ring)
1122 amdgpu_mes_remove_hw_queue(adev, ring->hw_queue_id);
1123 amdgpu_ring_fini(ring);
1127 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
1128 enum amdgpu_mes_priority_level prio)
1130 return adev->mes.aggregated_doorbells[prio];
1133 int amdgpu_mes_ctx_alloc_meta_data(struct amdgpu_device *adev,
1134 struct amdgpu_mes_ctx_data *ctx_data)
1138 r = amdgpu_bo_create_kernel(adev,
1139 sizeof(struct amdgpu_mes_ctx_meta_data),
1140 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1141 &ctx_data->meta_data_obj,
1142 &ctx_data->meta_data_mc_addr,
1143 &ctx_data->meta_data_ptr);
1145 dev_warn(adev->dev, "(%d) create CTX bo failed\n", r);
1149 if (!ctx_data->meta_data_obj)
1152 memset(ctx_data->meta_data_ptr, 0,
1153 sizeof(struct amdgpu_mes_ctx_meta_data));
1158 void amdgpu_mes_ctx_free_meta_data(struct amdgpu_mes_ctx_data *ctx_data)
1160 if (ctx_data->meta_data_obj)
1161 amdgpu_bo_free_kernel(&ctx_data->meta_data_obj,
1162 &ctx_data->meta_data_mc_addr,
1163 &ctx_data->meta_data_ptr);
1166 int amdgpu_mes_ctx_map_meta_data(struct amdgpu_device *adev,
1167 struct amdgpu_vm *vm,
1168 struct amdgpu_mes_ctx_data *ctx_data)
1170 struct amdgpu_bo_va *bo_va;
1171 struct ww_acquire_ctx ticket;
1172 struct list_head list;
1173 struct amdgpu_bo_list_entry pd;
1174 struct ttm_validate_buffer csa_tv;
1175 struct amdgpu_sync sync;
1178 amdgpu_sync_create(&sync);
1179 INIT_LIST_HEAD(&list);
1180 INIT_LIST_HEAD(&csa_tv.head);
1182 csa_tv.bo = &ctx_data->meta_data_obj->tbo;
1183 csa_tv.num_shared = 1;
1185 list_add(&csa_tv.head, &list);
1186 amdgpu_vm_get_pd_bo(vm, &list, &pd);
1188 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
1190 DRM_ERROR("failed to reserve meta data BO: err=%d\n", r);
1194 bo_va = amdgpu_vm_bo_add(adev, vm, ctx_data->meta_data_obj);
1196 ttm_eu_backoff_reservation(&ticket, &list);
1197 DRM_ERROR("failed to create bo_va for meta data BO\n");
1201 r = amdgpu_vm_bo_map(adev, bo_va, ctx_data->meta_data_gpu_addr, 0,
1202 sizeof(struct amdgpu_mes_ctx_meta_data),
1203 AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
1204 AMDGPU_PTE_EXECUTABLE);
1207 DRM_ERROR("failed to do bo_map on meta data, err=%d\n", r);
1211 r = amdgpu_vm_bo_update(adev, bo_va, false);
1213 DRM_ERROR("failed to do vm_bo_update on meta data\n");
1216 amdgpu_sync_fence(&sync, bo_va->last_pt_update);
1218 r = amdgpu_vm_update_pdes(adev, vm, false);
1220 DRM_ERROR("failed to update pdes on meta data\n");
1223 amdgpu_sync_fence(&sync, vm->last_update);
1225 amdgpu_sync_wait(&sync, false);
1226 ttm_eu_backoff_reservation(&ticket, &list);
1228 amdgpu_sync_free(&sync);
1229 ctx_data->meta_data_va = bo_va;
1233 amdgpu_vm_bo_del(adev, bo_va);
1234 ttm_eu_backoff_reservation(&ticket, &list);
1235 amdgpu_sync_free(&sync);
1239 int amdgpu_mes_ctx_unmap_meta_data(struct amdgpu_device *adev,
1240 struct amdgpu_mes_ctx_data *ctx_data)
1242 struct amdgpu_bo_va *bo_va = ctx_data->meta_data_va;
1243 struct amdgpu_bo *bo = ctx_data->meta_data_obj;
1244 struct amdgpu_vm *vm = bo_va->base.vm;
1245 struct amdgpu_bo_list_entry vm_pd;
1246 struct list_head list, duplicates;
1247 struct dma_fence *fence = NULL;
1248 struct ttm_validate_buffer tv;
1249 struct ww_acquire_ctx ticket;
1252 INIT_LIST_HEAD(&list);
1253 INIT_LIST_HEAD(&duplicates);
1257 list_add(&tv.head, &list);
1259 amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
1261 r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
1263 dev_err(adev->dev, "leaking bo va because "
1264 "we fail to reserve bo (%ld)\n", r);
1268 amdgpu_vm_bo_del(adev, bo_va);
1269 if (!amdgpu_vm_ready(vm))
1272 r = dma_resv_get_singleton(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP, &fence);
1276 amdgpu_bo_fence(bo, fence, true);
1280 r = amdgpu_vm_clear_freed(adev, vm, &fence);
1284 dma_fence_wait(fence, false);
1285 amdgpu_bo_fence(bo, fence, true);
1286 dma_fence_put(fence);
1289 if (unlikely(r < 0))
1290 dev_err(adev->dev, "failed to clear page tables (%ld)\n", r);
1291 ttm_eu_backoff_reservation(&ticket, &list);
1296 static int amdgpu_mes_test_create_gang_and_queues(struct amdgpu_device *adev,
1297 int pasid, int *gang_id,
1298 int queue_type, int num_queue,
1299 struct amdgpu_ring **added_rings,
1300 struct amdgpu_mes_ctx_data *ctx_data)
1302 struct amdgpu_ring *ring;
1303 struct amdgpu_mes_gang_properties gprops = {0};
1306 /* create a gang for the process */
1307 gprops.priority = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1308 gprops.gang_quantum = adev->mes.default_gang_quantum;
1309 gprops.inprocess_gang_priority = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1310 gprops.priority_level = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1311 gprops.global_priority_level = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1313 r = amdgpu_mes_add_gang(adev, pasid, &gprops, gang_id);
1315 DRM_ERROR("failed to add gang\n");
1319 /* create queues for the gang */
1320 for (j = 0; j < num_queue; j++) {
1321 r = amdgpu_mes_add_ring(adev, *gang_id, queue_type, j,
1324 DRM_ERROR("failed to add ring\n");
1328 DRM_INFO("ring %s was added\n", ring->name);
1329 added_rings[j] = ring;
1335 static int amdgpu_mes_test_queues(struct amdgpu_ring **added_rings)
1337 struct amdgpu_ring *ring;
1340 for (i = 0; i < AMDGPU_MES_CTX_MAX_RINGS; i++) {
1341 ring = added_rings[i];
1345 r = amdgpu_ring_test_helper(ring);
1349 r = amdgpu_ring_test_ib(ring, 1000 * 10);
1351 DRM_DEV_ERROR(ring->adev->dev,
1352 "ring %s ib test failed (%d)\n",
1356 DRM_INFO("ring %s ib test pass\n", ring->name);
1362 int amdgpu_mes_self_test(struct amdgpu_device *adev)
1364 struct amdgpu_vm *vm = NULL;
1365 struct amdgpu_mes_ctx_data ctx_data = {0};
1366 struct amdgpu_ring *added_rings[AMDGPU_MES_CTX_MAX_RINGS] = { NULL };
1367 int gang_ids[3] = {0};
1368 int queue_types[][2] = { { AMDGPU_RING_TYPE_GFX, 1 },
1369 { AMDGPU_RING_TYPE_COMPUTE, 1 },
1370 { AMDGPU_RING_TYPE_SDMA, 1} };
1371 int i, r, pasid, k = 0;
1373 pasid = amdgpu_pasid_alloc(16);
1375 dev_warn(adev->dev, "No more PASIDs available!");
1379 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1385 r = amdgpu_vm_init(adev, vm, -1);
1387 DRM_ERROR("failed to initialize vm\n");
1391 r = amdgpu_mes_ctx_alloc_meta_data(adev, &ctx_data);
1393 DRM_ERROR("failed to alloc ctx meta data\n");
1397 ctx_data.meta_data_gpu_addr = AMDGPU_VA_RESERVED_SIZE;
1398 r = amdgpu_mes_ctx_map_meta_data(adev, vm, &ctx_data);
1400 DRM_ERROR("failed to map ctx meta data\n");
1404 r = amdgpu_mes_create_process(adev, pasid, vm);
1406 DRM_ERROR("failed to create MES process\n");
1410 for (i = 0; i < ARRAY_SIZE(queue_types); i++) {
1411 /* On GFX v10.3, fw hasn't supported to map sdma queue. */
1412 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(10, 3, 0) &&
1413 adev->ip_versions[GC_HWIP][0] < IP_VERSION(11, 0, 0) &&
1414 queue_types[i][0] == AMDGPU_RING_TYPE_SDMA)
1417 r = amdgpu_mes_test_create_gang_and_queues(adev, pasid,
1426 k += queue_types[i][1];
1429 /* start ring test and ib test for MES queues */
1430 amdgpu_mes_test_queues(added_rings);
1433 /* remove all queues */
1434 for (i = 0; i < ARRAY_SIZE(added_rings); i++) {
1435 if (!added_rings[i])
1437 amdgpu_mes_remove_ring(adev, added_rings[i]);
1440 for (i = 0; i < ARRAY_SIZE(gang_ids); i++) {
1443 amdgpu_mes_remove_gang(adev, gang_ids[i]);
1446 amdgpu_mes_destroy_process(adev, pasid);
1449 amdgpu_mes_ctx_unmap_meta_data(adev, &ctx_data);
1452 amdgpu_vm_fini(adev, vm);
1456 amdgpu_pasid_free(pasid);
1458 amdgpu_mes_ctx_free_meta_data(&ctx_data);
1463 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe)
1465 const struct mes_firmware_header_v1_0 *mes_hdr;
1466 struct amdgpu_firmware_info *info;
1467 char ucode_prefix[30];
1469 bool need_retry = false;
1472 amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix,
1473 sizeof(ucode_prefix));
1474 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(11, 0, 0)) {
1475 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
1477 pipe == AMDGPU_MES_SCHED_PIPE ? "_2" : "1");
1480 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
1482 pipe == AMDGPU_MES_SCHED_PIPE ? "" : "1");
1485 r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe], fw_name);
1486 if (r && need_retry && pipe == AMDGPU_MES_SCHED_PIPE) {
1487 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes.bin",
1489 DRM_INFO("try to fall back to %s\n", fw_name);
1490 r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe],
1497 mes_hdr = (const struct mes_firmware_header_v1_0 *)
1498 adev->mes.fw[pipe]->data;
1499 adev->mes.uc_start_addr[pipe] =
1500 le32_to_cpu(mes_hdr->mes_uc_start_addr_lo) |
1501 ((uint64_t)(le32_to_cpu(mes_hdr->mes_uc_start_addr_hi)) << 32);
1502 adev->mes.data_start_addr[pipe] =
1503 le32_to_cpu(mes_hdr->mes_data_start_addr_lo) |
1504 ((uint64_t)(le32_to_cpu(mes_hdr->mes_data_start_addr_hi)) << 32);
1506 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1507 int ucode, ucode_data;
1509 if (pipe == AMDGPU_MES_SCHED_PIPE) {
1510 ucode = AMDGPU_UCODE_ID_CP_MES;
1511 ucode_data = AMDGPU_UCODE_ID_CP_MES_DATA;
1513 ucode = AMDGPU_UCODE_ID_CP_MES1;
1514 ucode_data = AMDGPU_UCODE_ID_CP_MES1_DATA;
1517 info = &adev->firmware.ucode[ucode];
1518 info->ucode_id = ucode;
1519 info->fw = adev->mes.fw[pipe];
1520 adev->firmware.fw_size +=
1521 ALIGN(le32_to_cpu(mes_hdr->mes_ucode_size_bytes),
1524 info = &adev->firmware.ucode[ucode_data];
1525 info->ucode_id = ucode_data;
1526 info->fw = adev->mes.fw[pipe];
1527 adev->firmware.fw_size +=
1528 ALIGN(le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes),
1534 amdgpu_ucode_release(&adev->mes.fw[pipe]);