Skip to content

Commit

Permalink
drm/amdgpu: stop unmapping MQD for kernel queues v3
Browse files Browse the repository at this point in the history
This looks unnecessary and actually extremely harmful since using kmap()
is not possible while inside the ring reset.

Remove all the extra mapping and unmapping of the MQDs.

v2: also fix debugfs
v3: fix coding style typo

Signed-off-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
  • Loading branch information
Christian König authored and Alex Deucher committed Mar 26, 2025
1 parent 510a16d commit 1f86f41
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 432 deletions.
58 changes: 8 additions & 50 deletions drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,59 +608,17 @@ static ssize_t amdgpu_debugfs_mqd_read(struct file *f, char __user *buf,
size_t size, loff_t *pos)
{
struct amdgpu_ring *ring = file_inode(f)->i_private;
volatile u32 *mqd;
u32 *kbuf;
int r, i;
uint32_t value, result;
ssize_t bytes = min_t(ssize_t, ring->mqd_size - *pos, size);
void *from = ((u8 *)ring->mqd_ptr) + *pos;

if (*pos & 3 || size & 3)
return -EINVAL;

kbuf = kmalloc(ring->mqd_size, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0))
goto err_free;

r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&mqd);
if (r)
goto err_unreserve;

/*
* Copy to local buffer to avoid put_user(), which might fault
* and acquire mmap_sem, under reservation_ww_class_mutex.
*/
for (i = 0; i < ring->mqd_size/sizeof(u32); i++)
kbuf[i] = mqd[i];
if (*pos > ring->mqd_size)
return 0;

amdgpu_bo_kunmap(ring->mqd_obj);
amdgpu_bo_unreserve(ring->mqd_obj);
if (copy_to_user(buf, from, bytes))
return -EFAULT;

result = 0;
while (size) {
if (*pos >= ring->mqd_size)
break;

value = kbuf[*pos/4];
r = put_user(value, (uint32_t *)buf);
if (r)
goto err_free;
buf += 4;
result += 4;
size -= 4;
*pos += 4;
}

kfree(kbuf);
return result;

err_unreserve:
amdgpu_bo_unreserve(ring->mqd_obj);
err_free:
kfree(kbuf);
return r;
*pos += bytes;
return bytes;
}

static const struct file_operations amdgpu_debugfs_mqd_fops = {
Expand Down
88 changes: 11 additions & 77 deletions drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
Original file line number Diff line number Diff line change
Expand Up @@ -6851,22 +6851,9 @@ static int gfx_v10_0_kgq_init_queue(struct amdgpu_ring *ring, bool reset)
static int gfx_v10_0_cp_async_gfx_ring_resume(struct amdgpu_device *adev)
{
int r, i;
struct amdgpu_ring *ring;

for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
ring = &adev->gfx.gfx_ring[i];

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0))
return r;

r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v10_0_kgq_init_queue(ring, false);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v10_0_kgq_init_queue(&adev->gfx.gfx_ring[i], false);
if (r)
return r;
}
Expand Down Expand Up @@ -7173,55 +7160,24 @@ static int gfx_v10_0_kcq_init_queue(struct amdgpu_ring *ring, bool restore)

static int gfx_v10_0_kiq_resume(struct amdgpu_device *adev)
{
struct amdgpu_ring *ring;
int r;

ring = &adev->gfx.kiq[0].ring;

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0))
return r;

r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (unlikely(r != 0)) {
amdgpu_bo_unreserve(ring->mqd_obj);
return r;
}

gfx_v10_0_kiq_init_queue(ring);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
amdgpu_bo_unreserve(ring->mqd_obj);
gfx_v10_0_kiq_init_queue(&adev->gfx.kiq[0].ring);
return 0;
}

static int gfx_v10_0_kcq_resume(struct amdgpu_device *adev)
{
struct amdgpu_ring *ring = NULL;
int r = 0, i;
int i, r;

gfx_v10_0_cp_compute_enable(adev, true);

for (i = 0; i < adev->gfx.num_compute_rings; i++) {
ring = &adev->gfx.compute_ring[i];

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0))
goto done;
r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v10_0_kcq_init_queue(ring, false);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v10_0_kcq_init_queue(&adev->gfx.compute_ring[i],
false);
if (r)
goto done;
return r;
}

r = amdgpu_gfx_enable_kcq(adev, 0);
done:
return r;
return amdgpu_gfx_enable_kcq(adev, 0);
}

static int gfx_v10_0_cp_resume(struct amdgpu_device *adev)
Expand Down Expand Up @@ -9579,20 +9535,9 @@ static int gfx_v10_0_reset_kgq(struct amdgpu_ring *ring, unsigned int vmid)
if (r)
return r;

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0)) {
DRM_ERROR("fail to resv mqd_obj\n");
return r;
}
r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v10_0_kgq_init_queue(ring, true);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v10_0_kgq_init_queue(ring, true);
if (r) {
DRM_ERROR("fail to unresv mqd_obj\n");
DRM_ERROR("fail to init kgq\n");
return r;
}

Expand Down Expand Up @@ -9649,20 +9594,9 @@ static int gfx_v10_0_reset_kcq(struct amdgpu_ring *ring,
return r;
}

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0)) {
dev_err(adev->dev, "fail to resv mqd_obj\n");
return r;
}
r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v10_0_kcq_init_queue(ring, true);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v10_0_kcq_init_queue(ring, true);
if (r) {
dev_err(adev->dev, "fail to unresv mqd_obj\n");
dev_err(adev->dev, "fail to init kcq\n");
return r;
}

Expand Down
88 changes: 10 additions & 78 deletions drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
Original file line number Diff line number Diff line change
Expand Up @@ -4115,22 +4115,9 @@ static int gfx_v11_0_kgq_init_queue(struct amdgpu_ring *ring, bool reset)
static int gfx_v11_0_cp_async_gfx_ring_resume(struct amdgpu_device *adev)
{
int r, i;
struct amdgpu_ring *ring;

for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
ring = &adev->gfx.gfx_ring[i];

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0))
return r;

r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v11_0_kgq_init_queue(ring, false);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v11_0_kgq_init_queue(&adev->gfx.gfx_ring[i], false);
if (r)
return r;
}
Expand Down Expand Up @@ -4452,57 +4439,24 @@ static int gfx_v11_0_kcq_init_queue(struct amdgpu_ring *ring, bool reset)

static int gfx_v11_0_kiq_resume(struct amdgpu_device *adev)
{
struct amdgpu_ring *ring;
int r;

ring = &adev->gfx.kiq[0].ring;

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0))
return r;

r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (unlikely(r != 0)) {
amdgpu_bo_unreserve(ring->mqd_obj);
return r;
}

gfx_v11_0_kiq_init_queue(ring);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
amdgpu_bo_unreserve(ring->mqd_obj);
ring->sched.ready = true;
gfx_v11_0_kiq_init_queue(&adev->gfx.kiq[0].ring);
return 0;
}

static int gfx_v11_0_kcq_resume(struct amdgpu_device *adev)
{
struct amdgpu_ring *ring = NULL;
int r = 0, i;
int i, r;

if (!amdgpu_async_gfx_ring)
gfx_v11_0_cp_compute_enable(adev, true);

for (i = 0; i < adev->gfx.num_compute_rings; i++) {
ring = &adev->gfx.compute_ring[i];

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0))
goto done;
r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v11_0_kcq_init_queue(ring, false);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v11_0_kcq_init_queue(&adev->gfx.compute_ring[i], false);
if (r)
goto done;
return r;
}

r = amdgpu_gfx_enable_kcq(adev, 0);
done:
return r;
return amdgpu_gfx_enable_kcq(adev, 0);
}

static int gfx_v11_0_cp_resume(struct amdgpu_device *adev)
Expand Down Expand Up @@ -6667,20 +6621,9 @@ static int gfx_v11_0_reset_kgq(struct amdgpu_ring *ring, unsigned int vmid)
if (r)
return r;

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0)) {
dev_err(adev->dev, "fail to resv mqd_obj\n");
return r;
}
r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v11_0_kgq_init_queue(ring, true);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v11_0_kgq_init_queue(ring, true);
if (r) {
dev_err(adev->dev, "fail to unresv mqd_obj\n");
dev_err(adev->dev, "failed to init kgq\n");
return r;
}

Expand All @@ -6707,20 +6650,9 @@ static int gfx_v11_0_reset_kcq(struct amdgpu_ring *ring, unsigned int vmid)
return r;
}

r = amdgpu_bo_reserve(ring->mqd_obj, false);
if (unlikely(r != 0)) {
dev_err(adev->dev, "fail to resv mqd_obj\n");
return r;
}
r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
if (!r) {
r = gfx_v11_0_kcq_init_queue(ring, true);
amdgpu_bo_kunmap(ring->mqd_obj);
ring->mqd_ptr = NULL;
}
amdgpu_bo_unreserve(ring->mqd_obj);
r = gfx_v11_0_kcq_init_queue(ring, true);
if (r) {
dev_err(adev->dev, "fail to unresv mqd_obj\n");
dev_err(adev->dev, "fail to init kcq\n");
return r;
}
r = amdgpu_mes_map_legacy_queue(adev, ring);
Expand Down
Loading

0 comments on commit 1f86f41

Please sign in to comment.