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>
25 #include <drm/drm_exec.h>
27 #include "amdgpu_mes.h"
29 #include "soc15_common.h"
30 #include "amdgpu_mes_ctx.h"
32 #define AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
33 #define AMDGPU_ONE_DOORBELL_SIZE 8
35 int amdgpu_mes_doorbell_process_slice(struct amdgpu_device *adev)
37 return roundup(AMDGPU_ONE_DOORBELL_SIZE *
38 AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
42 int amdgpu_mes_alloc_process_doorbells(struct amdgpu_device *adev,
43 unsigned int *doorbell_index)
45 int r = ida_simple_get(&adev->mes.doorbell_ida, 2,
46 adev->mes.max_doorbell_slices,
54 void amdgpu_mes_free_process_doorbells(struct amdgpu_device *adev,
55 unsigned int doorbell_index)
58 ida_simple_remove(&adev->mes.doorbell_ida, doorbell_index);
61 unsigned int amdgpu_mes_get_doorbell_dw_offset_in_bar(
62 struct amdgpu_device *adev,
63 uint32_t doorbell_index,
64 unsigned int doorbell_id)
66 return ((doorbell_index *
67 amdgpu_mes_doorbell_process_slice(adev)) / sizeof(u32) +
71 static int amdgpu_mes_queue_doorbell_get(struct amdgpu_device *adev,
72 struct amdgpu_mes_process *process,
73 int ip_type, uint64_t *doorbell_index)
75 unsigned int offset, found;
77 if (ip_type == AMDGPU_RING_TYPE_SDMA) {
78 offset = adev->doorbell_index.sdma_engine[0];
79 found = find_next_zero_bit(process->doorbell_bitmap,
80 AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
83 found = find_first_zero_bit(process->doorbell_bitmap,
84 AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS);
87 if (found >= AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS) {
88 DRM_WARN("No doorbell available\n");
92 set_bit(found, process->doorbell_bitmap);
94 *doorbell_index = amdgpu_mes_get_doorbell_dw_offset_in_bar(adev,
95 process->doorbell_index, found);
100 static void amdgpu_mes_queue_doorbell_free(struct amdgpu_device *adev,
101 struct amdgpu_mes_process *process,
102 uint32_t doorbell_index)
104 unsigned int old, doorbell_id;
106 doorbell_id = doorbell_index -
107 (process->doorbell_index *
108 amdgpu_mes_doorbell_process_slice(adev)) / sizeof(u32);
111 old = test_and_clear_bit(doorbell_id, process->doorbell_bitmap);
115 static int amdgpu_mes_doorbell_init(struct amdgpu_device *adev)
117 size_t doorbell_start_offset;
118 size_t doorbell_aperture_size;
119 size_t doorbell_process_limit;
120 size_t aggregated_doorbell_start;
123 aggregated_doorbell_start = (adev->doorbell_index.max_assignment + 1) * sizeof(u32);
124 aggregated_doorbell_start =
125 roundup(aggregated_doorbell_start, PAGE_SIZE);
127 doorbell_start_offset = aggregated_doorbell_start + PAGE_SIZE;
128 doorbell_start_offset =
129 roundup(doorbell_start_offset,
130 amdgpu_mes_doorbell_process_slice(adev));
132 doorbell_aperture_size = adev->doorbell.size;
133 doorbell_aperture_size =
134 rounddown(doorbell_aperture_size,
135 amdgpu_mes_doorbell_process_slice(adev));
137 if (doorbell_aperture_size > doorbell_start_offset)
138 doorbell_process_limit =
139 (doorbell_aperture_size - doorbell_start_offset) /
140 amdgpu_mes_doorbell_process_slice(adev);
144 adev->mes.doorbell_id_offset = doorbell_start_offset / sizeof(u32);
145 adev->mes.max_doorbell_slices = doorbell_process_limit;
147 /* allocate Qword range for aggregated doorbell */
148 for (i = 0; i < AMDGPU_MES_PRIORITY_NUM_LEVELS; i++)
149 adev->mes.aggregated_doorbells[i] =
150 aggregated_doorbell_start / sizeof(u32) + i * 2;
152 DRM_INFO("max_doorbell_slices=%zu\n", doorbell_process_limit);
156 int amdgpu_mes_init(struct amdgpu_device *adev)
160 adev->mes.adev = adev;
162 idr_init(&adev->mes.pasid_idr);
163 idr_init(&adev->mes.gang_id_idr);
164 idr_init(&adev->mes.queue_id_idr);
165 ida_init(&adev->mes.doorbell_ida);
166 spin_lock_init(&adev->mes.queue_id_lock);
167 spin_lock_init(&adev->mes.ring_lock);
168 mutex_init(&adev->mes.mutex_hidden);
170 adev->mes.total_max_queue = AMDGPU_FENCE_MES_QUEUE_ID_MASK;
171 adev->mes.vmid_mask_mmhub = 0xffffff00;
172 adev->mes.vmid_mask_gfxhub = 0xffffff00;
174 for (i = 0; i < AMDGPU_MES_MAX_COMPUTE_PIPES; i++) {
175 /* use only 1st MEC pipes */
178 adev->mes.compute_hqd_mask[i] = 0xc;
181 for (i = 0; i < AMDGPU_MES_MAX_GFX_PIPES; i++)
182 adev->mes.gfx_hqd_mask[i] = i ? 0 : 0xfffffffe;
184 for (i = 0; i < AMDGPU_MES_MAX_SDMA_PIPES; i++) {
185 if (adev->ip_versions[SDMA0_HWIP][0] < IP_VERSION(6, 0, 0))
186 adev->mes.sdma_hqd_mask[i] = i ? 0 : 0x3fc;
187 /* zero sdma_hqd_mask for non-existent engine */
188 else if (adev->sdma.num_instances == 1)
189 adev->mes.sdma_hqd_mask[i] = i ? 0 : 0xfc;
191 adev->mes.sdma_hqd_mask[i] = 0xfc;
194 r = amdgpu_device_wb_get(adev, &adev->mes.sch_ctx_offs);
197 "(%d) ring trail_fence_offs wb alloc failed\n", r);
200 adev->mes.sch_ctx_gpu_addr =
201 adev->wb.gpu_addr + (adev->mes.sch_ctx_offs * 4);
202 adev->mes.sch_ctx_ptr =
203 (uint64_t *)&adev->wb.wb[adev->mes.sch_ctx_offs];
205 r = amdgpu_device_wb_get(adev, &adev->mes.query_status_fence_offs);
207 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
209 "(%d) query_status_fence_offs wb alloc failed\n", r);
212 adev->mes.query_status_fence_gpu_addr =
213 adev->wb.gpu_addr + (adev->mes.query_status_fence_offs * 4);
214 adev->mes.query_status_fence_ptr =
215 (uint64_t *)&adev->wb.wb[adev->mes.query_status_fence_offs];
217 r = amdgpu_device_wb_get(adev, &adev->mes.read_val_offs);
219 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
220 amdgpu_device_wb_free(adev, adev->mes.query_status_fence_offs);
222 "(%d) read_val_offs alloc failed\n", r);
225 adev->mes.read_val_gpu_addr =
226 adev->wb.gpu_addr + (adev->mes.read_val_offs * 4);
227 adev->mes.read_val_ptr =
228 (uint32_t *)&adev->wb.wb[adev->mes.read_val_offs];
230 r = amdgpu_mes_doorbell_init(adev);
237 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
238 amdgpu_device_wb_free(adev, adev->mes.query_status_fence_offs);
239 amdgpu_device_wb_free(adev, adev->mes.read_val_offs);
241 idr_destroy(&adev->mes.pasid_idr);
242 idr_destroy(&adev->mes.gang_id_idr);
243 idr_destroy(&adev->mes.queue_id_idr);
244 ida_destroy(&adev->mes.doorbell_ida);
245 mutex_destroy(&adev->mes.mutex_hidden);
249 void amdgpu_mes_fini(struct amdgpu_device *adev)
251 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs);
252 amdgpu_device_wb_free(adev, adev->mes.query_status_fence_offs);
253 amdgpu_device_wb_free(adev, adev->mes.read_val_offs);
255 idr_destroy(&adev->mes.pasid_idr);
256 idr_destroy(&adev->mes.gang_id_idr);
257 idr_destroy(&adev->mes.queue_id_idr);
258 ida_destroy(&adev->mes.doorbell_ida);
259 mutex_destroy(&adev->mes.mutex_hidden);
262 static void amdgpu_mes_queue_free_mqd(struct amdgpu_mes_queue *q)
264 amdgpu_bo_free_kernel(&q->mqd_obj,
269 int amdgpu_mes_create_process(struct amdgpu_device *adev, int pasid,
270 struct amdgpu_vm *vm)
272 struct amdgpu_mes_process *process;
275 /* allocate the mes process buffer */
276 process = kzalloc(sizeof(struct amdgpu_mes_process), GFP_KERNEL);
278 DRM_ERROR("no more memory to create mes process\n");
282 process->doorbell_bitmap =
283 kzalloc(DIV_ROUND_UP(AMDGPU_MES_MAX_NUM_OF_QUEUES_PER_PROCESS,
284 BITS_PER_BYTE), GFP_KERNEL);
285 if (!process->doorbell_bitmap) {
286 DRM_ERROR("failed to allocate doorbell bitmap\n");
291 /* allocate the process context bo and map it */
292 r = amdgpu_bo_create_kernel(adev, AMDGPU_MES_PROC_CTX_SIZE, PAGE_SIZE,
293 AMDGPU_GEM_DOMAIN_GTT,
294 &process->proc_ctx_bo,
295 &process->proc_ctx_gpu_addr,
296 &process->proc_ctx_cpu_ptr);
298 DRM_ERROR("failed to allocate process context bo\n");
299 goto clean_up_memory;
301 memset(process->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
304 * Avoid taking any other locks under MES lock to avoid circular
307 amdgpu_mes_lock(&adev->mes);
309 /* add the mes process to idr list */
310 r = idr_alloc(&adev->mes.pasid_idr, process, pasid, pasid + 1,
313 DRM_ERROR("failed to lock pasid=%d\n", pasid);
317 /* allocate the starting doorbell index of the process */
318 r = amdgpu_mes_alloc_process_doorbells(adev, &process->doorbell_index);
320 DRM_ERROR("failed to allocate doorbell for process\n");
324 DRM_DEBUG("process doorbell index = %d\n", process->doorbell_index);
326 INIT_LIST_HEAD(&process->gang_list);
328 process->pasid = pasid;
329 process->process_quantum = adev->mes.default_process_quantum;
330 process->pd_gpu_addr = amdgpu_bo_gpu_offset(vm->root.bo);
332 amdgpu_mes_unlock(&adev->mes);
336 idr_remove(&adev->mes.pasid_idr, pasid);
337 amdgpu_mes_unlock(&adev->mes);
339 amdgpu_bo_free_kernel(&process->proc_ctx_bo,
340 &process->proc_ctx_gpu_addr,
341 &process->proc_ctx_cpu_ptr);
343 kfree(process->doorbell_bitmap);
348 void amdgpu_mes_destroy_process(struct amdgpu_device *adev, int pasid)
350 struct amdgpu_mes_process *process;
351 struct amdgpu_mes_gang *gang, *tmp1;
352 struct amdgpu_mes_queue *queue, *tmp2;
353 struct mes_remove_queue_input queue_input;
358 * Avoid taking any other locks under MES lock to avoid circular
361 amdgpu_mes_lock(&adev->mes);
363 process = idr_find(&adev->mes.pasid_idr, pasid);
365 DRM_WARN("pasid %d doesn't exist\n", pasid);
366 amdgpu_mes_unlock(&adev->mes);
370 /* Remove all queues from hardware */
371 list_for_each_entry_safe(gang, tmp1, &process->gang_list, list) {
372 list_for_each_entry_safe(queue, tmp2, &gang->queue_list, list) {
373 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
374 idr_remove(&adev->mes.queue_id_idr, queue->queue_id);
375 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
377 queue_input.doorbell_offset = queue->doorbell_off;
378 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
380 r = adev->mes.funcs->remove_hw_queue(&adev->mes,
383 DRM_WARN("failed to remove hardware queue\n");
386 idr_remove(&adev->mes.gang_id_idr, gang->gang_id);
389 amdgpu_mes_free_process_doorbells(adev, process->doorbell_index);
390 idr_remove(&adev->mes.pasid_idr, pasid);
391 amdgpu_mes_unlock(&adev->mes);
393 /* free all memory allocated by the process */
394 list_for_each_entry_safe(gang, tmp1, &process->gang_list, list) {
395 /* free all queues in the gang */
396 list_for_each_entry_safe(queue, tmp2, &gang->queue_list, list) {
397 amdgpu_mes_queue_free_mqd(queue);
398 list_del(&queue->list);
401 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
402 &gang->gang_ctx_gpu_addr,
403 &gang->gang_ctx_cpu_ptr);
404 list_del(&gang->list);
408 amdgpu_bo_free_kernel(&process->proc_ctx_bo,
409 &process->proc_ctx_gpu_addr,
410 &process->proc_ctx_cpu_ptr);
411 kfree(process->doorbell_bitmap);
415 int amdgpu_mes_add_gang(struct amdgpu_device *adev, int pasid,
416 struct amdgpu_mes_gang_properties *gprops,
419 struct amdgpu_mes_process *process;
420 struct amdgpu_mes_gang *gang;
423 /* allocate the mes gang buffer */
424 gang = kzalloc(sizeof(struct amdgpu_mes_gang), GFP_KERNEL);
429 /* allocate the gang context bo and map it to cpu space */
430 r = amdgpu_bo_create_kernel(adev, AMDGPU_MES_GANG_CTX_SIZE, PAGE_SIZE,
431 AMDGPU_GEM_DOMAIN_GTT,
433 &gang->gang_ctx_gpu_addr,
434 &gang->gang_ctx_cpu_ptr);
436 DRM_ERROR("failed to allocate process context bo\n");
439 memset(gang->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
442 * Avoid taking any other locks under MES lock to avoid circular
445 amdgpu_mes_lock(&adev->mes);
447 process = idr_find(&adev->mes.pasid_idr, pasid);
449 DRM_ERROR("pasid %d doesn't exist\n", pasid);
454 /* add the mes gang to idr list */
455 r = idr_alloc(&adev->mes.gang_id_idr, gang, 1, 0,
458 DRM_ERROR("failed to allocate idr for gang\n");
465 INIT_LIST_HEAD(&gang->queue_list);
466 gang->process = process;
467 gang->priority = gprops->priority;
468 gang->gang_quantum = gprops->gang_quantum ?
469 gprops->gang_quantum : adev->mes.default_gang_quantum;
470 gang->global_priority_level = gprops->global_priority_level;
471 gang->inprocess_gang_priority = gprops->inprocess_gang_priority;
472 list_add_tail(&gang->list, &process->gang_list);
474 amdgpu_mes_unlock(&adev->mes);
478 amdgpu_mes_unlock(&adev->mes);
479 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
480 &gang->gang_ctx_gpu_addr,
481 &gang->gang_ctx_cpu_ptr);
487 int amdgpu_mes_remove_gang(struct amdgpu_device *adev, int gang_id)
489 struct amdgpu_mes_gang *gang;
492 * Avoid taking any other locks under MES lock to avoid circular
495 amdgpu_mes_lock(&adev->mes);
497 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
499 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
500 amdgpu_mes_unlock(&adev->mes);
504 if (!list_empty(&gang->queue_list)) {
505 DRM_ERROR("queue list is not empty\n");
506 amdgpu_mes_unlock(&adev->mes);
510 idr_remove(&adev->mes.gang_id_idr, gang->gang_id);
511 list_del(&gang->list);
512 amdgpu_mes_unlock(&adev->mes);
514 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
515 &gang->gang_ctx_gpu_addr,
516 &gang->gang_ctx_cpu_ptr);
523 int amdgpu_mes_suspend(struct amdgpu_device *adev)
526 struct amdgpu_mes_process *process;
527 struct amdgpu_mes_gang *gang;
528 struct mes_suspend_gang_input input;
532 * Avoid taking any other locks under MES lock to avoid circular
535 amdgpu_mes_lock(&adev->mes);
537 idp = &adev->mes.pasid_idr;
539 idr_for_each_entry(idp, process, pasid) {
540 list_for_each_entry(gang, &process->gang_list, list) {
541 r = adev->mes.funcs->suspend_gang(&adev->mes, &input);
543 DRM_ERROR("failed to suspend pasid %d gangid %d",
544 pasid, gang->gang_id);
548 amdgpu_mes_unlock(&adev->mes);
552 int amdgpu_mes_resume(struct amdgpu_device *adev)
555 struct amdgpu_mes_process *process;
556 struct amdgpu_mes_gang *gang;
557 struct mes_resume_gang_input input;
561 * Avoid taking any other locks under MES lock to avoid circular
564 amdgpu_mes_lock(&adev->mes);
566 idp = &adev->mes.pasid_idr;
568 idr_for_each_entry(idp, process, pasid) {
569 list_for_each_entry(gang, &process->gang_list, list) {
570 r = adev->mes.funcs->resume_gang(&adev->mes, &input);
572 DRM_ERROR("failed to resume pasid %d gangid %d",
573 pasid, gang->gang_id);
577 amdgpu_mes_unlock(&adev->mes);
581 static int amdgpu_mes_queue_alloc_mqd(struct amdgpu_device *adev,
582 struct amdgpu_mes_queue *q,
583 struct amdgpu_mes_queue_properties *p)
585 struct amdgpu_mqd *mqd_mgr = &adev->mqds[p->queue_type];
586 u32 mqd_size = mqd_mgr->mqd_size;
589 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
590 AMDGPU_GEM_DOMAIN_GTT,
592 &q->mqd_gpu_addr, &q->mqd_cpu_ptr);
594 dev_warn(adev->dev, "failed to create queue mqd bo (%d)", r);
597 memset(q->mqd_cpu_ptr, 0, mqd_size);
599 r = amdgpu_bo_reserve(q->mqd_obj, false);
600 if (unlikely(r != 0))
606 amdgpu_bo_free_kernel(&q->mqd_obj,
612 static void amdgpu_mes_queue_init_mqd(struct amdgpu_device *adev,
613 struct amdgpu_mes_queue *q,
614 struct amdgpu_mes_queue_properties *p)
616 struct amdgpu_mqd *mqd_mgr = &adev->mqds[p->queue_type];
617 struct amdgpu_mqd_prop mqd_prop = {0};
619 mqd_prop.mqd_gpu_addr = q->mqd_gpu_addr;
620 mqd_prop.hqd_base_gpu_addr = p->hqd_base_gpu_addr;
621 mqd_prop.rptr_gpu_addr = p->rptr_gpu_addr;
622 mqd_prop.wptr_gpu_addr = p->wptr_gpu_addr;
623 mqd_prop.queue_size = p->queue_size;
624 mqd_prop.use_doorbell = true;
625 mqd_prop.doorbell_index = p->doorbell_off;
626 mqd_prop.eop_gpu_addr = p->eop_gpu_addr;
627 mqd_prop.hqd_pipe_priority = p->hqd_pipe_priority;
628 mqd_prop.hqd_queue_priority = p->hqd_queue_priority;
629 mqd_prop.hqd_active = false;
631 mqd_mgr->init_mqd(adev, q->mqd_cpu_ptr, &mqd_prop);
633 amdgpu_bo_unreserve(q->mqd_obj);
636 int amdgpu_mes_add_hw_queue(struct amdgpu_device *adev, int gang_id,
637 struct amdgpu_mes_queue_properties *qprops,
640 struct amdgpu_mes_queue *queue;
641 struct amdgpu_mes_gang *gang;
642 struct mes_add_queue_input queue_input;
646 /* allocate the mes queue buffer */
647 queue = kzalloc(sizeof(struct amdgpu_mes_queue), GFP_KERNEL);
649 DRM_ERROR("Failed to allocate memory for queue\n");
653 /* Allocate the queue mqd */
654 r = amdgpu_mes_queue_alloc_mqd(adev, queue, qprops);
656 goto clean_up_memory;
659 * Avoid taking any other locks under MES lock to avoid circular
662 amdgpu_mes_lock(&adev->mes);
664 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
666 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
671 /* add the mes gang to idr list */
672 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
673 r = idr_alloc(&adev->mes.queue_id_idr, queue, 1, 0,
676 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
679 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
680 *queue_id = queue->queue_id = r;
682 /* allocate a doorbell index for the queue */
683 r = amdgpu_mes_queue_doorbell_get(adev, gang->process,
685 &qprops->doorbell_off);
687 goto clean_up_queue_id;
689 /* initialize the queue mqd */
690 amdgpu_mes_queue_init_mqd(adev, queue, qprops);
692 /* add hw queue to mes */
693 queue_input.process_id = gang->process->pasid;
695 queue_input.page_table_base_addr =
696 adev->vm_manager.vram_base_offset + gang->process->pd_gpu_addr -
697 adev->gmc.vram_start;
699 queue_input.process_va_start = 0;
700 queue_input.process_va_end =
701 (adev->vm_manager.max_pfn - 1) << AMDGPU_GPU_PAGE_SHIFT;
702 queue_input.process_quantum = gang->process->process_quantum;
703 queue_input.process_context_addr = gang->process->proc_ctx_gpu_addr;
704 queue_input.gang_quantum = gang->gang_quantum;
705 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
706 queue_input.inprocess_gang_priority = gang->inprocess_gang_priority;
707 queue_input.gang_global_priority_level = gang->global_priority_level;
708 queue_input.doorbell_offset = qprops->doorbell_off;
709 queue_input.mqd_addr = queue->mqd_gpu_addr;
710 queue_input.wptr_addr = qprops->wptr_gpu_addr;
711 queue_input.wptr_mc_addr = qprops->wptr_mc_addr;
712 queue_input.queue_type = qprops->queue_type;
713 queue_input.paging = qprops->paging;
714 queue_input.is_kfd_process = 0;
716 r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
718 DRM_ERROR("failed to add hardware queue to MES, doorbell=0x%llx\n",
719 qprops->doorbell_off);
720 goto clean_up_doorbell;
723 DRM_DEBUG("MES hw queue was added, pasid=%d, gang id=%d, "
724 "queue type=%d, doorbell=0x%llx\n",
725 gang->process->pasid, gang_id, qprops->queue_type,
726 qprops->doorbell_off);
728 queue->ring = qprops->ring;
729 queue->doorbell_off = qprops->doorbell_off;
730 queue->wptr_gpu_addr = qprops->wptr_gpu_addr;
731 queue->queue_type = qprops->queue_type;
732 queue->paging = qprops->paging;
734 queue->ring->mqd_ptr = queue->mqd_cpu_ptr;
735 list_add_tail(&queue->list, &gang->queue_list);
737 amdgpu_mes_unlock(&adev->mes);
741 amdgpu_mes_queue_doorbell_free(adev, gang->process,
742 qprops->doorbell_off);
744 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
745 idr_remove(&adev->mes.queue_id_idr, queue->queue_id);
746 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
748 amdgpu_mes_unlock(&adev->mes);
749 amdgpu_mes_queue_free_mqd(queue);
755 int amdgpu_mes_remove_hw_queue(struct amdgpu_device *adev, int queue_id)
758 struct amdgpu_mes_queue *queue;
759 struct amdgpu_mes_gang *gang;
760 struct mes_remove_queue_input queue_input;
764 * Avoid taking any other locks under MES lock to avoid circular
767 amdgpu_mes_lock(&adev->mes);
769 /* remove the mes gang from idr list */
770 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
772 queue = idr_find(&adev->mes.queue_id_idr, queue_id);
774 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
775 amdgpu_mes_unlock(&adev->mes);
776 DRM_ERROR("queue id %d doesn't exist\n", queue_id);
780 idr_remove(&adev->mes.queue_id_idr, queue_id);
781 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
783 DRM_DEBUG("try to remove queue, doorbell off = 0x%llx\n",
784 queue->doorbell_off);
787 queue_input.doorbell_offset = queue->doorbell_off;
788 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
790 r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
792 DRM_ERROR("failed to remove hardware queue, queue id = %d\n",
795 list_del(&queue->list);
796 amdgpu_mes_queue_doorbell_free(adev, gang->process,
797 queue->doorbell_off);
798 amdgpu_mes_unlock(&adev->mes);
800 amdgpu_mes_queue_free_mqd(queue);
805 int amdgpu_mes_unmap_legacy_queue(struct amdgpu_device *adev,
806 struct amdgpu_ring *ring,
807 enum amdgpu_unmap_queues_action action,
808 u64 gpu_addr, u64 seq)
810 struct mes_unmap_legacy_queue_input queue_input;
813 queue_input.action = action;
814 queue_input.queue_type = ring->funcs->type;
815 queue_input.doorbell_offset = ring->doorbell_index;
816 queue_input.pipe_id = ring->pipe;
817 queue_input.queue_id = ring->queue;
818 queue_input.trail_fence_addr = gpu_addr;
819 queue_input.trail_fence_data = seq;
821 r = adev->mes.funcs->unmap_legacy_queue(&adev->mes, &queue_input);
823 DRM_ERROR("failed to unmap legacy queue\n");
828 uint32_t amdgpu_mes_rreg(struct amdgpu_device *adev, uint32_t reg)
830 struct mes_misc_op_input op_input;
833 op_input.op = MES_MISC_OP_READ_REG;
834 op_input.read_reg.reg_offset = reg;
835 op_input.read_reg.buffer_addr = adev->mes.read_val_gpu_addr;
837 if (!adev->mes.funcs->misc_op) {
838 DRM_ERROR("mes rreg is not supported!\n");
842 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
844 DRM_ERROR("failed to read reg (0x%x)\n", reg);
846 val = *(adev->mes.read_val_ptr);
852 int amdgpu_mes_wreg(struct amdgpu_device *adev,
853 uint32_t reg, uint32_t val)
855 struct mes_misc_op_input op_input;
858 op_input.op = MES_MISC_OP_WRITE_REG;
859 op_input.write_reg.reg_offset = reg;
860 op_input.write_reg.reg_value = val;
862 if (!adev->mes.funcs->misc_op) {
863 DRM_ERROR("mes wreg is not supported!\n");
868 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
870 DRM_ERROR("failed to write reg (0x%x)\n", reg);
876 int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,
877 uint32_t reg0, uint32_t reg1,
878 uint32_t ref, uint32_t mask)
880 struct mes_misc_op_input op_input;
883 op_input.op = MES_MISC_OP_WRM_REG_WR_WAIT;
884 op_input.wrm_reg.reg0 = reg0;
885 op_input.wrm_reg.reg1 = reg1;
886 op_input.wrm_reg.ref = ref;
887 op_input.wrm_reg.mask = mask;
889 if (!adev->mes.funcs->misc_op) {
890 DRM_ERROR("mes reg_write_reg_wait is not supported!\n");
895 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
897 DRM_ERROR("failed to reg_write_reg_wait\n");
903 int amdgpu_mes_reg_wait(struct amdgpu_device *adev, uint32_t reg,
904 uint32_t val, uint32_t mask)
906 struct mes_misc_op_input op_input;
909 op_input.op = MES_MISC_OP_WRM_REG_WAIT;
910 op_input.wrm_reg.reg0 = reg;
911 op_input.wrm_reg.ref = val;
912 op_input.wrm_reg.mask = mask;
914 if (!adev->mes.funcs->misc_op) {
915 DRM_ERROR("mes reg wait is not supported!\n");
920 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
922 DRM_ERROR("failed to reg_write_reg_wait\n");
928 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
929 uint64_t process_context_addr,
930 uint32_t spi_gdbg_per_vmid_cntl,
931 const uint32_t *tcp_watch_cntl,
935 struct mes_misc_op_input op_input = {0};
938 if (!adev->mes.funcs->misc_op) {
939 DRM_ERROR("mes set shader debugger is not supported!\n");
943 op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
944 op_input.set_shader_debugger.process_context_addr = process_context_addr;
945 op_input.set_shader_debugger.flags.u32all = flags;
946 op_input.set_shader_debugger.spi_gdbg_per_vmid_cntl = spi_gdbg_per_vmid_cntl;
947 memcpy(op_input.set_shader_debugger.tcp_watch_cntl, tcp_watch_cntl,
948 sizeof(op_input.set_shader_debugger.tcp_watch_cntl));
950 if (((adev->mes.sched_version & AMDGPU_MES_API_VERSION_MASK) >>
951 AMDGPU_MES_API_VERSION_SHIFT) >= 14)
952 op_input.set_shader_debugger.trap_en = trap_en;
954 amdgpu_mes_lock(&adev->mes);
956 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
958 DRM_ERROR("failed to set_shader_debugger\n");
960 amdgpu_mes_unlock(&adev->mes);
966 amdgpu_mes_ring_to_queue_props(struct amdgpu_device *adev,
967 struct amdgpu_ring *ring,
968 struct amdgpu_mes_queue_properties *props)
970 props->queue_type = ring->funcs->type;
971 props->hqd_base_gpu_addr = ring->gpu_addr;
972 props->rptr_gpu_addr = ring->rptr_gpu_addr;
973 props->wptr_gpu_addr = ring->wptr_gpu_addr;
974 props->wptr_mc_addr =
975 ring->mes_ctx->meta_data_mc_addr + ring->wptr_offs;
976 props->queue_size = ring->ring_size;
977 props->eop_gpu_addr = ring->eop_gpu_addr;
978 props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
979 props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
980 props->paging = false;
984 #define DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(_eng) \
986 if (id_offs < AMDGPU_MES_CTX_MAX_OFFS) \
987 return offsetof(struct amdgpu_mes_ctx_meta_data, \
988 _eng[ring->idx].slots[id_offs]); \
989 else if (id_offs == AMDGPU_MES_CTX_RING_OFFS) \
990 return offsetof(struct amdgpu_mes_ctx_meta_data, \
991 _eng[ring->idx].ring); \
992 else if (id_offs == AMDGPU_MES_CTX_IB_OFFS) \
993 return offsetof(struct amdgpu_mes_ctx_meta_data, \
994 _eng[ring->idx].ib); \
995 else if (id_offs == AMDGPU_MES_CTX_PADDING_OFFS) \
996 return offsetof(struct amdgpu_mes_ctx_meta_data, \
997 _eng[ring->idx].padding); \
1000 int amdgpu_mes_ctx_get_offs(struct amdgpu_ring *ring, unsigned int id_offs)
1002 switch (ring->funcs->type) {
1003 case AMDGPU_RING_TYPE_GFX:
1004 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(gfx);
1006 case AMDGPU_RING_TYPE_COMPUTE:
1007 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(compute);
1009 case AMDGPU_RING_TYPE_SDMA:
1010 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(sdma);
1020 int amdgpu_mes_add_ring(struct amdgpu_device *adev, int gang_id,
1021 int queue_type, int idx,
1022 struct amdgpu_mes_ctx_data *ctx_data,
1023 struct amdgpu_ring **out)
1025 struct amdgpu_ring *ring;
1026 struct amdgpu_mes_gang *gang;
1027 struct amdgpu_mes_queue_properties qprops = {0};
1028 int r, queue_id, pasid;
1031 * Avoid taking any other locks under MES lock to avoid circular
1032 * lock dependencies.
1034 amdgpu_mes_lock(&adev->mes);
1035 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
1037 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
1038 amdgpu_mes_unlock(&adev->mes);
1041 pasid = gang->process->pasid;
1043 ring = kzalloc(sizeof(struct amdgpu_ring), GFP_KERNEL);
1045 amdgpu_mes_unlock(&adev->mes);
1049 ring->ring_obj = NULL;
1050 ring->use_doorbell = true;
1051 ring->is_mes_queue = true;
1052 ring->mes_ctx = ctx_data;
1054 ring->no_scheduler = true;
1056 if (queue_type == AMDGPU_RING_TYPE_COMPUTE) {
1057 int offset = offsetof(struct amdgpu_mes_ctx_meta_data,
1058 compute[ring->idx].mec_hpd);
1059 ring->eop_gpu_addr =
1060 amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
1063 switch (queue_type) {
1064 case AMDGPU_RING_TYPE_GFX:
1065 ring->funcs = adev->gfx.gfx_ring[0].funcs;
1067 case AMDGPU_RING_TYPE_COMPUTE:
1068 ring->funcs = adev->gfx.compute_ring[0].funcs;
1070 case AMDGPU_RING_TYPE_SDMA:
1071 ring->funcs = adev->sdma.instance[0].ring.funcs;
1077 r = amdgpu_ring_init(adev, ring, 1024, NULL, 0,
1078 AMDGPU_RING_PRIO_DEFAULT, NULL);
1080 goto clean_up_memory;
1082 amdgpu_mes_ring_to_queue_props(adev, ring, &qprops);
1084 dma_fence_wait(gang->process->vm->last_update, false);
1085 dma_fence_wait(ctx_data->meta_data_va->last_pt_update, false);
1086 amdgpu_mes_unlock(&adev->mes);
1088 r = amdgpu_mes_add_hw_queue(adev, gang_id, &qprops, &queue_id);
1092 ring->hw_queue_id = queue_id;
1093 ring->doorbell_index = qprops.doorbell_off;
1095 if (queue_type == AMDGPU_RING_TYPE_GFX)
1096 sprintf(ring->name, "gfx_%d.%d.%d", pasid, gang_id, queue_id);
1097 else if (queue_type == AMDGPU_RING_TYPE_COMPUTE)
1098 sprintf(ring->name, "compute_%d.%d.%d", pasid, gang_id,
1100 else if (queue_type == AMDGPU_RING_TYPE_SDMA)
1101 sprintf(ring->name, "sdma_%d.%d.%d", pasid, gang_id,
1110 amdgpu_ring_fini(ring);
1113 amdgpu_mes_unlock(&adev->mes);
1117 void amdgpu_mes_remove_ring(struct amdgpu_device *adev,
1118 struct amdgpu_ring *ring)
1123 amdgpu_mes_remove_hw_queue(adev, ring->hw_queue_id);
1124 amdgpu_ring_fini(ring);
1128 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
1129 enum amdgpu_mes_priority_level prio)
1131 return adev->mes.aggregated_doorbells[prio];
1134 int amdgpu_mes_ctx_alloc_meta_data(struct amdgpu_device *adev,
1135 struct amdgpu_mes_ctx_data *ctx_data)
1139 r = amdgpu_bo_create_kernel(adev,
1140 sizeof(struct amdgpu_mes_ctx_meta_data),
1141 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1142 &ctx_data->meta_data_obj,
1143 &ctx_data->meta_data_mc_addr,
1144 &ctx_data->meta_data_ptr);
1146 dev_warn(adev->dev, "(%d) create CTX bo failed\n", r);
1150 if (!ctx_data->meta_data_obj)
1153 memset(ctx_data->meta_data_ptr, 0,
1154 sizeof(struct amdgpu_mes_ctx_meta_data));
1159 void amdgpu_mes_ctx_free_meta_data(struct amdgpu_mes_ctx_data *ctx_data)
1161 if (ctx_data->meta_data_obj)
1162 amdgpu_bo_free_kernel(&ctx_data->meta_data_obj,
1163 &ctx_data->meta_data_mc_addr,
1164 &ctx_data->meta_data_ptr);
1167 int amdgpu_mes_ctx_map_meta_data(struct amdgpu_device *adev,
1168 struct amdgpu_vm *vm,
1169 struct amdgpu_mes_ctx_data *ctx_data)
1171 struct amdgpu_bo_va *bo_va;
1172 struct amdgpu_sync sync;
1173 struct drm_exec exec;
1176 amdgpu_sync_create(&sync);
1178 drm_exec_init(&exec, 0);
1179 drm_exec_until_all_locked(&exec) {
1180 r = drm_exec_lock_obj(&exec,
1181 &ctx_data->meta_data_obj->tbo.base);
1182 drm_exec_retry_on_contention(&exec);
1184 goto error_fini_exec;
1186 r = amdgpu_vm_lock_pd(vm, &exec, 0);
1187 drm_exec_retry_on_contention(&exec);
1189 goto error_fini_exec;
1192 bo_va = amdgpu_vm_bo_add(adev, vm, ctx_data->meta_data_obj);
1194 DRM_ERROR("failed to create bo_va for meta data BO\n");
1196 goto error_fini_exec;
1199 r = amdgpu_vm_bo_map(adev, bo_va, ctx_data->meta_data_gpu_addr, 0,
1200 sizeof(struct amdgpu_mes_ctx_meta_data),
1201 AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
1202 AMDGPU_PTE_EXECUTABLE);
1205 DRM_ERROR("failed to do bo_map on meta data, err=%d\n", r);
1206 goto error_del_bo_va;
1209 r = amdgpu_vm_bo_update(adev, bo_va, false);
1211 DRM_ERROR("failed to do vm_bo_update on meta data\n");
1212 goto error_del_bo_va;
1214 amdgpu_sync_fence(&sync, bo_va->last_pt_update);
1216 r = amdgpu_vm_update_pdes(adev, vm, false);
1218 DRM_ERROR("failed to update pdes on meta data\n");
1219 goto error_del_bo_va;
1221 amdgpu_sync_fence(&sync, vm->last_update);
1223 amdgpu_sync_wait(&sync, false);
1224 drm_exec_fini(&exec);
1226 amdgpu_sync_free(&sync);
1227 ctx_data->meta_data_va = bo_va;
1231 amdgpu_vm_bo_del(adev, bo_va);
1234 drm_exec_fini(&exec);
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 dma_fence *fence;
1246 struct drm_exec exec;
1249 drm_exec_init(&exec, 0);
1250 drm_exec_until_all_locked(&exec) {
1251 r = drm_exec_lock_obj(&exec,
1252 &ctx_data->meta_data_obj->tbo.base);
1253 drm_exec_retry_on_contention(&exec);
1257 r = amdgpu_vm_lock_pd(vm, &exec, 0);
1258 drm_exec_retry_on_contention(&exec);
1263 amdgpu_vm_bo_del(adev, bo_va);
1264 if (!amdgpu_vm_ready(vm))
1267 r = dma_resv_get_singleton(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
1272 amdgpu_bo_fence(bo, fence, true);
1276 r = amdgpu_vm_clear_freed(adev, vm, &fence);
1280 dma_fence_wait(fence, false);
1281 amdgpu_bo_fence(bo, fence, true);
1282 dma_fence_put(fence);
1285 if (unlikely(r < 0))
1286 dev_err(adev->dev, "failed to clear page tables (%ld)\n", r);
1287 drm_exec_fini(&exec);
1292 static int amdgpu_mes_test_create_gang_and_queues(struct amdgpu_device *adev,
1293 int pasid, int *gang_id,
1294 int queue_type, int num_queue,
1295 struct amdgpu_ring **added_rings,
1296 struct amdgpu_mes_ctx_data *ctx_data)
1298 struct amdgpu_ring *ring;
1299 struct amdgpu_mes_gang_properties gprops = {0};
1302 /* create a gang for the process */
1303 gprops.priority = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1304 gprops.gang_quantum = adev->mes.default_gang_quantum;
1305 gprops.inprocess_gang_priority = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1306 gprops.priority_level = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1307 gprops.global_priority_level = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1309 r = amdgpu_mes_add_gang(adev, pasid, &gprops, gang_id);
1311 DRM_ERROR("failed to add gang\n");
1315 /* create queues for the gang */
1316 for (j = 0; j < num_queue; j++) {
1317 r = amdgpu_mes_add_ring(adev, *gang_id, queue_type, j,
1320 DRM_ERROR("failed to add ring\n");
1324 DRM_INFO("ring %s was added\n", ring->name);
1325 added_rings[j] = ring;
1331 static int amdgpu_mes_test_queues(struct amdgpu_ring **added_rings)
1333 struct amdgpu_ring *ring;
1336 for (i = 0; i < AMDGPU_MES_CTX_MAX_RINGS; i++) {
1337 ring = added_rings[i];
1341 r = amdgpu_ring_test_helper(ring);
1345 r = amdgpu_ring_test_ib(ring, 1000 * 10);
1347 DRM_DEV_ERROR(ring->adev->dev,
1348 "ring %s ib test failed (%d)\n",
1352 DRM_INFO("ring %s ib test pass\n", ring->name);
1358 int amdgpu_mes_self_test(struct amdgpu_device *adev)
1360 struct amdgpu_vm *vm = NULL;
1361 struct amdgpu_mes_ctx_data ctx_data = {0};
1362 struct amdgpu_ring *added_rings[AMDGPU_MES_CTX_MAX_RINGS] = { NULL };
1363 int gang_ids[3] = {0};
1364 int queue_types[][2] = { { AMDGPU_RING_TYPE_GFX, 1 },
1365 { AMDGPU_RING_TYPE_COMPUTE, 1 },
1366 { AMDGPU_RING_TYPE_SDMA, 1} };
1367 int i, r, pasid, k = 0;
1369 pasid = amdgpu_pasid_alloc(16);
1371 dev_warn(adev->dev, "No more PASIDs available!");
1375 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1381 r = amdgpu_vm_init(adev, vm);
1383 DRM_ERROR("failed to initialize vm\n");
1387 r = amdgpu_mes_ctx_alloc_meta_data(adev, &ctx_data);
1389 DRM_ERROR("failed to alloc ctx meta data\n");
1393 ctx_data.meta_data_gpu_addr = AMDGPU_VA_RESERVED_SIZE;
1394 r = amdgpu_mes_ctx_map_meta_data(adev, vm, &ctx_data);
1396 DRM_ERROR("failed to map ctx meta data\n");
1400 r = amdgpu_mes_create_process(adev, pasid, vm);
1402 DRM_ERROR("failed to create MES process\n");
1406 for (i = 0; i < ARRAY_SIZE(queue_types); i++) {
1407 /* On GFX v10.3, fw hasn't supported to map sdma queue. */
1408 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(10, 3, 0) &&
1409 adev->ip_versions[GC_HWIP][0] < IP_VERSION(11, 0, 0) &&
1410 queue_types[i][0] == AMDGPU_RING_TYPE_SDMA)
1413 r = amdgpu_mes_test_create_gang_and_queues(adev, pasid,
1422 k += queue_types[i][1];
1425 /* start ring test and ib test for MES queues */
1426 amdgpu_mes_test_queues(added_rings);
1429 /* remove all queues */
1430 for (i = 0; i < ARRAY_SIZE(added_rings); i++) {
1431 if (!added_rings[i])
1433 amdgpu_mes_remove_ring(adev, added_rings[i]);
1436 for (i = 0; i < ARRAY_SIZE(gang_ids); i++) {
1439 amdgpu_mes_remove_gang(adev, gang_ids[i]);
1442 amdgpu_mes_destroy_process(adev, pasid);
1445 amdgpu_mes_ctx_unmap_meta_data(adev, &ctx_data);
1448 amdgpu_vm_fini(adev, vm);
1452 amdgpu_pasid_free(pasid);
1454 amdgpu_mes_ctx_free_meta_data(&ctx_data);
1459 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe)
1461 const struct mes_firmware_header_v1_0 *mes_hdr;
1462 struct amdgpu_firmware_info *info;
1463 char ucode_prefix[30];
1465 bool need_retry = false;
1468 amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix,
1469 sizeof(ucode_prefix));
1470 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(11, 0, 0)) {
1471 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
1473 pipe == AMDGPU_MES_SCHED_PIPE ? "_2" : "1");
1476 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
1478 pipe == AMDGPU_MES_SCHED_PIPE ? "" : "1");
1481 r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe], fw_name);
1482 if (r && need_retry && pipe == AMDGPU_MES_SCHED_PIPE) {
1483 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes.bin",
1485 DRM_INFO("try to fall back to %s\n", fw_name);
1486 r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe],
1493 mes_hdr = (const struct mes_firmware_header_v1_0 *)
1494 adev->mes.fw[pipe]->data;
1495 adev->mes.uc_start_addr[pipe] =
1496 le32_to_cpu(mes_hdr->mes_uc_start_addr_lo) |
1497 ((uint64_t)(le32_to_cpu(mes_hdr->mes_uc_start_addr_hi)) << 32);
1498 adev->mes.data_start_addr[pipe] =
1499 le32_to_cpu(mes_hdr->mes_data_start_addr_lo) |
1500 ((uint64_t)(le32_to_cpu(mes_hdr->mes_data_start_addr_hi)) << 32);
1502 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1503 int ucode, ucode_data;
1505 if (pipe == AMDGPU_MES_SCHED_PIPE) {
1506 ucode = AMDGPU_UCODE_ID_CP_MES;
1507 ucode_data = AMDGPU_UCODE_ID_CP_MES_DATA;
1509 ucode = AMDGPU_UCODE_ID_CP_MES1;
1510 ucode_data = AMDGPU_UCODE_ID_CP_MES1_DATA;
1513 info = &adev->firmware.ucode[ucode];
1514 info->ucode_id = ucode;
1515 info->fw = adev->mes.fw[pipe];
1516 adev->firmware.fw_size +=
1517 ALIGN(le32_to_cpu(mes_hdr->mes_ucode_size_bytes),
1520 info = &adev->firmware.ucode[ucode_data];
1521 info->ucode_id = ucode_data;
1522 info->fw = adev->mes.fw[pipe];
1523 adev->firmware.fw_size +=
1524 ALIGN(le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes),
1530 amdgpu_ucode_release(&adev->mes.fw[pipe]);