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 static int amdgpu_mes_kernel_doorbell_get(struct amdgpu_device *adev,
43 int ip_type, uint64_t *doorbell_index)
45 unsigned int offset, found;
46 struct amdgpu_mes *mes = &adev->mes;
48 if (ip_type == AMDGPU_RING_TYPE_SDMA)
49 offset = adev->doorbell_index.sdma_engine[0];
53 found = find_next_zero_bit(mes->doorbell_bitmap, mes->num_mes_dbs, offset);
54 if (found >= mes->num_mes_dbs) {
55 DRM_WARN("No doorbell available\n");
59 set_bit(found, mes->doorbell_bitmap);
61 /* Get the absolute doorbell index on BAR */
62 *doorbell_index = mes->db_start_dw_offset + found * 2;
66 static void amdgpu_mes_kernel_doorbell_free(struct amdgpu_device *adev,
67 uint32_t doorbell_index)
69 unsigned int old, rel_index;
70 struct amdgpu_mes *mes = &adev->mes;
72 /* Find the relative index of the doorbell in this object */
73 rel_index = (doorbell_index - mes->db_start_dw_offset) / 2;
74 old = test_and_clear_bit(rel_index, mes->doorbell_bitmap);
78 static int amdgpu_mes_doorbell_init(struct amdgpu_device *adev)
81 struct amdgpu_mes *mes = &adev->mes;
83 /* Bitmap for dynamic allocation of kernel doorbells */
84 mes->doorbell_bitmap = bitmap_zalloc(PAGE_SIZE / sizeof(u32), GFP_KERNEL);
85 if (!mes->doorbell_bitmap) {
86 DRM_ERROR("Failed to allocate MES doorbell bitmap\n");
90 mes->num_mes_dbs = PAGE_SIZE / AMDGPU_ONE_DOORBELL_SIZE;
91 for (i = 0; i < AMDGPU_MES_PRIORITY_NUM_LEVELS; i++) {
92 adev->mes.aggregated_doorbells[i] = mes->db_start_dw_offset + i * 2;
93 set_bit(i, mes->doorbell_bitmap);
99 static int amdgpu_mes_event_log_init(struct amdgpu_device *adev)
103 if (!amdgpu_mes_log_enable)
106 r = amdgpu_bo_create_kernel(adev, adev->mes.event_log_size, PAGE_SIZE,
107 AMDGPU_GEM_DOMAIN_GTT,
108 &adev->mes.event_log_gpu_obj,
109 &adev->mes.event_log_gpu_addr,
110 &adev->mes.event_log_cpu_addr);
112 dev_warn(adev->dev, "failed to create MES event log buffer (%d)", r);
116 memset(adev->mes.event_log_cpu_addr, 0, adev->mes.event_log_size);
122 static void amdgpu_mes_doorbell_free(struct amdgpu_device *adev)
124 bitmap_free(adev->mes.doorbell_bitmap);
127 int amdgpu_mes_init(struct amdgpu_device *adev)
131 adev->mes.adev = adev;
133 idr_init(&adev->mes.pasid_idr);
134 idr_init(&adev->mes.gang_id_idr);
135 idr_init(&adev->mes.queue_id_idr);
136 ida_init(&adev->mes.doorbell_ida);
137 spin_lock_init(&adev->mes.queue_id_lock);
138 mutex_init(&adev->mes.mutex_hidden);
140 for (i = 0; i < AMDGPU_MAX_MES_PIPES; i++)
141 spin_lock_init(&adev->mes.ring_lock[i]);
143 adev->mes.total_max_queue = AMDGPU_FENCE_MES_QUEUE_ID_MASK;
144 adev->mes.vmid_mask_mmhub = 0xffffff00;
145 adev->mes.vmid_mask_gfxhub = 0xffffff00;
147 for (i = 0; i < AMDGPU_MES_MAX_COMPUTE_PIPES; i++) {
148 /* use only 1st MEC pipes */
149 if (i >= adev->gfx.mec.num_pipe_per_mec)
151 adev->mes.compute_hqd_mask[i] = 0xc;
154 for (i = 0; i < AMDGPU_MES_MAX_GFX_PIPES; i++)
155 adev->mes.gfx_hqd_mask[i] = i ? 0 : 0xfffffffe;
157 for (i = 0; i < AMDGPU_MES_MAX_SDMA_PIPES; i++) {
158 if (amdgpu_ip_version(adev, SDMA0_HWIP, 0) <
160 adev->mes.sdma_hqd_mask[i] = i ? 0 : 0x3fc;
161 /* zero sdma_hqd_mask for non-existent engine */
162 else if (adev->sdma.num_instances == 1)
163 adev->mes.sdma_hqd_mask[i] = i ? 0 : 0xfc;
165 adev->mes.sdma_hqd_mask[i] = 0xfc;
168 for (i = 0; i < AMDGPU_MAX_MES_PIPES; i++) {
169 r = amdgpu_device_wb_get(adev, &adev->mes.sch_ctx_offs[i]);
172 "(%d) ring trail_fence_offs wb alloc failed\n",
176 adev->mes.sch_ctx_gpu_addr[i] =
177 adev->wb.gpu_addr + (adev->mes.sch_ctx_offs[i] * 4);
178 adev->mes.sch_ctx_ptr[i] =
179 (uint64_t *)&adev->wb.wb[adev->mes.sch_ctx_offs[i]];
181 r = amdgpu_device_wb_get(adev,
182 &adev->mes.query_status_fence_offs[i]);
185 "(%d) query_status_fence_offs wb alloc failed\n",
189 adev->mes.query_status_fence_gpu_addr[i] = adev->wb.gpu_addr +
190 (adev->mes.query_status_fence_offs[i] * 4);
191 adev->mes.query_status_fence_ptr[i] =
192 (uint64_t *)&adev->wb.wb[adev->mes.query_status_fence_offs[i]];
195 r = amdgpu_device_wb_get(adev, &adev->mes.read_val_offs);
198 "(%d) read_val_offs alloc failed\n", r);
201 adev->mes.read_val_gpu_addr =
202 adev->wb.gpu_addr + (adev->mes.read_val_offs * 4);
203 adev->mes.read_val_ptr =
204 (uint32_t *)&adev->wb.wb[adev->mes.read_val_offs];
206 r = amdgpu_mes_doorbell_init(adev);
210 r = amdgpu_mes_event_log_init(adev);
217 amdgpu_mes_doorbell_free(adev);
219 for (i = 0; i < AMDGPU_MAX_MES_PIPES; i++) {
220 if (adev->mes.sch_ctx_ptr[i])
221 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs[i]);
222 if (adev->mes.query_status_fence_ptr[i])
223 amdgpu_device_wb_free(adev,
224 adev->mes.query_status_fence_offs[i]);
226 if (adev->mes.read_val_ptr)
227 amdgpu_device_wb_free(adev, adev->mes.read_val_offs);
229 idr_destroy(&adev->mes.pasid_idr);
230 idr_destroy(&adev->mes.gang_id_idr);
231 idr_destroy(&adev->mes.queue_id_idr);
232 ida_destroy(&adev->mes.doorbell_ida);
233 mutex_destroy(&adev->mes.mutex_hidden);
237 void amdgpu_mes_fini(struct amdgpu_device *adev)
241 amdgpu_bo_free_kernel(&adev->mes.event_log_gpu_obj,
242 &adev->mes.event_log_gpu_addr,
243 &adev->mes.event_log_cpu_addr);
245 for (i = 0; i < AMDGPU_MAX_MES_PIPES; i++) {
246 if (adev->mes.sch_ctx_ptr[i])
247 amdgpu_device_wb_free(adev, adev->mes.sch_ctx_offs[i]);
248 if (adev->mes.query_status_fence_ptr[i])
249 amdgpu_device_wb_free(adev,
250 adev->mes.query_status_fence_offs[i]);
252 if (adev->mes.read_val_ptr)
253 amdgpu_device_wb_free(adev, adev->mes.read_val_offs);
255 amdgpu_mes_doorbell_free(adev);
257 idr_destroy(&adev->mes.pasid_idr);
258 idr_destroy(&adev->mes.gang_id_idr);
259 idr_destroy(&adev->mes.queue_id_idr);
260 ida_destroy(&adev->mes.doorbell_ida);
261 mutex_destroy(&adev->mes.mutex_hidden);
264 static void amdgpu_mes_queue_free_mqd(struct amdgpu_mes_queue *q)
266 amdgpu_bo_free_kernel(&q->mqd_obj,
271 int amdgpu_mes_create_process(struct amdgpu_device *adev, int pasid,
272 struct amdgpu_vm *vm)
274 struct amdgpu_mes_process *process;
277 /* allocate the mes process buffer */
278 process = kzalloc(sizeof(struct amdgpu_mes_process), GFP_KERNEL);
280 DRM_ERROR("no more memory to create mes process\n");
284 /* allocate the process context bo and map it */
285 r = amdgpu_bo_create_kernel(adev, AMDGPU_MES_PROC_CTX_SIZE, PAGE_SIZE,
286 AMDGPU_GEM_DOMAIN_GTT,
287 &process->proc_ctx_bo,
288 &process->proc_ctx_gpu_addr,
289 &process->proc_ctx_cpu_ptr);
291 DRM_ERROR("failed to allocate process context bo\n");
292 goto clean_up_memory;
294 memset(process->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
297 * Avoid taking any other locks under MES lock to avoid circular
300 amdgpu_mes_lock(&adev->mes);
302 /* add the mes process to idr list */
303 r = idr_alloc(&adev->mes.pasid_idr, process, pasid, pasid + 1,
306 DRM_ERROR("failed to lock pasid=%d\n", pasid);
310 INIT_LIST_HEAD(&process->gang_list);
312 process->pasid = pasid;
313 process->process_quantum = adev->mes.default_process_quantum;
314 process->pd_gpu_addr = amdgpu_bo_gpu_offset(vm->root.bo);
316 amdgpu_mes_unlock(&adev->mes);
320 amdgpu_mes_unlock(&adev->mes);
321 amdgpu_bo_free_kernel(&process->proc_ctx_bo,
322 &process->proc_ctx_gpu_addr,
323 &process->proc_ctx_cpu_ptr);
329 void amdgpu_mes_destroy_process(struct amdgpu_device *adev, int pasid)
331 struct amdgpu_mes_process *process;
332 struct amdgpu_mes_gang *gang, *tmp1;
333 struct amdgpu_mes_queue *queue, *tmp2;
334 struct mes_remove_queue_input queue_input;
339 * Avoid taking any other locks under MES lock to avoid circular
342 amdgpu_mes_lock(&adev->mes);
344 process = idr_find(&adev->mes.pasid_idr, pasid);
346 DRM_WARN("pasid %d doesn't exist\n", pasid);
347 amdgpu_mes_unlock(&adev->mes);
351 /* Remove all queues from hardware */
352 list_for_each_entry_safe(gang, tmp1, &process->gang_list, list) {
353 list_for_each_entry_safe(queue, tmp2, &gang->queue_list, list) {
354 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
355 idr_remove(&adev->mes.queue_id_idr, queue->queue_id);
356 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
358 queue_input.doorbell_offset = queue->doorbell_off;
359 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
361 r = adev->mes.funcs->remove_hw_queue(&adev->mes,
364 DRM_WARN("failed to remove hardware queue\n");
367 idr_remove(&adev->mes.gang_id_idr, gang->gang_id);
370 idr_remove(&adev->mes.pasid_idr, pasid);
371 amdgpu_mes_unlock(&adev->mes);
373 /* free all memory allocated by the process */
374 list_for_each_entry_safe(gang, tmp1, &process->gang_list, list) {
375 /* free all queues in the gang */
376 list_for_each_entry_safe(queue, tmp2, &gang->queue_list, list) {
377 amdgpu_mes_queue_free_mqd(queue);
378 list_del(&queue->list);
381 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
382 &gang->gang_ctx_gpu_addr,
383 &gang->gang_ctx_cpu_ptr);
384 list_del(&gang->list);
388 amdgpu_bo_free_kernel(&process->proc_ctx_bo,
389 &process->proc_ctx_gpu_addr,
390 &process->proc_ctx_cpu_ptr);
394 int amdgpu_mes_add_gang(struct amdgpu_device *adev, int pasid,
395 struct amdgpu_mes_gang_properties *gprops,
398 struct amdgpu_mes_process *process;
399 struct amdgpu_mes_gang *gang;
402 /* allocate the mes gang buffer */
403 gang = kzalloc(sizeof(struct amdgpu_mes_gang), GFP_KERNEL);
408 /* allocate the gang context bo and map it to cpu space */
409 r = amdgpu_bo_create_kernel(adev, AMDGPU_MES_GANG_CTX_SIZE, PAGE_SIZE,
410 AMDGPU_GEM_DOMAIN_GTT,
412 &gang->gang_ctx_gpu_addr,
413 &gang->gang_ctx_cpu_ptr);
415 DRM_ERROR("failed to allocate process context bo\n");
418 memset(gang->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
421 * Avoid taking any other locks under MES lock to avoid circular
424 amdgpu_mes_lock(&adev->mes);
426 process = idr_find(&adev->mes.pasid_idr, pasid);
428 DRM_ERROR("pasid %d doesn't exist\n", pasid);
433 /* add the mes gang to idr list */
434 r = idr_alloc(&adev->mes.gang_id_idr, gang, 1, 0,
437 DRM_ERROR("failed to allocate idr for gang\n");
444 INIT_LIST_HEAD(&gang->queue_list);
445 gang->process = process;
446 gang->priority = gprops->priority;
447 gang->gang_quantum = gprops->gang_quantum ?
448 gprops->gang_quantum : adev->mes.default_gang_quantum;
449 gang->global_priority_level = gprops->global_priority_level;
450 gang->inprocess_gang_priority = gprops->inprocess_gang_priority;
451 list_add_tail(&gang->list, &process->gang_list);
453 amdgpu_mes_unlock(&adev->mes);
457 amdgpu_mes_unlock(&adev->mes);
458 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
459 &gang->gang_ctx_gpu_addr,
460 &gang->gang_ctx_cpu_ptr);
466 int amdgpu_mes_remove_gang(struct amdgpu_device *adev, int gang_id)
468 struct amdgpu_mes_gang *gang;
471 * Avoid taking any other locks under MES lock to avoid circular
474 amdgpu_mes_lock(&adev->mes);
476 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
478 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
479 amdgpu_mes_unlock(&adev->mes);
483 if (!list_empty(&gang->queue_list)) {
484 DRM_ERROR("queue list is not empty\n");
485 amdgpu_mes_unlock(&adev->mes);
489 idr_remove(&adev->mes.gang_id_idr, gang->gang_id);
490 list_del(&gang->list);
491 amdgpu_mes_unlock(&adev->mes);
493 amdgpu_bo_free_kernel(&gang->gang_ctx_bo,
494 &gang->gang_ctx_gpu_addr,
495 &gang->gang_ctx_cpu_ptr);
502 int amdgpu_mes_suspend(struct amdgpu_device *adev)
504 struct mes_suspend_gang_input input;
507 if (!amdgpu_mes_suspend_resume_all_supported(adev))
510 memset(&input, 0x0, sizeof(struct mes_suspend_gang_input));
511 input.suspend_all_gangs = 1;
514 * Avoid taking any other locks under MES lock to avoid circular
517 amdgpu_mes_lock(&adev->mes);
518 r = adev->mes.funcs->suspend_gang(&adev->mes, &input);
519 amdgpu_mes_unlock(&adev->mes);
521 DRM_ERROR("failed to suspend all gangs");
526 int amdgpu_mes_resume(struct amdgpu_device *adev)
528 struct mes_resume_gang_input input;
531 if (!amdgpu_mes_suspend_resume_all_supported(adev))
534 memset(&input, 0x0, sizeof(struct mes_resume_gang_input));
535 input.resume_all_gangs = 1;
538 * Avoid taking any other locks under MES lock to avoid circular
541 amdgpu_mes_lock(&adev->mes);
542 r = adev->mes.funcs->resume_gang(&adev->mes, &input);
543 amdgpu_mes_unlock(&adev->mes);
545 DRM_ERROR("failed to resume all gangs");
550 static int amdgpu_mes_queue_alloc_mqd(struct amdgpu_device *adev,
551 struct amdgpu_mes_queue *q,
552 struct amdgpu_mes_queue_properties *p)
554 struct amdgpu_mqd *mqd_mgr = &adev->mqds[p->queue_type];
555 u32 mqd_size = mqd_mgr->mqd_size;
558 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
559 AMDGPU_GEM_DOMAIN_GTT,
561 &q->mqd_gpu_addr, &q->mqd_cpu_ptr);
563 dev_warn(adev->dev, "failed to create queue mqd bo (%d)", r);
566 memset(q->mqd_cpu_ptr, 0, mqd_size);
568 r = amdgpu_bo_reserve(q->mqd_obj, false);
569 if (unlikely(r != 0))
575 amdgpu_bo_free_kernel(&q->mqd_obj,
581 static void amdgpu_mes_queue_init_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 struct amdgpu_mqd_prop mqd_prop = {0};
588 mqd_prop.mqd_gpu_addr = q->mqd_gpu_addr;
589 mqd_prop.hqd_base_gpu_addr = p->hqd_base_gpu_addr;
590 mqd_prop.rptr_gpu_addr = p->rptr_gpu_addr;
591 mqd_prop.wptr_gpu_addr = p->wptr_gpu_addr;
592 mqd_prop.queue_size = p->queue_size;
593 mqd_prop.use_doorbell = true;
594 mqd_prop.doorbell_index = p->doorbell_off;
595 mqd_prop.eop_gpu_addr = p->eop_gpu_addr;
596 mqd_prop.hqd_pipe_priority = p->hqd_pipe_priority;
597 mqd_prop.hqd_queue_priority = p->hqd_queue_priority;
598 mqd_prop.hqd_active = false;
600 if (p->queue_type == AMDGPU_RING_TYPE_GFX ||
601 p->queue_type == AMDGPU_RING_TYPE_COMPUTE) {
602 mutex_lock(&adev->srbm_mutex);
603 amdgpu_gfx_select_me_pipe_q(adev, p->ring->me, p->ring->pipe, 0, 0, 0);
606 mqd_mgr->init_mqd(adev, q->mqd_cpu_ptr, &mqd_prop);
608 if (p->queue_type == AMDGPU_RING_TYPE_GFX ||
609 p->queue_type == AMDGPU_RING_TYPE_COMPUTE) {
610 amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0, 0);
611 mutex_unlock(&adev->srbm_mutex);
614 amdgpu_bo_unreserve(q->mqd_obj);
617 int amdgpu_mes_add_hw_queue(struct amdgpu_device *adev, int gang_id,
618 struct amdgpu_mes_queue_properties *qprops,
621 struct amdgpu_mes_queue *queue;
622 struct amdgpu_mes_gang *gang;
623 struct mes_add_queue_input queue_input;
627 memset(&queue_input, 0, sizeof(struct mes_add_queue_input));
629 /* allocate the mes queue buffer */
630 queue = kzalloc(sizeof(struct amdgpu_mes_queue), GFP_KERNEL);
632 DRM_ERROR("Failed to allocate memory for queue\n");
636 /* Allocate the queue mqd */
637 r = amdgpu_mes_queue_alloc_mqd(adev, queue, qprops);
639 goto clean_up_memory;
642 * Avoid taking any other locks under MES lock to avoid circular
645 amdgpu_mes_lock(&adev->mes);
647 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
649 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
654 /* add the mes gang to idr list */
655 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
656 r = idr_alloc(&adev->mes.queue_id_idr, queue, 1, 0,
659 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
662 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
663 *queue_id = queue->queue_id = r;
665 /* allocate a doorbell index for the queue */
666 r = amdgpu_mes_kernel_doorbell_get(adev,
668 &qprops->doorbell_off);
670 goto clean_up_queue_id;
672 /* initialize the queue mqd */
673 amdgpu_mes_queue_init_mqd(adev, queue, qprops);
675 /* add hw queue to mes */
676 queue_input.process_id = gang->process->pasid;
678 queue_input.page_table_base_addr =
679 adev->vm_manager.vram_base_offset + gang->process->pd_gpu_addr -
680 adev->gmc.vram_start;
682 queue_input.process_va_start = 0;
683 queue_input.process_va_end =
684 (adev->vm_manager.max_pfn - 1) << AMDGPU_GPU_PAGE_SHIFT;
685 queue_input.process_quantum = gang->process->process_quantum;
686 queue_input.process_context_addr = gang->process->proc_ctx_gpu_addr;
687 queue_input.gang_quantum = gang->gang_quantum;
688 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
689 queue_input.inprocess_gang_priority = gang->inprocess_gang_priority;
690 queue_input.gang_global_priority_level = gang->global_priority_level;
691 queue_input.doorbell_offset = qprops->doorbell_off;
692 queue_input.mqd_addr = queue->mqd_gpu_addr;
693 queue_input.wptr_addr = qprops->wptr_gpu_addr;
694 queue_input.wptr_mc_addr = qprops->wptr_mc_addr;
695 queue_input.queue_type = qprops->queue_type;
696 queue_input.paging = qprops->paging;
697 queue_input.is_kfd_process = 0;
699 r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
701 DRM_ERROR("failed to add hardware queue to MES, doorbell=0x%llx\n",
702 qprops->doorbell_off);
703 goto clean_up_doorbell;
706 DRM_DEBUG("MES hw queue was added, pasid=%d, gang id=%d, "
707 "queue type=%d, doorbell=0x%llx\n",
708 gang->process->pasid, gang_id, qprops->queue_type,
709 qprops->doorbell_off);
711 queue->ring = qprops->ring;
712 queue->doorbell_off = qprops->doorbell_off;
713 queue->wptr_gpu_addr = qprops->wptr_gpu_addr;
714 queue->queue_type = qprops->queue_type;
715 queue->paging = qprops->paging;
717 queue->ring->mqd_ptr = queue->mqd_cpu_ptr;
718 list_add_tail(&queue->list, &gang->queue_list);
720 amdgpu_mes_unlock(&adev->mes);
724 amdgpu_mes_kernel_doorbell_free(adev, qprops->doorbell_off);
726 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
727 idr_remove(&adev->mes.queue_id_idr, queue->queue_id);
728 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
730 amdgpu_mes_unlock(&adev->mes);
731 amdgpu_mes_queue_free_mqd(queue);
737 int amdgpu_mes_remove_hw_queue(struct amdgpu_device *adev, int queue_id)
740 struct amdgpu_mes_queue *queue;
741 struct amdgpu_mes_gang *gang;
742 struct mes_remove_queue_input queue_input;
746 * Avoid taking any other locks under MES lock to avoid circular
749 amdgpu_mes_lock(&adev->mes);
751 /* remove the mes gang from idr list */
752 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
754 queue = idr_find(&adev->mes.queue_id_idr, queue_id);
756 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
757 amdgpu_mes_unlock(&adev->mes);
758 DRM_ERROR("queue id %d doesn't exist\n", queue_id);
762 idr_remove(&adev->mes.queue_id_idr, queue_id);
763 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
765 DRM_DEBUG("try to remove queue, doorbell off = 0x%llx\n",
766 queue->doorbell_off);
769 queue_input.doorbell_offset = queue->doorbell_off;
770 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
772 r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
774 DRM_ERROR("failed to remove hardware queue, queue id = %d\n",
777 list_del(&queue->list);
778 amdgpu_mes_kernel_doorbell_free(adev, queue->doorbell_off);
779 amdgpu_mes_unlock(&adev->mes);
781 amdgpu_mes_queue_free_mqd(queue);
786 int amdgpu_mes_reset_hw_queue(struct amdgpu_device *adev, int queue_id)
789 struct amdgpu_mes_queue *queue;
790 struct amdgpu_mes_gang *gang;
791 struct mes_reset_queue_input queue_input;
795 * Avoid taking any other locks under MES lock to avoid circular
798 amdgpu_mes_lock(&adev->mes);
800 /* remove the mes gang from idr list */
801 spin_lock_irqsave(&adev->mes.queue_id_lock, flags);
803 queue = idr_find(&adev->mes.queue_id_idr, queue_id);
805 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
806 amdgpu_mes_unlock(&adev->mes);
807 DRM_ERROR("queue id %d doesn't exist\n", queue_id);
810 spin_unlock_irqrestore(&adev->mes.queue_id_lock, flags);
812 DRM_DEBUG("try to reset queue, doorbell off = 0x%llx\n",
813 queue->doorbell_off);
816 queue_input.doorbell_offset = queue->doorbell_off;
817 queue_input.gang_context_addr = gang->gang_ctx_gpu_addr;
819 r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
821 DRM_ERROR("failed to reset hardware queue, queue id = %d\n",
824 amdgpu_mes_unlock(&adev->mes);
829 int amdgpu_mes_reset_hw_queue_mmio(struct amdgpu_device *adev, int queue_type,
830 int me_id, int pipe_id, int queue_id, int vmid)
832 struct mes_reset_queue_input queue_input;
835 queue_input.queue_type = queue_type;
836 queue_input.use_mmio = true;
837 queue_input.me_id = me_id;
838 queue_input.pipe_id = pipe_id;
839 queue_input.queue_id = queue_id;
840 queue_input.vmid = vmid;
841 r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
843 DRM_ERROR("failed to reset hardware queue by mmio, queue id = %d\n",
848 int amdgpu_mes_map_legacy_queue(struct amdgpu_device *adev,
849 struct amdgpu_ring *ring)
851 struct mes_map_legacy_queue_input queue_input;
854 memset(&queue_input, 0, sizeof(queue_input));
856 queue_input.queue_type = ring->funcs->type;
857 queue_input.doorbell_offset = ring->doorbell_index;
858 queue_input.pipe_id = ring->pipe;
859 queue_input.queue_id = ring->queue;
860 queue_input.mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
861 queue_input.wptr_addr = ring->wptr_gpu_addr;
863 r = adev->mes.funcs->map_legacy_queue(&adev->mes, &queue_input);
865 DRM_ERROR("failed to map legacy queue\n");
870 int amdgpu_mes_unmap_legacy_queue(struct amdgpu_device *adev,
871 struct amdgpu_ring *ring,
872 enum amdgpu_unmap_queues_action action,
873 u64 gpu_addr, u64 seq)
875 struct mes_unmap_legacy_queue_input queue_input;
878 queue_input.action = action;
879 queue_input.queue_type = ring->funcs->type;
880 queue_input.doorbell_offset = ring->doorbell_index;
881 queue_input.pipe_id = ring->pipe;
882 queue_input.queue_id = ring->queue;
883 queue_input.trail_fence_addr = gpu_addr;
884 queue_input.trail_fence_data = seq;
886 r = adev->mes.funcs->unmap_legacy_queue(&adev->mes, &queue_input);
888 DRM_ERROR("failed to unmap legacy queue\n");
893 int amdgpu_mes_reset_legacy_queue(struct amdgpu_device *adev,
894 struct amdgpu_ring *ring,
898 struct mes_reset_legacy_queue_input queue_input;
901 memset(&queue_input, 0, sizeof(queue_input));
903 queue_input.queue_type = ring->funcs->type;
904 queue_input.doorbell_offset = ring->doorbell_index;
905 queue_input.me_id = ring->me;
906 queue_input.pipe_id = ring->pipe;
907 queue_input.queue_id = ring->queue;
908 queue_input.mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
909 queue_input.wptr_addr = ring->wptr_gpu_addr;
910 queue_input.vmid = vmid;
911 queue_input.use_mmio = use_mmio;
913 r = adev->mes.funcs->reset_legacy_queue(&adev->mes, &queue_input);
915 DRM_ERROR("failed to reset legacy queue\n");
920 uint32_t amdgpu_mes_rreg(struct amdgpu_device *adev, uint32_t reg)
922 struct mes_misc_op_input op_input;
925 op_input.op = MES_MISC_OP_READ_REG;
926 op_input.read_reg.reg_offset = reg;
927 op_input.read_reg.buffer_addr = adev->mes.read_val_gpu_addr;
929 if (!adev->mes.funcs->misc_op) {
930 DRM_ERROR("mes rreg is not supported!\n");
934 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
936 DRM_ERROR("failed to read reg (0x%x)\n", reg);
938 val = *(adev->mes.read_val_ptr);
944 int amdgpu_mes_wreg(struct amdgpu_device *adev,
945 uint32_t reg, uint32_t val)
947 struct mes_misc_op_input op_input;
950 op_input.op = MES_MISC_OP_WRITE_REG;
951 op_input.write_reg.reg_offset = reg;
952 op_input.write_reg.reg_value = val;
954 if (!adev->mes.funcs->misc_op) {
955 DRM_ERROR("mes wreg is not supported!\n");
960 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
962 DRM_ERROR("failed to write reg (0x%x)\n", reg);
968 int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,
969 uint32_t reg0, uint32_t reg1,
970 uint32_t ref, uint32_t mask)
972 struct mes_misc_op_input op_input;
975 op_input.op = MES_MISC_OP_WRM_REG_WR_WAIT;
976 op_input.wrm_reg.reg0 = reg0;
977 op_input.wrm_reg.reg1 = reg1;
978 op_input.wrm_reg.ref = ref;
979 op_input.wrm_reg.mask = mask;
981 if (!adev->mes.funcs->misc_op) {
982 DRM_ERROR("mes reg_write_reg_wait is not supported!\n");
987 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
989 DRM_ERROR("failed to reg_write_reg_wait\n");
995 int amdgpu_mes_reg_wait(struct amdgpu_device *adev, uint32_t reg,
996 uint32_t val, uint32_t mask)
998 struct mes_misc_op_input op_input;
1001 op_input.op = MES_MISC_OP_WRM_REG_WAIT;
1002 op_input.wrm_reg.reg0 = reg;
1003 op_input.wrm_reg.ref = val;
1004 op_input.wrm_reg.mask = mask;
1006 if (!adev->mes.funcs->misc_op) {
1007 DRM_ERROR("mes reg wait is not supported!\n");
1012 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
1014 DRM_ERROR("failed to reg_write_reg_wait\n");
1020 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
1021 uint64_t process_context_addr,
1022 uint32_t spi_gdbg_per_vmid_cntl,
1023 const uint32_t *tcp_watch_cntl,
1027 struct mes_misc_op_input op_input = {0};
1030 if (!adev->mes.funcs->misc_op) {
1031 DRM_ERROR("mes set shader debugger is not supported!\n");
1035 op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
1036 op_input.set_shader_debugger.process_context_addr = process_context_addr;
1037 op_input.set_shader_debugger.flags.u32all = flags;
1039 /* use amdgpu mes_flush_shader_debugger instead */
1040 if (op_input.set_shader_debugger.flags.process_ctx_flush)
1043 op_input.set_shader_debugger.spi_gdbg_per_vmid_cntl = spi_gdbg_per_vmid_cntl;
1044 memcpy(op_input.set_shader_debugger.tcp_watch_cntl, tcp_watch_cntl,
1045 sizeof(op_input.set_shader_debugger.tcp_watch_cntl));
1047 if (((adev->mes.sched_version & AMDGPU_MES_API_VERSION_MASK) >>
1048 AMDGPU_MES_API_VERSION_SHIFT) >= 14)
1049 op_input.set_shader_debugger.trap_en = trap_en;
1051 amdgpu_mes_lock(&adev->mes);
1053 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
1055 DRM_ERROR("failed to set_shader_debugger\n");
1057 amdgpu_mes_unlock(&adev->mes);
1062 int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
1063 uint64_t process_context_addr)
1065 struct mes_misc_op_input op_input = {0};
1068 if (!adev->mes.funcs->misc_op) {
1069 DRM_ERROR("mes flush shader debugger is not supported!\n");
1073 op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
1074 op_input.set_shader_debugger.process_context_addr = process_context_addr;
1075 op_input.set_shader_debugger.flags.process_ctx_flush = true;
1077 amdgpu_mes_lock(&adev->mes);
1079 r = adev->mes.funcs->misc_op(&adev->mes, &op_input);
1081 DRM_ERROR("failed to set_shader_debugger\n");
1083 amdgpu_mes_unlock(&adev->mes);
1089 amdgpu_mes_ring_to_queue_props(struct amdgpu_device *adev,
1090 struct amdgpu_ring *ring,
1091 struct amdgpu_mes_queue_properties *props)
1093 props->queue_type = ring->funcs->type;
1094 props->hqd_base_gpu_addr = ring->gpu_addr;
1095 props->rptr_gpu_addr = ring->rptr_gpu_addr;
1096 props->wptr_gpu_addr = ring->wptr_gpu_addr;
1097 props->wptr_mc_addr =
1098 ring->mes_ctx->meta_data_mc_addr + ring->wptr_offs;
1099 props->queue_size = ring->ring_size;
1100 props->eop_gpu_addr = ring->eop_gpu_addr;
1101 props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
1102 props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
1103 props->paging = false;
1107 #define DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(_eng) \
1109 if (id_offs < AMDGPU_MES_CTX_MAX_OFFS) \
1110 return offsetof(struct amdgpu_mes_ctx_meta_data, \
1111 _eng[ring->idx].slots[id_offs]); \
1112 else if (id_offs == AMDGPU_MES_CTX_RING_OFFS) \
1113 return offsetof(struct amdgpu_mes_ctx_meta_data, \
1114 _eng[ring->idx].ring); \
1115 else if (id_offs == AMDGPU_MES_CTX_IB_OFFS) \
1116 return offsetof(struct amdgpu_mes_ctx_meta_data, \
1117 _eng[ring->idx].ib); \
1118 else if (id_offs == AMDGPU_MES_CTX_PADDING_OFFS) \
1119 return offsetof(struct amdgpu_mes_ctx_meta_data, \
1120 _eng[ring->idx].padding); \
1123 int amdgpu_mes_ctx_get_offs(struct amdgpu_ring *ring, unsigned int id_offs)
1125 switch (ring->funcs->type) {
1126 case AMDGPU_RING_TYPE_GFX:
1127 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(gfx);
1129 case AMDGPU_RING_TYPE_COMPUTE:
1130 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(compute);
1132 case AMDGPU_RING_TYPE_SDMA:
1133 DEFINE_AMDGPU_MES_CTX_GET_OFFS_ENG(sdma);
1143 int amdgpu_mes_add_ring(struct amdgpu_device *adev, int gang_id,
1144 int queue_type, int idx,
1145 struct amdgpu_mes_ctx_data *ctx_data,
1146 struct amdgpu_ring **out)
1148 struct amdgpu_ring *ring;
1149 struct amdgpu_mes_gang *gang;
1150 struct amdgpu_mes_queue_properties qprops = {0};
1151 int r, queue_id, pasid;
1154 * Avoid taking any other locks under MES lock to avoid circular
1155 * lock dependencies.
1157 amdgpu_mes_lock(&adev->mes);
1158 gang = idr_find(&adev->mes.gang_id_idr, gang_id);
1160 DRM_ERROR("gang id %d doesn't exist\n", gang_id);
1161 amdgpu_mes_unlock(&adev->mes);
1164 pasid = gang->process->pasid;
1166 ring = kzalloc(sizeof(struct amdgpu_ring), GFP_KERNEL);
1168 amdgpu_mes_unlock(&adev->mes);
1172 ring->ring_obj = NULL;
1173 ring->use_doorbell = true;
1174 ring->is_mes_queue = true;
1175 ring->mes_ctx = ctx_data;
1177 ring->no_scheduler = true;
1179 if (queue_type == AMDGPU_RING_TYPE_COMPUTE) {
1180 int offset = offsetof(struct amdgpu_mes_ctx_meta_data,
1181 compute[ring->idx].mec_hpd);
1182 ring->eop_gpu_addr =
1183 amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
1186 switch (queue_type) {
1187 case AMDGPU_RING_TYPE_GFX:
1188 ring->funcs = adev->gfx.gfx_ring[0].funcs;
1189 ring->me = adev->gfx.gfx_ring[0].me;
1190 ring->pipe = adev->gfx.gfx_ring[0].pipe;
1192 case AMDGPU_RING_TYPE_COMPUTE:
1193 ring->funcs = adev->gfx.compute_ring[0].funcs;
1194 ring->me = adev->gfx.compute_ring[0].me;
1195 ring->pipe = adev->gfx.compute_ring[0].pipe;
1197 case AMDGPU_RING_TYPE_SDMA:
1198 ring->funcs = adev->sdma.instance[0].ring.funcs;
1204 r = amdgpu_ring_init(adev, ring, 1024, NULL, 0,
1205 AMDGPU_RING_PRIO_DEFAULT, NULL);
1207 goto clean_up_memory;
1209 amdgpu_mes_ring_to_queue_props(adev, ring, &qprops);
1211 dma_fence_wait(gang->process->vm->last_update, false);
1212 dma_fence_wait(ctx_data->meta_data_va->last_pt_update, false);
1213 amdgpu_mes_unlock(&adev->mes);
1215 r = amdgpu_mes_add_hw_queue(adev, gang_id, &qprops, &queue_id);
1219 ring->hw_queue_id = queue_id;
1220 ring->doorbell_index = qprops.doorbell_off;
1222 if (queue_type == AMDGPU_RING_TYPE_GFX)
1223 sprintf(ring->name, "gfx_%d.%d.%d", pasid, gang_id, queue_id);
1224 else if (queue_type == AMDGPU_RING_TYPE_COMPUTE)
1225 sprintf(ring->name, "compute_%d.%d.%d", pasid, gang_id,
1227 else if (queue_type == AMDGPU_RING_TYPE_SDMA)
1228 sprintf(ring->name, "sdma_%d.%d.%d", pasid, gang_id,
1237 amdgpu_ring_fini(ring);
1240 amdgpu_mes_unlock(&adev->mes);
1244 void amdgpu_mes_remove_ring(struct amdgpu_device *adev,
1245 struct amdgpu_ring *ring)
1250 amdgpu_mes_remove_hw_queue(adev, ring->hw_queue_id);
1251 del_timer_sync(&ring->fence_drv.fallback_timer);
1252 amdgpu_ring_fini(ring);
1256 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
1257 enum amdgpu_mes_priority_level prio)
1259 return adev->mes.aggregated_doorbells[prio];
1262 int amdgpu_mes_ctx_alloc_meta_data(struct amdgpu_device *adev,
1263 struct amdgpu_mes_ctx_data *ctx_data)
1267 r = amdgpu_bo_create_kernel(adev,
1268 sizeof(struct amdgpu_mes_ctx_meta_data),
1269 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1270 &ctx_data->meta_data_obj,
1271 &ctx_data->meta_data_mc_addr,
1272 &ctx_data->meta_data_ptr);
1274 dev_warn(adev->dev, "(%d) create CTX bo failed\n", r);
1278 if (!ctx_data->meta_data_obj)
1281 memset(ctx_data->meta_data_ptr, 0,
1282 sizeof(struct amdgpu_mes_ctx_meta_data));
1287 void amdgpu_mes_ctx_free_meta_data(struct amdgpu_mes_ctx_data *ctx_data)
1289 if (ctx_data->meta_data_obj)
1290 amdgpu_bo_free_kernel(&ctx_data->meta_data_obj,
1291 &ctx_data->meta_data_mc_addr,
1292 &ctx_data->meta_data_ptr);
1295 int amdgpu_mes_ctx_map_meta_data(struct amdgpu_device *adev,
1296 struct amdgpu_vm *vm,
1297 struct amdgpu_mes_ctx_data *ctx_data)
1299 struct amdgpu_bo_va *bo_va;
1300 struct amdgpu_sync sync;
1301 struct drm_exec exec;
1304 amdgpu_sync_create(&sync);
1306 drm_exec_init(&exec, 0, 0);
1307 drm_exec_until_all_locked(&exec) {
1308 r = drm_exec_lock_obj(&exec,
1309 &ctx_data->meta_data_obj->tbo.base);
1310 drm_exec_retry_on_contention(&exec);
1312 goto error_fini_exec;
1314 r = amdgpu_vm_lock_pd(vm, &exec, 0);
1315 drm_exec_retry_on_contention(&exec);
1317 goto error_fini_exec;
1320 bo_va = amdgpu_vm_bo_add(adev, vm, ctx_data->meta_data_obj);
1322 DRM_ERROR("failed to create bo_va for meta data BO\n");
1324 goto error_fini_exec;
1327 r = amdgpu_vm_bo_map(adev, bo_va, ctx_data->meta_data_gpu_addr, 0,
1328 sizeof(struct amdgpu_mes_ctx_meta_data),
1329 AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
1330 AMDGPU_PTE_EXECUTABLE);
1333 DRM_ERROR("failed to do bo_map on meta data, err=%d\n", r);
1334 goto error_del_bo_va;
1337 r = amdgpu_vm_bo_update(adev, bo_va, false);
1339 DRM_ERROR("failed to do vm_bo_update on meta data\n");
1340 goto error_del_bo_va;
1342 amdgpu_sync_fence(&sync, bo_va->last_pt_update);
1344 r = amdgpu_vm_update_pdes(adev, vm, false);
1346 DRM_ERROR("failed to update pdes on meta data\n");
1347 goto error_del_bo_va;
1349 amdgpu_sync_fence(&sync, vm->last_update);
1351 amdgpu_sync_wait(&sync, false);
1352 drm_exec_fini(&exec);
1354 amdgpu_sync_free(&sync);
1355 ctx_data->meta_data_va = bo_va;
1359 amdgpu_vm_bo_del(adev, bo_va);
1362 drm_exec_fini(&exec);
1363 amdgpu_sync_free(&sync);
1367 int amdgpu_mes_ctx_unmap_meta_data(struct amdgpu_device *adev,
1368 struct amdgpu_mes_ctx_data *ctx_data)
1370 struct amdgpu_bo_va *bo_va = ctx_data->meta_data_va;
1371 struct amdgpu_bo *bo = ctx_data->meta_data_obj;
1372 struct amdgpu_vm *vm = bo_va->base.vm;
1373 struct dma_fence *fence;
1374 struct drm_exec exec;
1377 drm_exec_init(&exec, 0, 0);
1378 drm_exec_until_all_locked(&exec) {
1379 r = drm_exec_lock_obj(&exec,
1380 &ctx_data->meta_data_obj->tbo.base);
1381 drm_exec_retry_on_contention(&exec);
1385 r = amdgpu_vm_lock_pd(vm, &exec, 0);
1386 drm_exec_retry_on_contention(&exec);
1391 amdgpu_vm_bo_del(adev, bo_va);
1392 if (!amdgpu_vm_ready(vm))
1395 r = dma_resv_get_singleton(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
1400 amdgpu_bo_fence(bo, fence, true);
1404 r = amdgpu_vm_clear_freed(adev, vm, &fence);
1408 dma_fence_wait(fence, false);
1409 amdgpu_bo_fence(bo, fence, true);
1410 dma_fence_put(fence);
1413 if (unlikely(r < 0))
1414 dev_err(adev->dev, "failed to clear page tables (%ld)\n", r);
1415 drm_exec_fini(&exec);
1420 static int amdgpu_mes_test_create_gang_and_queues(struct amdgpu_device *adev,
1421 int pasid, int *gang_id,
1422 int queue_type, int num_queue,
1423 struct amdgpu_ring **added_rings,
1424 struct amdgpu_mes_ctx_data *ctx_data)
1426 struct amdgpu_ring *ring;
1427 struct amdgpu_mes_gang_properties gprops = {0};
1430 /* create a gang for the process */
1431 gprops.priority = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1432 gprops.gang_quantum = adev->mes.default_gang_quantum;
1433 gprops.inprocess_gang_priority = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1434 gprops.priority_level = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1435 gprops.global_priority_level = AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
1437 r = amdgpu_mes_add_gang(adev, pasid, &gprops, gang_id);
1439 DRM_ERROR("failed to add gang\n");
1443 /* create queues for the gang */
1444 for (j = 0; j < num_queue; j++) {
1445 r = amdgpu_mes_add_ring(adev, *gang_id, queue_type, j,
1448 DRM_ERROR("failed to add ring\n");
1452 DRM_INFO("ring %s was added\n", ring->name);
1453 added_rings[j] = ring;
1459 static int amdgpu_mes_test_queues(struct amdgpu_ring **added_rings)
1461 struct amdgpu_ring *ring;
1464 for (i = 0; i < AMDGPU_MES_CTX_MAX_RINGS; i++) {
1465 ring = added_rings[i];
1469 r = amdgpu_ring_test_helper(ring);
1473 r = amdgpu_ring_test_ib(ring, 1000 * 10);
1475 DRM_DEV_ERROR(ring->adev->dev,
1476 "ring %s ib test failed (%d)\n",
1480 DRM_INFO("ring %s ib test pass\n", ring->name);
1486 int amdgpu_mes_self_test(struct amdgpu_device *adev)
1488 struct amdgpu_vm *vm = NULL;
1489 struct amdgpu_mes_ctx_data ctx_data = {0};
1490 struct amdgpu_ring *added_rings[AMDGPU_MES_CTX_MAX_RINGS] = { NULL };
1491 int gang_ids[3] = {0};
1492 int queue_types[][2] = { { AMDGPU_RING_TYPE_GFX, 1 },
1493 { AMDGPU_RING_TYPE_COMPUTE, 1 },
1494 { AMDGPU_RING_TYPE_SDMA, 1} };
1495 int i, r, pasid, k = 0;
1497 pasid = amdgpu_pasid_alloc(16);
1499 dev_warn(adev->dev, "No more PASIDs available!");
1503 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1509 r = amdgpu_vm_init(adev, vm, -1);
1511 DRM_ERROR("failed to initialize vm\n");
1515 r = amdgpu_mes_ctx_alloc_meta_data(adev, &ctx_data);
1517 DRM_ERROR("failed to alloc ctx meta data\n");
1521 ctx_data.meta_data_gpu_addr = AMDGPU_VA_RESERVED_BOTTOM;
1522 r = amdgpu_mes_ctx_map_meta_data(adev, vm, &ctx_data);
1524 DRM_ERROR("failed to map ctx meta data\n");
1528 r = amdgpu_mes_create_process(adev, pasid, vm);
1530 DRM_ERROR("failed to create MES process\n");
1534 for (i = 0; i < ARRAY_SIZE(queue_types); i++) {
1535 /* On GFX v10.3, fw hasn't supported to map sdma queue. */
1536 if (amdgpu_ip_version(adev, GC_HWIP, 0) >=
1537 IP_VERSION(10, 3, 0) &&
1538 amdgpu_ip_version(adev, GC_HWIP, 0) <
1539 IP_VERSION(11, 0, 0) &&
1540 queue_types[i][0] == AMDGPU_RING_TYPE_SDMA)
1543 r = amdgpu_mes_test_create_gang_and_queues(adev, pasid,
1552 k += queue_types[i][1];
1555 /* start ring test and ib test for MES queues */
1556 amdgpu_mes_test_queues(added_rings);
1559 /* remove all queues */
1560 for (i = 0; i < ARRAY_SIZE(added_rings); i++) {
1561 if (!added_rings[i])
1563 amdgpu_mes_remove_ring(adev, added_rings[i]);
1566 for (i = 0; i < ARRAY_SIZE(gang_ids); i++) {
1569 amdgpu_mes_remove_gang(adev, gang_ids[i]);
1572 amdgpu_mes_destroy_process(adev, pasid);
1575 amdgpu_mes_ctx_unmap_meta_data(adev, &ctx_data);
1578 amdgpu_vm_fini(adev, vm);
1582 amdgpu_pasid_free(pasid);
1584 amdgpu_mes_ctx_free_meta_data(&ctx_data);
1589 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe)
1591 const struct mes_firmware_header_v1_0 *mes_hdr;
1592 struct amdgpu_firmware_info *info;
1593 char ucode_prefix[30];
1595 bool need_retry = false;
1598 amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix,
1599 sizeof(ucode_prefix));
1600 if (adev->enable_uni_mes) {
1601 snprintf(fw_name, sizeof(fw_name),
1602 "amdgpu/%s_uni_mes.bin", ucode_prefix);
1603 } else if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0) &&
1604 amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(12, 0, 0)) {
1605 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
1607 pipe == AMDGPU_MES_SCHED_PIPE ? "_2" : "1");
1610 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mes%s.bin",
1612 pipe == AMDGPU_MES_SCHED_PIPE ? "" : "1");
1615 r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe], "%s", fw_name);
1616 if (r && need_retry && pipe == AMDGPU_MES_SCHED_PIPE) {
1617 dev_info(adev->dev, "try to fall back to %s_mes.bin\n", ucode_prefix);
1618 r = amdgpu_ucode_request(adev, &adev->mes.fw[pipe],
1619 "amdgpu/%s_mes.bin", ucode_prefix);
1625 mes_hdr = (const struct mes_firmware_header_v1_0 *)
1626 adev->mes.fw[pipe]->data;
1627 adev->mes.uc_start_addr[pipe] =
1628 le32_to_cpu(mes_hdr->mes_uc_start_addr_lo) |
1629 ((uint64_t)(le32_to_cpu(mes_hdr->mes_uc_start_addr_hi)) << 32);
1630 adev->mes.data_start_addr[pipe] =
1631 le32_to_cpu(mes_hdr->mes_data_start_addr_lo) |
1632 ((uint64_t)(le32_to_cpu(mes_hdr->mes_data_start_addr_hi)) << 32);
1634 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1635 int ucode, ucode_data;
1637 if (pipe == AMDGPU_MES_SCHED_PIPE) {
1638 ucode = AMDGPU_UCODE_ID_CP_MES;
1639 ucode_data = AMDGPU_UCODE_ID_CP_MES_DATA;
1641 ucode = AMDGPU_UCODE_ID_CP_MES1;
1642 ucode_data = AMDGPU_UCODE_ID_CP_MES1_DATA;
1645 info = &adev->firmware.ucode[ucode];
1646 info->ucode_id = ucode;
1647 info->fw = adev->mes.fw[pipe];
1648 adev->firmware.fw_size +=
1649 ALIGN(le32_to_cpu(mes_hdr->mes_ucode_size_bytes),
1652 info = &adev->firmware.ucode[ucode_data];
1653 info->ucode_id = ucode_data;
1654 info->fw = adev->mes.fw[pipe];
1655 adev->firmware.fw_size +=
1656 ALIGN(le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes),
1662 amdgpu_ucode_release(&adev->mes.fw[pipe]);
1666 bool amdgpu_mes_suspend_resume_all_supported(struct amdgpu_device *adev)
1668 uint32_t mes_rev = adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;
1669 bool is_supported = false;
1671 if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0) &&
1672 amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(12, 0, 0) &&
1674 is_supported = true;
1676 return is_supported;
1679 #if defined(CONFIG_DEBUG_FS)
1681 static int amdgpu_debugfs_mes_event_log_show(struct seq_file *m, void *unused)
1683 struct amdgpu_device *adev = m->private;
1684 uint32_t *mem = (uint32_t *)(adev->mes.event_log_cpu_addr);
1686 seq_hex_dump(m, "", DUMP_PREFIX_OFFSET, 32, 4,
1687 mem, adev->mes.event_log_size, false);
1692 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_mes_event_log);
1696 void amdgpu_debugfs_mes_event_log_init(struct amdgpu_device *adev)
1699 #if defined(CONFIG_DEBUG_FS)
1700 struct drm_minor *minor = adev_to_drm(adev)->primary;
1701 struct dentry *root = minor->debugfs_root;
1702 if (adev->enable_mes && amdgpu_mes_log_enable)
1703 debugfs_create_file("amdgpu_mes_event_log", 0444, root,
1704 adev, &amdgpu_debugfs_mes_event_log_fops);