All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position
@ 2017-11-01  8:58 Horace Chen
       [not found] ` <1509526695-11782-1-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Horace Chen @ 2017-11-01  8:58 UTC (permalink / raw)
  To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW; +Cc: Horace Chen

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.

Signed-off-by: Horace Chen <horace.chen@amd.com>
---
 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(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
index e44b880..7108dc5 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
@@ -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;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
index 33615e2..71f4f82 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
@@ -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
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] drm/amdgpu: change interface when firmware reserving vram
       [not found] ` <1509526695-11782-1-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
@ 2017-11-01  8:58   ` Horace Chen
       [not found]     ` <1509526695-11782-2-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
  2017-11-01  8:58   ` [PATCH 3/3] drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware Horace Chen
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Horace Chen @ 2017-11-01  8:58 UTC (permalink / raw)
  To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW; +Cc: Horace Chen

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

Signed-off-by: Horace Chen <horace.chen@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 44 ++++++------------------------
 1 file changed, 8 insertions(+), 36 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
index 2ff2c54..c0c6eab 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
@@ -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
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware
       [not found] ` <1509526695-11782-1-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
  2017-11-01  8:58   ` [PATCH 2/3] drm/amdgpu: change interface when firmware reserving vram Horace Chen
@ 2017-11-01  8:58   ` Horace Chen
       [not found]     ` <1509526695-11782-3-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
  2017-11-01  9:12   ` [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position Christian König
  2017-11-01 11:17   ` Liu, Monk
  3 siblings, 1 reply; 7+ messages in thread
From: Horace Chen @ 2017-11-01  8:58 UTC (permalink / raw)
  To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW; +Cc: Horace Chen

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.

Signed-off-by: Horace Chen <horace.chen@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c
index f9ffe8e..8509907 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c
@@ -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
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position
       [not found] ` <1509526695-11782-1-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
  2017-11-01  8:58   ` [PATCH 2/3] drm/amdgpu: change interface when firmware reserving vram Horace Chen
  2017-11-01  8:58   ` [PATCH 3/3] drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware Horace Chen
@ 2017-11-01  9:12   ` Christian König
  2017-11-01 11:17   ` Liu, Monk
  3 siblings, 0 replies; 7+ messages in thread
From: Christian König @ 2017-11-01  9:12 UTC (permalink / raw)
  To: Horace Chen, amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW

Am 01.11.2017 um 09:58 schrieb Horace Chen:
> 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.
>
> Signed-off-by: Horace Chen <horace.chen@amd.com>

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.

> ---
>   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(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> index e44b880..7108dc5 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> @@ -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;
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
> index 33615e2..71f4f82 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
> @@ -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
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position
       [not found] ` <1509526695-11782-1-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
                     ` (2 preceding siblings ...)
  2017-11-01  9:12   ` [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position Christian König
@ 2017-11-01 11:17   ` Liu, Monk
  3 siblings, 0 replies; 7+ messages in thread
From: Liu, Monk @ 2017-11-01 11:17 UTC (permalink / raw)
  To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW; +Cc: Chen, Horace

+	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:amd-gfx-bounces@lists.freedesktop.org] On Behalf Of Horace Chen
Sent: 2017年11月1日 16:58
To: amd-gfx@lists.freedesktop.org
Cc: Chen, Horace <Horace.Chen@amd.com>
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.

Signed-off-by: Horace Chen <horace.chen@amd.com>
---
 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(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
index e44b880..7108dc5 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
@@ -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;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
index 33615e2..71f4f82 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
@@ -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
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* RE: [PATCH 2/3] drm/amdgpu: change interface when firmware reserving vram
       [not found]     ` <1509526695-11782-2-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
@ 2017-11-01 11:17       ` Liu, Monk
  0 siblings, 0 replies; 7+ messages in thread
From: Liu, Monk @ 2017-11-01 11:17 UTC (permalink / raw)
  To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW; +Cc: Chen, Horace

Reviewed-by: Monk Liu <monk.liu@amd.com>

-----Original Message-----
From: amd-gfx [mailto:amd-gfx-bounces@lists.freedesktop.org] On Behalf Of Horace Chen
Sent: 2017年11月1日 16:58
To: amd-gfx@lists.freedesktop.org
Cc: Chen, Horace <Horace.Chen@amd.com>
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.

Signed-off-by: Horace Chen <horace.chen@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 44 ++++++------------------------
 1 file changed, 8 insertions(+), 36 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
index 2ff2c54..c0c6eab 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
@@ -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
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* RE: [PATCH 3/3] drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware
       [not found]     ` <1509526695-11782-3-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
@ 2017-11-01 11:17       ` Liu, Monk
  0 siblings, 0 replies; 7+ messages in thread
From: Liu, Monk @ 2017-11-01 11:17 UTC (permalink / raw)
  To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW; +Cc: Chen, Horace

This one is already submitted

-----Original Message-----
From: amd-gfx [mailto:amd-gfx-bounces@lists.freedesktop.org] On Behalf Of Horace Chen
Sent: 2017年11月1日 16:58
To: amd-gfx@lists.freedesktop.org
Cc: Chen, Horace <Horace.Chen@amd.com>
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.

Signed-off-by: Horace Chen <horace.chen@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c
index f9ffe8e..8509907 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_atomfirmware.c
@@ -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
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-11-01 11:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-01  8:58 [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position Horace Chen
     [not found] ` <1509526695-11782-1-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
2017-11-01  8:58   ` [PATCH 2/3] drm/amdgpu: change interface when firmware reserving vram Horace Chen
     [not found]     ` <1509526695-11782-2-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
2017-11-01 11:17       ` Liu, Monk
2017-11-01  8:58   ` [PATCH 3/3] drm/amdgpu: pick SR-IOV fw reservation information in atomfirmware Horace Chen
     [not found]     ` <1509526695-11782-3-git-send-email-horace.chen-5C7GfCeVMHo@public.gmane.org>
2017-11-01 11:17       ` Liu, Monk
2017-11-01  9:12   ` [PATCH 1/3] drm/amdgpu: Add interface to protect VRAM at exact position Christian König
2017-11-01 11:17   ` Liu, Monk

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.