Skip to contentSkip to conversation

drm/amdgpu/ttm: optimize vram access in amdgpu_ttm_access_memory()

RFC PATCH In review Kevin Wang 2 participants 4 messages Jul 13, 2021

Subsystems (2)
lore
Share & tools
lore

Kevin Wang

author
#0 lore raw

RFC PATCH v2 1/1 drm/amdgpu/ttm: optimize vram access in amdgpu_ttm_access_memory()

1. using vram aper to access vram if possible
2. avoid MM_INDEX/MM_DATA is not working when mmio protect feature is enabled.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 126 +++++++++++++++++-------
 1 file changed, 89 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +89 -37
index 2aa2eb5de37a..8f6f605bc459 100644
@@ -1407,6 +1407,91 @@ static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
 	return ttm_bo_eviction_valuable(bo, place);
 }
 
+static void amdgpu_ttm_vram_mm_align_access(struct amdgpu_device *adev, loff_t pos,
+					    uint32_t *value, bool write)
+{
+	unsigned long flags;
+
+	BUG_ON(!IS_ALIGNED(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 (write)
+		WREG32_NO_KIQ(mmMM_DATA, *value);
+	else
+		*value = RREG32_NO_KIQ(mmMM_DATA);
+	spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+}
+
+static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
+				      void *buf, size_t size, bool write)
+{
+	while (size) {
+		uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
+		uint64_t bytes = 4 - (pos & 0x3);
+		uint32_t shift = (pos & 0x3) * 8;
+		uint32_t mask = 0xffffffff << shift;
+		uint32_t value = 0;
+
+		if (size < bytes) {
+			mask &= 0xffffffff >> (bytes - size) * 8;
+			bytes = size;
+		}
+
+		if (mask != 0xffffffff) {
+			amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, false);
+			if (write) {
+				value &= ~mask;
+				value |= (*(uint32_t *)buf << shift) & mask;
+				amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, true);
+			} else {
+				value = (value & mask) >> shift;
+				memcpy(buf, &value, bytes);
+			}
+		} else {
+			amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, buf, write);
+		}
+
+		pos += bytes;
+		buf += bytes;
+		size -= bytes;
+	}
+}
+
+static void amdgpu_ttm_vram_access(struct amdgpu_device *adev, loff_t pos,
+				   void *buf, size_t size, bool write)
+{
+	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);
+			mb();
+			amdgpu_device_flush_hdp(adev, NULL);
+		} else {
+			amdgpu_device_invalidate_hdp(adev, NULL);
+			mb();
+			memcpy_fromio(buf, addr, count);
+		}
+
+		if (count == size)
+			return;
+
+		pos += count;
+		buf += count;
+		size -= count;
+	}
+#endif
+
+	amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
+}
+
 /**
  * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
  *
@@ -1426,8 +1511,6 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
 	struct amdgpu_res_cursor cursor;
-	unsigned long flags;
-	uint32_t value = 0;
 	int ret = 0;
 
 	if (bo->mem.mem_type != TTM_PL_VRAM)
@@ -1435,41 +1518,10 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 
 	amdgpu_res_first(&bo->mem, offset, len, &cursor);
 	while (cursor.remaining) {
-		uint64_t aligned_pos = cursor.start & ~(uint64_t)3;
-		uint64_t bytes = 4 - (cursor.start & 3);
-		uint32_t shift = (cursor.start & 3) * 8;
-		uint32_t mask = 0xffffffff << shift;
-
-		if (cursor.size < bytes) {
-			mask &= 0xffffffff >> (bytes - cursor.size) * 8;
-			bytes = cursor.size;
-		}
-
-		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);
-			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 = cursor.size & ~0x3ULL;
-			amdgpu_device_vram_access(adev, cursor.start,
-						  (uint32_t *)buf, bytes,
-						  write);
-		}
-
-		ret += bytes;
-		buf = (uint8_t *)buf + bytes;
-		amdgpu_res_next(&cursor, bytes);
+		amdgpu_ttm_vram_access(adev, cursor.start, buf, cursor.size, write);
+		ret += cursor.size;
+		buf += cursor.size;
+		amdgpu_res_next(&cursor, cursor.size);
 	}
 
 	return ret;
Signature · 6 lines
2.17.1

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

Am 13.07.21 um 05:23 schrieb Kevin Wang:

Quoted 14 lines

1. using vram aper to access vram if possible
2. avoid MM_INDEX/MM_DATA is not working when mmio protect feature is enabled.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 126 +++++++++++++++++-------
  1 file changed, 89 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 2aa2eb5de37a..8f6f605bc459 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1411

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
@@ -1407,6 +1407,91 @@ static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  	return ttm_bo_eviction_valuable(bo, place);
  }
 
+static void amdgpu_ttm_vram_mm_align_access(struct amdgpu_device *adev, loff_t pos,
+					    uint32_t *value, bool write)

Please drop the _vram_ and _align_ part from the name. Just amdgpu_device_mm_access.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+{
+	unsigned long flags;
+
+	BUG_ON(!IS_ALIGNED(pos, 4));

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1425

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+
+	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 (write)
+		WREG32_NO_KIQ(mmMM_DATA, *value);
+	else
+		*value = RREG32_NO_KIQ(mmMM_DATA);
+	spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+}

That should still be in amdgpu_device.c and you completely messed up the drm_dev_enter()/drm_dev_exit() annotation.

Looks like you are working on an old branch where that is not yet merged?

Quoted 55 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +55 -0
+
+static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
+				      void *buf, size_t size, bool write)
+{
+	while (size) {
+		uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
+		uint64_t bytes = 4 - (pos & 0x3);
+		uint32_t shift = (pos & 0x3) * 8;
+		uint32_t mask = 0xffffffff << shift;
+		uint32_t value = 0;
+
+		if (size < bytes) {
+			mask &= 0xffffffff >> (bytes - size) * 8;
+			bytes = size;
+		}
+
+		if (mask != 0xffffffff) {
+			amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, false);
+			if (write) {
+				value &= ~mask;
+				value |= (*(uint32_t *)buf << shift) & mask;
+				amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, true);
+			} else {
+				value = (value & mask) >> shift;
+				memcpy(buf, &value, bytes);
+			}
+		} else {
+			amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, buf, write);
+		}
+
+		pos += bytes;
+		buf += bytes;
+		size -= bytes;
+	}
+}
+
+static void amdgpu_ttm_vram_access(struct amdgpu_device *adev, loff_t pos,
+				   void *buf, size_t size, bool write)
+{
+	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);
+			mb();
+			amdgpu_device_flush_hdp(adev, NULL);
+		} else {
+			amdgpu_device_invalidate_hdp(adev, NULL);
+			mb();
+			memcpy_fromio(buf, addr, count);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1490

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+		}
+
+		if (count == size)
+			return;
+
+		pos += count;
+		buf += count;
+		size -= count;
+	}
+#endif

I would put this as a separate function into amdgpu_device.c.

But all of this should only be the second step since we need a much smaller patch for backporting first.

Quoted 35 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -17
+
+	amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
+}
+
  /**
   * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
   *
@@ -1426,8 +1511,6 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
  	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
  	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  	struct amdgpu_res_cursor cursor;
-	unsigned long flags;
-	uint32_t value = 0;
  	int ret = 0;
 
  	if (bo->mem.mem_type != TTM_PL_VRAM)
@@ -1435,41 +1518,10 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 
  	amdgpu_res_first(&bo->mem, offset, len, &cursor);
  	while (cursor.remaining) {
-		uint64_t aligned_pos = cursor.start & ~(uint64_t)3;
-		uint64_t bytes = 4 - (cursor.start & 3);
-		uint32_t shift = (cursor.start & 3) * 8;
-		uint32_t mask = 0xffffffff << shift;
-
-		if (cursor.size < bytes) {
-			mask &= 0xffffffff >> (bytes - cursor.size) * 8;
-			bytes = cursor.size;
-		}
-
-		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);
-			value = RREG32_NO_KIQ(mmMM_DATA);

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -10
-			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);
-			}

This here is the problematic part and should use amdgpu_ttm_vram_access() instead.

That should be implemented first since we might need to backport that.

Regards,
Christian.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -7
-		} else {
-			bytes = cursor.size & ~0x3ULL;
-			amdgpu_device_vram_access(adev, cursor.start,
-						  (uint32_t *)buf, bytes,
-						  write);
-		}
-

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -3
-		ret += bytes;
-		buf = (uint8_t *)buf + bytes;
-		amdgpu_res_next(&cursor, bytes);
+		amdgpu_ttm_vram_access(adev, cursor.start, buf, cursor.size, write);
+		ret += cursor.size;
+		buf += cursor.size;
+		amdgpu_res_next(&cursor, cursor.size);
  	}
 
  	return ret;

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

author Reviewed-by Alex Deucher
#2 lore raw

[AMD Official Use Only]

<comments inline>

________________________________
From: Christian König <[email protected]>
Sent: Tuesday, July 13, 2021 3:11 PM
To: Wang, Kevin(Yang) <[email protected]>; [email protected] <[email protected]>
Cc: Lazar, Lijo <[email protected]>; Deucher, Alexander <[email protected]>; Min, Frank <[email protected]>; Koenig, Christian <[email protected]>; Zhang, Hawking <[email protected]>
Subject: Re: [RFC PATCH v2] drm/amdgpu/ttm: optimize vram access in amdgpu_ttm_access_memory()

Am 13.07.21 um 05:23 schrieb Kevin Wang:

Quoted 14 lines

1. using vram aper to access vram if possible
2. avoid MM_INDEX/MM_DATA is not working when mmio protect feature is enabled.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 126 +++++++++++++++++-------
  1 file changed, 89 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 2aa2eb5de37a..8f6f605bc459 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1411

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
@@ -1407,6 +1407,91 @@ static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
       return ttm_bo_eviction_valuable(bo, place);
  }
 
+static void amdgpu_ttm_vram_mm_align_access(struct amdgpu_device *adev, loff_t pos,
+                                         uint32_t *value, bool write)

Please drop the _vram_ and _align_ part from the name. Just amdgpu_device_mm_access.

[kevin]: I will correct it in next patch.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+{
+     unsigned long flags;
+
+     BUG_ON(!IS_ALIGNED(pos, 4));

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1425

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+
+     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 (write)
+             WREG32_NO_KIQ(mmMM_DATA, *value);
+     else
+             *value = RREG32_NO_KIQ(mmMM_DATA);
+     spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+}

That should still be in amdgpu_device.c and you completely messed up the drm_dev_enter()/drm_dev_exit() annotation.

Looks like you are working on an old branch where that is not yet merged?

[kevin]: yes, I'm working on amd-staging-drm-next branch, the following patch (from drm-next-misc) is not merged into this branch.

drm/amdgpu: Guard against write accesses after device removal

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

v5:
Protect more places wher memcopy_to/form_io takes place Protect IB submissions

v6: Switch to !drm_dev_enter instead of scoping entire code with brackets.

v7:
Drop guard of HW ring commands emission protection since they are in GART and not in MMIO.

Quoted 55 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +55 -0
+
+static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
+                                   void *buf, size_t size, bool write)
+{
+     while (size) {
+             uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
+             uint64_t bytes = 4 - (pos & 0x3);
+             uint32_t shift = (pos & 0x3) * 8;
+             uint32_t mask = 0xffffffff << shift;
+             uint32_t value = 0;
+
+             if (size < bytes) {
+                     mask &= 0xffffffff >> (bytes - size) * 8;
+                     bytes = size;
+             }
+
+             if (mask != 0xffffffff) {
+                     amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, false);
+                     if (write) {
+                             value &= ~mask;
+                             value |= (*(uint32_t *)buf << shift) & mask;
+                             amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, true);
+                     } else {
+                             value = (value & mask) >> shift;
+                             memcpy(buf, &value, bytes);
+                     }
+             } else {
+                     amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, buf, write);
+             }
+
+             pos += bytes;
+             buf += bytes;
+             size -= bytes;
+     }
+}
+
+static void amdgpu_ttm_vram_access(struct amdgpu_device *adev, loff_t pos,
+                                void *buf, size_t size, bool write)
+{
+     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);
+                     mb();
+                     amdgpu_device_flush_hdp(adev, NULL);
+             } else {
+                     amdgpu_device_invalidate_hdp(adev, NULL);
+                     mb();
+                     memcpy_fromio(buf, addr, count);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1490

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+             }
+
+             if (count == size)
+                     return;
+
+             pos += count;
+             buf += count;
+             size -= count;
+     }
+#endif

I would put this as a separate function into amdgpu_device.c.

But all of this should only be the second step since we need a much smaller patch for backporting first.

Quoted 35 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -17
+
+     amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
+}
+
  /**
   * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
   *
@@ -1426,8 +1511,6 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
       struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
       struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
       struct amdgpu_res_cursor cursor;
-     unsigned long flags;
-     uint32_t value = 0;
       int ret = 0;
 
       if (bo->mem.mem_type != TTM_PL_VRAM)
@@ -1435,41 +1518,10 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 
       amdgpu_res_first(&bo->mem, offset, len, &cursor);
       while (cursor.remaining) {
-             uint64_t aligned_pos = cursor.start & ~(uint64_t)3;
-             uint64_t bytes = 4 - (cursor.start & 3);
-             uint32_t shift = (cursor.start & 3) * 8;
-             uint32_t mask = 0xffffffff << shift;
-
-             if (cursor.size < bytes) {
-                     mask &= 0xffffffff >> (bytes - cursor.size) * 8;
-                     bytes = cursor.size;
-             }
-
-             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);
-                     value = RREG32_NO_KIQ(mmMM_DATA);

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -10
-                     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);
-                     }

This here is the problematic part and should use
amdgpu_ttm_vram_access() instead.

That should be implemented first since we might need to backport that.

Regards,
Christian.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -7
-             } else {
-                     bytes = cursor.size & ~0x3ULL;
-                     amdgpu_device_vram_access(adev, cursor.start,
-                                               (uint32_t *)buf, bytes,
-                                               write);
-             }
-

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -3
-             ret += bytes;
-             buf = (uint8_t *)buf + bytes;
-             amdgpu_res_next(&cursor, bytes);
+             amdgpu_ttm_vram_access(adev, cursor.start, buf, cursor.size, write);
+             ret += cursor.size;
+             buf += cursor.size;
+             amdgpu_res_next(&cursor, cursor.size);
       }
 
       return ret;

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

#3 lore raw

See below.

Am 13.07.21 um 10:32 schrieb Wang, Kevin(Yang):

Quoted 59 lines

[AMD Official Use Only]

<comments inline>

------------------------------------------------------------------------
*From:* Christian König <[email protected]>
*Sent:* Tuesday, July 13, 2021 3:11 PM
*To:* Wang, Kevin(Yang) <[email protected]>; [email protected] <[email protected]>
*Cc:* Lazar, Lijo <[email protected]>; Deucher, Alexander <[email protected]>; Min, Frank <[email protected]>; Koenig, Christian <[email protected]>; Zhang, Hawking <[email protected]> *Subject:* Re: [RFC PATCH v2] drm/amdgpu/ttm: optimize vram access in amdgpu_ttm_access_memory()
Am 13.07.21 um 05:23 schrieb Kevin Wang:

Review

Quoted 10 lines

1. using vram aper to access vram if possible
2. avoid MM_INDEX/MM_DATA is not working when mmio protect feature is enabled.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 126 +++++++++++++++++-------

   1 file changed, 89 insertions(+), 37 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool

Quoted 4 lines

index 2aa2eb5de37a..8f6f605bc459 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1407,6 +1407,91 @@ static bool 

amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
         return ttm_bo_eviction_valuable(bo, place);
    }
 
+static void amdgpu_ttm_vram_mm_align_access(struct amdgpu_device 

*adev, loff_t pos,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1411

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+                                         uint32_t *value, bool write)

Please drop the _vram_ and _align_ part from the name. Just amdgpu_device_mm_access.

[kevin]: I will correct it in next patch.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+{
+     unsigned long flags;
+
+     BUG_ON(!IS_ALIGNED(pos, 4));

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1425

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+
+ 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 (write)
+             WREG32_NO_KIQ(mmMM_DATA, *value);
+     else
+             *value = RREG32_NO_KIQ(mmMM_DATA);
+ spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+}

That should still be in amdgpu_device.c and you completely messed up the drm_dev_enter()/drm_dev_exit() annotation.

Quoted 3 lines

Looks like you are working on an old branch where that is not yet merged?

[kevin]: yes, I'm working on amd-staging-drm-next branch, the following patch (from drm-next-misc) is not merged into this branch.

Ok then just wait a bit. Alex wanted to update the amd-staging-drm-next branch in the next few days.

There is an internal mail thread about that, maybe ping him on this.

Christian.

Quoted 165 lines

drm/amdgpu: Guard against write accesses after device removal

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

v5:
Protect more places wher memcopy_to/form_io takes place Protect IB submissions

v6: Switch to !drm_dev_enter instead of scoping entire code with brackets.

v7:
Drop guard of HW ring commands emission protection since they are in GART and not in MMIO.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool · line 1427

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+
+static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, 

loff_t pos,

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
+                                   void *buf, size_t size, bool write)
+{
+     while (size) {
+             uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
+             uint64_t bytes = 4 - (pos & 0x3);
+             uint32_t shift = (pos & 0x3) * 8;
+             uint32_t mask = 0xffffffff << shift;
+             uint32_t value = 0;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+             if (size < bytes) {
+                     mask &= 0xffffffff >> (bytes - size) * 8;
+                     bytes = size;
+             }
+
+             if (mask != 0xffffffff) {
+ amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, false);
+                     if (write) {
+                             value &= ~mask;
+                             value |= (*(uint32_t *)buf << shift) & 

mask;

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -0
+ amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, &value, true);
+                     } else {
+                             value = (value & mask) >> shift;
+                             memcpy(buf, &value, bytes);
+                     }
+             } else {

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+ amdgpu_ttm_vram_mm_align_access(adev, aligned_pos, buf, write);
+             }
+
+             pos += bytes;
+             buf += bytes;
+             size -= bytes;
+     }
+}
+
+static void amdgpu_ttm_vram_access(struct amdgpu_device *adev, 

loff_t pos,

Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +18 -0
+                                void *buf, size_t size, bool write)
+{
+     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);
+                     mb();
+                     amdgpu_device_flush_hdp(adev, NULL);
+             } else {
+ amdgpu_device_invalidate_hdp(adev, NULL);
+                     mb();
+                     memcpy_fromio(buf, addr, count);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 1490

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+             }
+
+             if (count == size)
+                     return;
+
+             pos += count;
+             buf += count;
+             size -= count;
+     }
+#endif

I would put this as a separate function into amdgpu_device.c.

But all of this should only be the second step since we need a much smaller patch for backporting first.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+
+     amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
+}
+
    /**
     * amdgpu_ttm_access_memory - Read or Write memory that backs a 

buffer object.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
     *
@@ -1426,8 +1511,6 @@ static int amdgpu_ttm_access_memory(struct 

ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -2
         struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
         struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
         struct amdgpu_res_cursor cursor;
-     unsigned long flags;
-     uint32_t value = 0;
         int ret = 0;
 
         if (bo->mem.mem_type != TTM_PL_VRAM)
@@ -1435,41 +1518,10 @@ static int amdgpu_ttm_access_memory(struct 

ttm_buffer_object *bo,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -3
         amdgpu_res_first(&bo->mem, offset, len, &cursor);
         while (cursor.remaining) {
-             uint64_t aligned_pos = cursor.start & ~(uint64_t)3;
-             uint64_t bytes = 4 - (cursor.start & 3);
-             uint32_t shift = (cursor.start & 3) * 8;

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -10
-             uint32_t mask = 0xffffffff << shift;
-
-             if (cursor.size < bytes) {
-                     mask &= 0xffffffff >> (bytes - cursor.size) * 8;
-                     bytes = cursor.size;
-             }
-
-             if (mask != 0xffffffff) {
- spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-                     WREG32_NO_KIQ(mmMM_INDEX, 

((uint32_t)aligned_pos) | 0x80000000);

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -5
-                     WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
-                     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 1462

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -7
- WREG32_NO_KIQ(mmMM_DATA, value);
-                     }
- spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
-                     if (!write) {
-                             value = (value & mask) >> shift;
-                             memcpy(buf, &value, bytes);
-                     }

This here is the problematic part and should use
amdgpu_ttm_vram_access() instead.

That should be implemented first since we might need to backport that.

Regards,
Christian.

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

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -10
-             } else {
-                     bytes = cursor.size & ~0x3ULL;
- amdgpu_device_vram_access(adev, cursor.start,
- (uint32_t *)buf, bytes,
- write);
-             }
-
-             ret += bytes;
-             buf = (uint8_t *)buf + bytes;
-             amdgpu_res_next(&cursor, bytes);
+             amdgpu_ttm_vram_access(adev, cursor.start, buf, 
Quoted 8 lines

cursor.size, write);

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+             ret += cursor.size;
+             buf += cursor.size;
+             amdgpu_res_next(&cursor, cursor.size);
         }
 
         return ret;

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