Skip to contentSkip to conversation

drm/amdgpu: optimize amdgpu_device_vram_access a bit.

PATCH In review Christian König 4 participants 8 messages Feb 5, 2020 – Feb 6, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 drm/amdgpu: optimize amdgpu_device_vram_access a bit.

Only write the _HI register when necessary.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +11 -6
index 5030a09babb8..d39630edda01 100644
@@ -184,20 +184,25 @@ bool amdgpu_device_supports_baco(struct drm_device *dev)
 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
 			       uint32_t *buf, size_t size, bool write)
 {
-	uint64_t last;
 	unsigned long flags;
+	uint32_t hi = ~0;
+	uint64_t last;
+
+	spin_lock_irqsave(&adev->mmio_idx_lock, flags);
+	for (last = pos + size; pos < last; pos += 4) {
+		uint32_t tmp = pos >> 31;
 
-	last = size - 4;
-	for (last += pos; pos <= last; pos += 4) {
-		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
 		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
-		WREG32_NO_KIQ(mmMM_INDEX_HI, pos >> 31);
+		if (tmp != hi) {
+			WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
+			hi = tmp;
+		}
 		if (write)
 			WREG32_NO_KIQ(mmMM_DATA, *buf++);
 		else
 			*buf++ = RREG32_NO_KIQ(mmMM_DATA);
-		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
 	}
+	spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
 }
 
 /*
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#1 lore raw

PATCH 2/4 drm/amdgpu: use the BAR if possible in amdgpu_device_vram_access

This should speed up debugging VRAM access a lot.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +21 -0
index d39630edda01..7d65c9aedecd 100644
@@ -188,6 +188,27 @@ void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
 	uint32_t hi = ~0;
 	uint64_t last;
 
+
+#ifdef CONFIG_64BIT
+	last = min(pos + size, adev->gmc.visible_vram_size);
+	if (last > pos) {
+		void __iomem *addr = adev->mman.aper_base_kaddr + pos;
+		size_t count = last - pos;
+
+		if (write)
+			memcpy_toio(addr, buf, count);
+		else
+			memcpy_fromio(buf, addr, count);
+
+		if (count == size)
+			return;
+
+		pos += count;
+		buf += count / 4;
+		size -= count;
+	}
+#endif
+
 	spin_lock_irqsave(&adev->mmio_idx_lock, flags);
 	for (last = pos + size; pos < last; pos += 4) {
 		uint32_t tmp = pos >> 31;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#2 lore raw

Re: [PATCH 2/4] drm/amdgpu: use the BAR if possible in amdgpu_device_vram_access

If we're using the BAR, we should probably flush HDP cache/buffers before reading or after writing.

Regards,
  Felix

Quoted 29 lines · Christian König

This should speed up debugging VRAM access a lot.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +14 -0
index d39630edda01..7d65c9aedecd 100644
@@ -188,6 +188,27 @@ void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
  	uint32_t hi = ~0;
  	uint64_t last;
 
+
+#ifdef CONFIG_64BIT
+	last = min(pos + size, adev->gmc.visible_vram_size);
+	if (last > pos) {
+		void __iomem *addr = adev->mman.aper_base_kaddr + pos;
+		size_t count = last - pos;
+
+		if (write)
+			memcpy_toio(addr, buf, count);
+		else
+			memcpy_fromio(buf, addr, count);
+
+		if (count == size)
+			return;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos, · line 214

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -0
+
+		pos += count;
+		buf += count / 4;
+		size -= count;
+	}
+#endif
+
  	spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  	for (last = pos + size; pos < last; pos += 4) {
  		uint32_t tmp = pos >> 31;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#3 lore raw

PATCH 3/4 drm/amdgpu: use amdgpu_device_vram_access in amdgpu_ttm_vram_read

In reply to Christian König

This speeds up the access quite a bit from 2.2 MB/s to 2.9 MB/s on 32bit and 12,8 MB/s on 64bit.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 27 ++++++++++---------------
 1 file changed, 11 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +11 -16
index ae1b00def5d8..58d143b24ba0 100644
@@ -59,6 +59,8 @@
 #include "amdgpu_ras.h"
 #include "bif/bif_4_1_d.h"
 
+#define AMDGPU_TTM_VRAM_MAX_DW_READ	(size_t)128
+
 static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
 			     struct ttm_mem_reg *mem, unsigned num_pages,
 			     uint64_t offset, unsigned window,
@@ -2255,27 +2257,20 @@ static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
 	if (*pos >= adev->gmc.mc_vram_size)
 		return -ENXIO;
 
+	size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
 	while (size) {
-		unsigned long flags;
-		uint32_t value;
-
-		if (*pos >= adev->gmc.mc_vram_size)
-			return result;
-
-		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
-		WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
-		value = RREG32_NO_KIQ(mmMM_DATA);
-		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+		size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
+		uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
 
-		r = put_user(value, (uint32_t *)buf);
+		amdgpu_device_vram_access(adev, *pos, value, bytes, false);
+		r = copy_to_user(buf, value, bytes);
 		if (r)
 			return r;
 
-		result += 4;
-		buf += 4;
-		*pos += 4;
-		size -= 4;
+		result += bytes;
+		buf += bytes;
+		*pos += bytes;
+		size -= bytes;
 	}
 
 	return result;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#4 lore raw

PATCH 4/4 drm/amdgpu: use amdgpu_device_vram_access in amdgpu_ttm_access_memory

In reply to Christian König

Make use of the better performance here as well.

This patch is only compile tested!

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++----------
 1 file changed, 23 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +23 -15
index 58d143b24ba0..538c3b52b712 100644
@@ -1565,7 +1565,7 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 
 	while (len && pos < adev->gmc.mc_vram_size) {
 		uint64_t aligned_pos = pos & ~(uint64_t)3;
-		uint32_t bytes = 4 - (pos & 3);
+		uint64_t bytes = 4 - (pos & 3);
 		uint32_t shift = (pos & 3) * 8;
 		uint32_t mask = 0xffffffff << shift;
 
@@ -1574,20 +1574,28 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 			bytes = len;
 		}
 
-		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
-		WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
-		if (!write || mask != 0xffffffff)
-			value = RREG32_NO_KIQ(mmMM_DATA);
-		if (write) {
-			value &= ~mask;
-			value |= (*(uint32_t *)buf << shift) & mask;
-			WREG32_NO_KIQ(mmMM_DATA, value);
-		}
-		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
-		if (!write) {
-			value = (value & mask) >> shift;
-			memcpy(buf, &value, bytes);
+		if (mask != 0xffffffff) {
+			spin_lock_irqsave(&adev->mmio_idx_lock, flags);
+			WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
+			WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
+			if (!write || mask != 0xffffffff)
+				value = RREG32_NO_KIQ(mmMM_DATA);
+			if (write) {
+				value &= ~mask;
+				value |= (*(uint32_t *)buf << shift) & mask;
+				WREG32_NO_KIQ(mmMM_DATA, value);
+			}
+			spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+			if (!write) {
+				value = (value & mask) >> shift;
+				memcpy(buf, &value, bytes);
+			}
+		} else {
+			bytes = (nodes->start + nodes->size) << PAGE_SHIFT;
+			bytes = min(pos - bytes, (uint64_t)len & ~0x3ull);
+
+			amdgpu_device_vram_access(adev, pos, (uint32_t *)buf,
+						  bytes, write);
 		}
 
 		ret += bytes;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Acked-by on v1 4/4
#5 lore raw

RE: [PATCH 4/4] drm/amdgpu: use amdgpu_device_vram_access in amdgpu_ttm_access_memory

[AMD Public Use]

Tested on Vega20 via proc mem op reads.

Old MMIO ~2.7MB/s, Improved MMIO ~3.2MB/s, BAR ~44MB/s

-----Original Message-----
From: Christian König <[email protected]>
Sent: Wednesday, February 5, 2020 10:23 AM
To: [email protected]; Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>
Subject: [PATCH 4/4] drm/amdgpu: use amdgpu_device_vram_access in amdgpu_ttm_access_memory

[CAUTION: External Email]

Make use of the better performance here as well.

This patch is only compile tested!

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++----------
 1 file changed, 23 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +23 -15
index 58d143b24ba0..538c3b52b712 100644
@@ -1565,7 +1565,7 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 
        while (len && pos < adev->gmc.mc_vram_size) {
                uint64_t aligned_pos = pos & ~(uint64_t)3;
-               uint32_t bytes = 4 - (pos & 3);
+               uint64_t bytes = 4 - (pos & 3);
                uint32_t shift = (pos & 3) * 8;
                uint32_t mask = 0xffffffff << shift;
 
@@ -1574,20 +1574,28 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
                        bytes = len;
                }
 
-               spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-               WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
-               WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
-               if (!write || mask != 0xffffffff)
-                       value = RREG32_NO_KIQ(mmMM_DATA);
-               if (write) {
-                       value &= ~mask;
-                       value |= (*(uint32_t *)buf << shift) & mask;
-                       WREG32_NO_KIQ(mmMM_DATA, value);
-               }
-               spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
-               if (!write) {
-                       value = (value & mask) >> shift;
-                       memcpy(buf, &value, bytes);
+               if (mask != 0xffffffff) {
+                       spin_lock_irqsave(&adev->mmio_idx_lock, flags);
+                       WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
+                       WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
+                       if (!write || mask != 0xffffffff)
+                               value = RREG32_NO_KIQ(mmMM_DATA);
+                       if (write) {
+                               value &= ~mask;
+                               value |= (*(uint32_t *)buf << shift) & mask;
+                               WREG32_NO_KIQ(mmMM_DATA, value);
+                       }
+                       spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+                       if (!write) {
+                               value = (value & mask) >> shift;
+                               memcpy(buf, &value, bytes);
+                       }
+               } else {
+                       bytes = (nodes->start + nodes->size) << PAGE_SHIFT;
+                       bytes = min(pos - bytes, (uint64_t)len & ~0x3ull);
+
+                       amdgpu_device_vram_access(adev, pos, (uint32_t *)buf,
+                                                 bytes, write);
                }
 
                ret += bytes;

--
2.17.1
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by on v1 4/4
#6 lore raw

Re: [PATCH 4/4] drm/amdgpu: use amdgpu_device_vram_access in amdgpu_ttm_access_memory

In reply to Christian König

Series is:

Quoted 69 lines · Christian König

Make use of the better performance here as well.

This patch is only compile tested!

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++----------
 1 file changed, 23 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +23 -16
index 58d143b24ba0..538c3b52b712 100644
@@ -1565,7 +1565,7 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 
        while (len && pos < adev->gmc.mc_vram_size) {
                uint64_t aligned_pos = pos & ~(uint64_t)3;
-               uint32_t bytes = 4 - (pos & 3);
+               uint64_t bytes = 4 - (pos & 3);
                uint32_t shift = (pos & 3) * 8;
                uint32_t mask = 0xffffffff << shift;
 
@@ -1574,20 +1574,28 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
                        bytes = len;
                }
 
-               spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-               WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
-               WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
-               if (!write || mask != 0xffffffff)
-                       value = RREG32_NO_KIQ(mmMM_DATA);
-               if (write) {
-                       value &= ~mask;
-                       value |= (*(uint32_t *)buf << shift) & mask;
-                       WREG32_NO_KIQ(mmMM_DATA, value);
-               }
-               spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
-               if (!write) {
-                       value = (value & mask) >> shift;
-                       memcpy(buf, &value, bytes);
+               if (mask != 0xffffffff) {
+                       spin_lock_irqsave(&adev->mmio_idx_lock, flags);
+                       WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
+                       WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
+                       if (!write || mask != 0xffffffff)
+                               value = RREG32_NO_KIQ(mmMM_DATA);
+                       if (write) {
+                               value &= ~mask;
+                               value |= (*(uint32_t *)buf << shift) & mask;
+                               WREG32_NO_KIQ(mmMM_DATA, value);
+                       }
+                       spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+                       if (!write) {
+                               value = (value & mask) >> shift;
+                               memcpy(buf, &value, bytes);
+                       }
+               } else {
+                       bytes = (nodes->start + nodes->size) << PAGE_SHIFT;
+                       bytes = min(pos - bytes, (uint64_t)len & ~0x3ull);
+
+                       amdgpu_device_vram_access(adev, pos, (uint32_t *)buf,
+                                                 bytes, write);
                }
 
                ret += bytes;
--

2.17.1

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#7 lore raw

Re: [PATCH 4/4] drm/amdgpu: use amdgpu_device_vram_access in amdgpu_ttm_access_memory

In reply to Christian König
Quoted 49 lines · Christian König

Make use of the better performance here as well.

This patch is only compile tested!

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++----------
  1 file changed, 23 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -15
index 58d143b24ba0..538c3b52b712 100644
@@ -1565,7 +1565,7 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 
  	while (len && pos < adev->gmc.mc_vram_size) {
  		uint64_t aligned_pos = pos & ~(uint64_t)3;
-		uint32_t bytes = 4 - (pos & 3);
+		uint64_t bytes = 4 - (pos & 3);
  		uint32_t shift = (pos & 3) * 8;
  		uint32_t mask = 0xffffffff << shift;
 
@@ -1574,20 +1574,28 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
  			bytes = len;
  		}
 
-		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
-		WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
-		if (!write || mask != 0xffffffff)
-			value = RREG32_NO_KIQ(mmMM_DATA);
-		if (write) {
-			value &= ~mask;
-			value |= (*(uint32_t *)buf << shift) & mask;
-			WREG32_NO_KIQ(mmMM_DATA, value);
-		}
-		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
-		if (!write) {
-			value = (value & mask) >> shift;
-			memcpy(buf, &value, bytes);
+		if (mask != 0xffffffff) {
+			spin_lock_irqsave(&adev->mmio_idx_lock, flags);
+			WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
+			WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
+			if (!write || mask != 0xffffffff)
+				value = RREG32_NO_KIQ(mmMM_DATA);
+			if (write) {
+				value &= ~mask;
+				value |= (*(uint32_t *)buf << shift) & mask;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo, · line 1595

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+				WREG32_NO_KIQ(mmMM_DATA, value);
+			}
+			spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+			if (!write) {
+				value = (value & mask) >> shift;
+				memcpy(buf, &value, bytes);
+			}
+		} else {
+			bytes = (nodes->start + nodes->size) << PAGE_SHIFT;
+			bytes = min(pos - bytes, (uint64_t)len & ~0x3ull);

I think this is incorrect. The following should be true: pos < ((nodes->start + nodes->size) << PAGE_SHIFT). Consequently pos - bytes is always negative here. But because you're doing unsigned math it will underflow to a big positive number, which is never the minimum. Therefore the min will always be len & ~0x3ull.

I believe this should be min(bytes - pos, (uint64_t)len & ~0x3ull).

Jon, to catch this bug, you'd need a test that first fragments VRAM (allocates lots of 2MB buffers and frees every other buffer), then allocates a large non-contiguous buffer. Then you need one 4KB or smaller access that crosses a boundary between 2MB VRAM buffer chunks.

Christian, your optimized VRAM allocator that tries to get large contiguous chunks is nice for performance, but it probably has a tendency to hide this kind of bug. I wonder if we should have a debug mode that forces non-contiguous buffers to be actually non-contiguous.

Regards,
  Felix

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo, · line 1601

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+
+			amdgpu_device_vram_access(adev, pos, (uint32_t *)buf,
+						  bytes, write);
  		}
 
  		ret += bytes;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx