Skip to contentSkip to conversation

drm/amdgpu: Add interface to protect VRAM at exact position

PATCH In review Horace Chen 3 participants 7 messages Nov 1, 2017 3 messages not in the archive

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

PATCH 1/3 drm/amdgpu: Add interface to protect VRAM at exact position

The existing method to reserve specified VRAM is to create a bo on system domain then pin it to VRAM. But in this process the existing data on the VRAM will be broken, because ttm will allocate a zero buffer on system domain then copy the zeroes to VRAM.

Actually SRIOV need to reserve VRAM to protect data at exact position.

So here add a new interface to create bo at the exact position directly and then pin it immediately to avoid eviction and avoid breaking the existing data on the VRAM.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 84 ++++++++++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  4 ++
 2 files changed, 85 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +81 -3
index e44b880..7108dc5 100644
@@ -281,7 +281,7 @@ void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
 		*cpu_addr = NULL;
 }
 
-static int amdgpu_bo_do_create(struct amdgpu_device *adev,
+static int amdgpu_bo_do_create(struct amdgpu_device *adev, u64 offset,
 			       unsigned long size, int byte_align,
 			       bool kernel, u32 domain, u64 flags,
 			       struct sg_table *sg,
@@ -295,6 +295,7 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 	u64 initial_bytes_moved, bytes_moved;
 	size_t acc_size;
 	int r;
+	int i;
 
 	page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
 	size = ALIGN(size, PAGE_SIZE);
@@ -364,6 +365,21 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 	bo->tbo.bdev = &adev->mman.bdev;
 	amdgpu_ttm_placement_from_domain(bo, domain);
 
+	/*
+	 * if offset is not ~0, it means the offset of this bo is restricted.
+	 * modify the placement to create bo at the exact place.
+	 */
+	if (((domain & AMDGPU_GEM_DOMAIN_VRAM) ||
+	    (domain & AMDGPU_GEM_DOMAIN_GTT)) && offset != ~0) {
+		offset = ALIGN(offset, PAGE_SIZE);
+		for (i = 0; i < bo->placement.num_placement; ++i) {
+			bo->placements[i].fpfn =
+			    offset >> PAGE_SHIFT;
+			bo->placements[i].lpfn =
+			    (offset + size) >> PAGE_SHIFT;
+		}
+	}
+
 	initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
 	/* Kernel allocation are uninterruptible */
 	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
@@ -416,6 +432,68 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 	return r;
 }
 
+/**
+ * amdgpu_bo_create_vram_restricted_kernel -
+ *    create BO at the specified place on the VRAM.
+ *
+ * @adev: amdgpu device object
+ * @offset: start offset of the new BO
+ * @size: size for the new BO
+ * @byte_align: alignment for the new BO
+ * @flags: addition flags for the BO
+ * @bo_ptr: resulting BO
+ * @gpu_addr: GPU addr of the pinned BO
+ * @cpu_addr: optional CPU address mapping
+ *
+ * Allocates and pins a BO at the specified place on the VRAM.
+ *
+ * Returns 0 on success, negative error code otherwise.
+ */
+int amdgpu_bo_create_vram_restricted_kernel(struct amdgpu_device *adev,
+		     u64 offset, unsigned long size, int byte_align,
+		     u64 flags, struct amdgpu_bo **bo_ptr,
+		     u64 *gpu_addr, void **cpu_addr)
+{
+	int r;
+	/* specified memory must be in contiguous*/
+	flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+
+	r = amdgpu_bo_do_create(adev, offset, size, byte_align, true,
+		AMDGPU_GEM_DOMAIN_VRAM, flags, NULL, NULL, 0, bo_ptr);
+	if (r)
+		return r;
+
+	r = amdgpu_bo_reserve(*bo_ptr, false);
+	if (r)
+		goto error_reserve;
+	r = amdgpu_bo_pin_restricted(*bo_ptr,
+		AMDGPU_GEM_DOMAIN_VRAM, offset, (offset + size),
+		gpu_addr);
+	if (r)
+		goto error_pin;
+	if (cpu_addr) {
+		r = amdgpu_bo_kmap(*bo_ptr,
+					cpu_addr);
+		if (r)
+			goto error_kmap;
+	}
+
+	amdgpu_bo_unreserve(*bo_ptr);
+
+	return r;
+error_kmap:
+	amdgpu_bo_unpin(*bo_ptr);
+error_pin:
+	amdgpu_bo_unreserve(*bo_ptr);
+error_reserve:
+	amdgpu_bo_unref(bo_ptr);
+	if (cpu_addr)
+		*cpu_addr = NULL;
+	if (gpu_addr)
+		*gpu_addr = 0;
+	return r;
+}
+
 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
 				   unsigned long size, int byte_align,
 				   struct amdgpu_bo *bo)
@@ -425,7 +503,7 @@ static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
 	if (bo->shadow)
 		return 0;
 
-	r = amdgpu_bo_do_create(adev, size, byte_align, true,
+	r = amdgpu_bo_do_create(adev, ~0, size, byte_align, true,
 				AMDGPU_GEM_DOMAIN_GTT,
 				AMDGPU_GEM_CREATE_CPU_GTT_USWC |
 				AMDGPU_GEM_CREATE_SHADOW,
@@ -455,7 +533,7 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
 	uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
 	int r;
 
-	r = amdgpu_bo_do_create(adev, size, byte_align, kernel, domain,
+	r = amdgpu_bo_do_create(adev, ~0, size, byte_align, kernel, domain,
 				parent_flags, sg, resv, init_value, bo_ptr);
 	if (r)
 		return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
index 33615e2..71f4f82 100644
@@ -216,6 +216,10 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
 			    unsigned long size, int align,
 			    u32 domain, struct amdgpu_bo **bo_ptr,
 			    u64 *gpu_addr, void **cpu_addr);
+int amdgpu_bo_create_vram_restricted_kernel(struct amdgpu_device *adev,
+			     u64 offset, unsigned long size, int byte_align,
+			     u64 flags, struct amdgpu_bo **bo_ptr,
+			     u64 *gpu_addr, void **cpu_addr);
 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
 			   void **cpu_addr);
 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr);
Signature · 6 lines
2.7.4

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

A message that is not in the archive (sent off-list or not yet received): 1509526695-11782-1-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 2/3 drm/amdgpu: change interface when firmware reserving vram

use amdgpu_bo_create_vram_restricted_kernel to reserve bo at specified position and keep the original data intact.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 44 ++++++------------------------
 1 file changed, 8 insertions(+), 36 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +8 -36
index 2ff2c54..c0c6eab 100644
@@ -687,45 +687,17 @@ int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
 	adev->fw_vram_usage.reserved_bo = NULL;
 
 	if (adev->fw_vram_usage.size > 0 &&
-		adev->fw_vram_usage.size <= vram_size) {
+	    adev->fw_vram_usage.size <= vram_size) {
+		r = amdgpu_bo_create_vram_restricted_kernel(adev,
+		     adev->fw_vram_usage.start_offset,
+		     adev->fw_vram_usage.size, PAGE_SIZE,
+		     AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
+		     AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
+		     &adev->fw_vram_usage.reserved_bo,
+		     &gpu_addr, &adev->fw_vram_usage.va);
 
-		r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
-			PAGE_SIZE, true, 0,
-			AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
-			AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
-			&adev->fw_vram_usage.reserved_bo);
-		if (r)
-			goto error_create;
-
-		r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
-		if (r)
-			goto error_reserve;
-		r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
-			AMDGPU_GEM_DOMAIN_VRAM,
-			adev->fw_vram_usage.start_offset,
-			(adev->fw_vram_usage.start_offset +
-			adev->fw_vram_usage.size), &gpu_addr);
-		if (r)
-			goto error_pin;
-		r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
-			&adev->fw_vram_usage.va);
-		if (r)
-			goto error_kmap;
-
-		amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
 	}
 	return r;
-
-error_kmap:
-	amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
-error_pin:
-	amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
-error_reserve:
-	amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
-error_create:
-	adev->fw_vram_usage.va = NULL;
-	adev->fw_vram_usage.reserved_bo = NULL;
-	return r;
 }
 
 
Signature · 6 lines
2.7.4

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

A message that is not in the archive (sent off-list or not yet received): 1509526695-11782-2-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org

Reviewed-by Monk Liu
#4 lore raw

RE: [PATCH 2/3] drm/amdgpu: change interface when firmware reserving vram

-----Original Message-----
From: amd-gfx [mailto:[email protected]] On Behalf Of Horace Chen
Sent: 2017年11月1日 16:58
To: [email protected]
Cc: Chen, Horace <[email protected]>
Subject: [PATCH 2/3] drm/amdgpu: change interface when firmware reserving vram

use amdgpu_bo_create_vram_restricted_kernel to reserve bo at specified position and keep the original data intact.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 44 ++++++------------------------
 1 file changed, 8 insertions(+), 36 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +8 -36
index 2ff2c54..c0c6eab 100644
@@ -687,45 +687,17 @@ int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
 	adev->fw_vram_usage.reserved_bo = NULL;
 
 	if (adev->fw_vram_usage.size > 0 &&
-		adev->fw_vram_usage.size <= vram_size) {
+	    adev->fw_vram_usage.size <= vram_size) {
+		r = amdgpu_bo_create_vram_restricted_kernel(adev,
+		     adev->fw_vram_usage.start_offset,
+		     adev->fw_vram_usage.size, PAGE_SIZE,
+		     AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
+		     AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
+		     &adev->fw_vram_usage.reserved_bo,
+		     &gpu_addr, &adev->fw_vram_usage.va);
 
-		r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
-			PAGE_SIZE, true, 0,
-			AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
-			AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
-			&adev->fw_vram_usage.reserved_bo);
-		if (r)
-			goto error_create;
-
-		r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
-		if (r)
-			goto error_reserve;
-		r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
-			AMDGPU_GEM_DOMAIN_VRAM,
-			adev->fw_vram_usage.start_offset,
-			(adev->fw_vram_usage.start_offset +
-			adev->fw_vram_usage.size), &gpu_addr);
-		if (r)
-			goto error_pin;
-		r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
-			&adev->fw_vram_usage.va);
-		if (r)
-			goto error_kmap;
-
-		amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
 	}
 	return r;
-
-error_kmap:
-	amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
-error_pin:
-	amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
-error_reserve:
-	amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
-error_create:
-	adev->fw_vram_usage.va = NULL;
-	adev->fw_vram_usage.reserved_bo = NULL;
-	return r;
 }
 
 

--
2.7.4

_______________________________________________
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

#5 lore raw

PATCH 3/3 drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware

In reply to a message not in the archive

SR-IOV need to get start offset and size from firmware for its vram reservation. This logic had been add to the atombios code path.

As the current branch will run atomfirmware by default, add same logic to atomfirmware code path.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c +14 -0
index f9ffe8e..8509907 100644
@@ -73,6 +73,8 @@ int amdgpu_atomfirmware_allocate_fb_scratch(struct amdgpu_device *adev)
 						vram_usagebyfirmware);
 	uint16_t data_offset;
 	int usage_bytes = 0;
+	u64 start_addr;
+	u64 size;
 
 	if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
 		struct vram_usagebyfirmware_v2_1 *firmware_usage =
@@ -83,6 +85,18 @@ int amdgpu_atomfirmware_allocate_fb_scratch(struct amdgpu_device *adev)
 			  le16_to_cpu(firmware_usage->used_by_firmware_in_kb),
 			  le16_to_cpu(firmware_usage->used_by_driver_in_kb));
 
+		start_addr = firmware_usage->start_address_in_kb;
+		size = firmware_usage->used_by_firmware_in_kb;
+
+		if ((uint32_t)(start_addr & ATOM_VRAM_OPERATION_FLAGS_MASK) ==
+			(uint32_t)(ATOM_VRAM_BLOCK_SRIOV_MSG_SHARE_RESERVATION <<
+			ATOM_VRAM_OPERATION_FLAGS_SHIFT)) {
+			/* Firmware request VRAM reservation for SR-IOV */
+			adev->fw_vram_usage.start_offset = (start_addr &
+				(~ATOM_VRAM_OPERATION_FLAGS_MASK)) << 10;
+			adev->fw_vram_usage.size = size << 10;
+			/* Use the default scratch size */
+		}
 		usage_bytes = le16_to_cpu(firmware_usage->used_by_driver_in_kb) * 1024;
 	}
 	ctx->scratch_size_bytes = 0;
Signature · 6 lines
2.7.4

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

A message that is not in the archive (sent off-list or not yet received): 1509526695-11782-3-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org

#7 lore raw

RE: [PATCH 3/3] drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware

This one is already submitted

-----Original Message-----
From: amd-gfx [mailto:[email protected]] On Behalf Of Horace Chen
Sent: 2017年11月1日 16:58
To: [email protected]
Cc: Chen, Horace <[email protected]>
Subject: [PATCH 3/3] drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware

SR-IOV need to get start offset and size from firmware for its vram reservation. This logic had been add to the atombios code path.

As the current branch will run atomfirmware by default, add same logic to atomfirmware code path.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c +2 -0
index f9ffe8e..8509907 100644
@@ -73,6 +73,8 @@ int amdgpu_atomfirmware_allocate_fb_scratch(struct amdgpu_device *adev)
 						vram_usagebyfirmware);
 	uint16_t data_offset;
 	int usage_bytes = 0;
+	u64 start_addr;
+	u64 size;
 
 	if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
 		struct vram_usagebyfirmware_v2_1 *firmware_usage = @@ -83,6 +85,18 @@ int amdgpu_atomfirmware_allocate_fb_scratch(struct amdgpu_device *adev)

le16_to_cpu(firmware_usage->used_by_firmware_in_kb), le16_to_cpu(firmware_usage->used_by_driver_in_kb));

+ start_addr = firmware_usage->start_address_in_kb;
+ size = firmware_usage->used_by_firmware_in_kb;
+
+ if ((uint32_t)(start_addr & ATOM_VRAM_OPERATION_FLAGS_MASK) ==
+ (uint32_t)(ATOM_VRAM_BLOCK_SRIOV_MSG_SHARE_RESERVATION <<
+ ATOM_VRAM_OPERATION_FLAGS_SHIFT)) {
+ /* Firmware request VRAM reservation for SR-IOV */
+ adev->fw_vram_usage.start_offset = (start_addr &
+ (~ATOM_VRAM_OPERATION_FLAGS_MASK)) << 10;
+ adev->fw_vram_usage.size = size << 10;
+ /* Use the default scratch size */
+ }
usage_bytes = le16_to_cpu(firmware_usage->used_by_driver_in_kb) * 1024;
}
ctx->scratch_size_bytes = 0;
--
2.7.4

_______________________________________________
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

#8 lore raw
In reply to a message not in the archive

Am 01.11.2017 um 09:58 schrieb Horace Chen:

Quoted 5 lines

The existing method to reserve specified VRAM is to create a bo on system domain then pin it to VRAM. But in this process the existing data on the VRAM will be broken, because ttm will allocate a zero buffer on system domain then copy the zeroes to VRAM.

Quoted 8 lines

Actually SRIOV need to reserve VRAM to protect data at exact position.

So here add a new interface to create bo at the exact position directly and then pin it immediately to avoid eviction and avoid breaking the existing data on the VRAM.

Modifying amdgpu_bo_do_create is still a NAK from myside, that function is why to complicated as it already is.

If you need to assign a specific placement without clearing it you can call ttm_bo_mem_space() manually to do so.

E.g. something like this:

ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
r = ttm_bo_mem_space(&bo->tbo, &placement, &bo->tbo.mem, false, false);

That assigns the new memory without any copy operation.

Regards,
Christian.

Quoted 140 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 84 ++++++++++++++++++++++++++++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  4 ++
  2 files changed, 85 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +81 -3
index e44b880..7108dc5 100644
@@ -281,7 +281,7 @@ void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  		*cpu_addr = NULL;
  }
 
-static int amdgpu_bo_do_create(struct amdgpu_device *adev,
+static int amdgpu_bo_do_create(struct amdgpu_device *adev, u64 offset,
  			       unsigned long size, int byte_align,
  			       bool kernel, u32 domain, u64 flags,
  			       struct sg_table *sg,
@@ -295,6 +295,7 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  	u64 initial_bytes_moved, bytes_moved;
  	size_t acc_size;
  	int r;
+	int i;
 
  	page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  	size = ALIGN(size, PAGE_SIZE);
@@ -364,6 +365,21 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  	bo->tbo.bdev = &adev->mman.bdev;
  	amdgpu_ttm_placement_from_domain(bo, domain);
 
+	/*
+	 * if offset is not ~0, it means the offset of this bo is restricted.
+	 * modify the placement to create bo at the exact place.
+	 */
+	if (((domain & AMDGPU_GEM_DOMAIN_VRAM) ||
+	    (domain & AMDGPU_GEM_DOMAIN_GTT)) && offset != ~0) {
+		offset = ALIGN(offset, PAGE_SIZE);
+		for (i = 0; i < bo->placement.num_placement; ++i) {
+			bo->placements[i].fpfn =
+			    offset >> PAGE_SHIFT;
+			bo->placements[i].lpfn =
+			    (offset + size) >> PAGE_SHIFT;
+		}
+	}
+
  	initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  	/* Kernel allocation are uninterruptible */
  	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
@@ -416,6 +432,68 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  	return r;
  }
 
+/**
+ * amdgpu_bo_create_vram_restricted_kernel -
+ *    create BO at the specified place on the VRAM.
+ *
+ * @adev: amdgpu device object
+ * @offset: start offset of the new BO
+ * @size: size for the new BO
+ * @byte_align: alignment for the new BO
+ * @flags: addition flags for the BO
+ * @bo_ptr: resulting BO
+ * @gpu_addr: GPU addr of the pinned BO
+ * @cpu_addr: optional CPU address mapping
+ *
+ * Allocates and pins a BO at the specified place on the VRAM.
+ *
+ * Returns 0 on success, negative error code otherwise.
+ */
+int amdgpu_bo_create_vram_restricted_kernel(struct amdgpu_device *adev,
+		     u64 offset, unsigned long size, int byte_align,
+		     u64 flags, struct amdgpu_bo **bo_ptr,
+		     u64 *gpu_addr, void **cpu_addr)
+{
+	int r;
+	/* specified memory must be in contiguous*/
+	flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+
+	r = amdgpu_bo_do_create(adev, offset, size, byte_align, true,
+		AMDGPU_GEM_DOMAIN_VRAM, flags, NULL, NULL, 0, bo_ptr);
+	if (r)
+		return r;
+
+	r = amdgpu_bo_reserve(*bo_ptr, false);
+	if (r)
+		goto error_reserve;
+	r = amdgpu_bo_pin_restricted(*bo_ptr,
+		AMDGPU_GEM_DOMAIN_VRAM, offset, (offset + size),
+		gpu_addr);
+	if (r)
+		goto error_pin;
+	if (cpu_addr) {
+		r = amdgpu_bo_kmap(*bo_ptr,
+					cpu_addr);
+		if (r)
+			goto error_kmap;
+	}
+
+	amdgpu_bo_unreserve(*bo_ptr);
+
+	return r;
+error_kmap:
+	amdgpu_bo_unpin(*bo_ptr);
+error_pin:
+	amdgpu_bo_unreserve(*bo_ptr);
+error_reserve:
+	amdgpu_bo_unref(bo_ptr);
+	if (cpu_addr)
+		*cpu_addr = NULL;
+	if (gpu_addr)
+		*gpu_addr = 0;
+	return r;
+}
+
  static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  				   unsigned long size, int byte_align,
  				   struct amdgpu_bo *bo)
@@ -425,7 +503,7 @@ static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  	if (bo->shadow)
  		return 0;
 
-	r = amdgpu_bo_do_create(adev, size, byte_align, true,
+	r = amdgpu_bo_do_create(adev, ~0, size, byte_align, true,
  				AMDGPU_GEM_DOMAIN_GTT,
  				AMDGPU_GEM_CREATE_CPU_GTT_USWC |
  				AMDGPU_GEM_CREATE_SHADOW,
@@ -455,7 +533,7 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
  	uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
  	int r;
 
-	r = amdgpu_bo_do_create(adev, size, byte_align, kernel, domain,
+	r = amdgpu_bo_do_create(adev, ~0, size, byte_align, kernel, domain,
  				parent_flags, sg, resv, init_value, bo_ptr);
  	if (r)
  		return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index 33615e2..71f4f82 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h int amdgpu_bo_create_kernel(struct amdgpu_device *adev, · line 225

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
@@ -216,6 +216,10 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  			    unsigned long size, int align,
  			    u32 domain, struct amdgpu_bo **bo_ptr,
  			    u64 *gpu_addr, void **cpu_addr);
+int amdgpu_bo_create_vram_restricted_kernel(struct amdgpu_device *adev,
+			     u64 offset, unsigned long size, int byte_align,
+			     u64 flags, struct amdgpu_bo **bo_ptr,
+			     u64 *gpu_addr, void **cpu_addr);
  void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  			   void **cpu_addr);
  int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr);

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

#9 lore raw
In reply to a message not in the archive

+ if (((domain & AMDGPU_GEM_DOMAIN_VRAM) ||
+ (domain & AMDGPU_GEM_DOMAIN_GTT)) && offset != ~0) {

Why not make it simple? Like:

if ((domain & (AMDGPU_GEM_DOMAIN_VRAM|AMDGPU_GEM_DOMAIN_GTT)) && offset !=~0)

-----Original Message-----
From: amd-gfx [mailto:[email protected]] On Behalf Of Horace Chen
Sent: 2017年11月1日 16:58
To: [email protected]
Cc: Chen, Horace <[email protected]>
Subject: [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position

The existing method to reserve specified VRAM is to create a bo on system domain then pin it to VRAM. But in this process the existing data on the VRAM will be broken, because ttm will allocate a zero buffer on system domain then copy the zeroes to VRAM.

Actually SRIOV need to reserve VRAM to protect data at exact position.

So here add a new interface to create bo at the exact position directly and then pin it immediately to avoid eviction and avoid breaking the existing data on the VRAM.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 84 ++++++++++++++++++++++++++++--  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  4 ++
 2 files changed, 85 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +17 -1
index e44b880..7108dc5 100644
@@ -281,7 +281,7 @@ void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
 		*cpu_addr = NULL;
 }
 
-static int amdgpu_bo_do_create(struct amdgpu_device *adev,
+static int amdgpu_bo_do_create(struct amdgpu_device *adev, u64 offset,
 			       unsigned long size, int byte_align,
 			       bool kernel, u32 domain, u64 flags,
 			       struct sg_table *sg,
@@ -295,6 +295,7 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 	u64 initial_bytes_moved, bytes_moved;
 	size_t acc_size;
 	int r;
+	int i;
 
 	page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
 	size = ALIGN(size, PAGE_SIZE);
@@ -364,6 +365,21 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 	bo->tbo.bdev = &adev->mman.bdev;
 	amdgpu_ttm_placement_from_domain(bo, domain);
 
+	/*
+	 * if offset is not ~0, it means the offset of this bo is restricted.
+	 * modify the placement to create bo at the exact place.
+	 */
+	if (((domain & AMDGPU_GEM_DOMAIN_VRAM) ||
+	    (domain & AMDGPU_GEM_DOMAIN_GTT)) && offset != ~0) {
+		offset = ALIGN(offset, PAGE_SIZE);
+		for (i = 0; i < bo->placement.num_placement; ++i) {
+			bo->placements[i].fpfn =
+			    offset >> PAGE_SHIFT;
+			bo->placements[i].lpfn =
+			    (offset + size) >> PAGE_SHIFT;
+		}
+	}
+
 	initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
 	/* Kernel allocation are uninterruptible */
 	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type, @@ -416,6 +432,68 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,

return r;
}

+/**
+ * amdgpu_bo_create_vram_restricted_kernel -
+ * create BO at the specified place on the VRAM.
+ *
+ * @adev: amdgpu device object
+ * @offset: start offset of the new BO
+ * @size: size for the new BO
+ * @byte_align: alignment for the new BO
+ * @flags: addition flags for the BO
+ * @bo_ptr: resulting BO
+ * @gpu_addr: GPU addr of the pinned BO
+ * @cpu_addr: optional CPU address mapping
+ *
+ * Allocates and pins a BO at the specified place on the VRAM.
+ *
+ * Returns 0 on success, negative error code otherwise.
+ */
+int amdgpu_bo_create_vram_restricted_kernel(struct amdgpu_device *adev,

+		     u64 offset, unsigned long size, int byte_align,
+		     u64 flags, struct amdgpu_bo **bo_ptr,
+		     u64 *gpu_addr, void **cpu_addr)

+{
+ int r;
+ /* specified memory must be in contiguous*/
+ flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+
+ r = amdgpu_bo_do_create(adev, offset, size, byte_align, true,
+ AMDGPU_GEM_DOMAIN_VRAM, flags, NULL, NULL, 0, bo_ptr);
+ if (r)
+ return r;
+
+ r = amdgpu_bo_reserve(*bo_ptr, false);
+ if (r)
+ goto error_reserve;
+ r = amdgpu_bo_pin_restricted(*bo_ptr,
+ AMDGPU_GEM_DOMAIN_VRAM, offset, (offset + size),
+ gpu_addr);
+ if (r)
+ goto error_pin;
+ if (cpu_addr) {
+ r = amdgpu_bo_kmap(*bo_ptr,
+ cpu_addr);
+ if (r)
+ goto error_kmap;
+ }
+
+ amdgpu_bo_unreserve(*bo_ptr);
+
+ return r;
+error_kmap:
+ amdgpu_bo_unpin(*bo_ptr);
+error_pin:
+ amdgpu_bo_unreserve(*bo_ptr);
+error_reserve:
+ amdgpu_bo_unref(bo_ptr);
+ if (cpu_addr)
+ *cpu_addr = NULL;
+ if (gpu_addr)
+ *gpu_addr = 0;
+ return r;
+}
+
static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
unsigned long size, int byte_align, struct amdgpu_bo *bo)
@@ -425,7 +503,7 @@ static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
if (bo->shadow)
return 0;

- r = amdgpu_bo_do_create(adev, size, byte_align, true,
+ r = amdgpu_bo_do_create(adev, ~0, size, byte_align, true,
AMDGPU_GEM_DOMAIN_GTT, AMDGPU_GEM_CREATE_CPU_GTT_USWC | AMDGPU_GEM_CREATE_SHADOW,
@@ -455,7 +533,7 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW; int r;

- r = amdgpu_bo_do_create(adev, size, byte_align, kernel, domain,
+ r = amdgpu_bo_do_create(adev, ~0, size, byte_align, kernel, domain,
parent_flags, sg, resv, init_value, bo_ptr);
if (r)
return r;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
index 33615e2..71f4f82 100644
@@ -216,6 +216,10 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
 			    unsigned long size, int align,
 			    u32 domain, struct amdgpu_bo **bo_ptr,
 			    u64 *gpu_addr, void **cpu_addr);
+int amdgpu_bo_create_vram_restricted_kernel(struct amdgpu_device *adev,
+			     u64 offset, unsigned long size, int byte_align,
+			     u64 flags, struct amdgpu_bo **bo_ptr,
+			     u64 *gpu_addr, void **cpu_addr);
 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
 			   void **cpu_addr);
 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr);

--
2.7.4

_______________________________________________
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