[PATCH 1/3] drm/amdgpu: Create csa per ctx

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



create csa for gfx/sdma engine to save the
middle command buffer when gpu preemption triggered.

Signed-off-by: Rex Zhu <Rex.Zhu@xxxxxxx>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c  | 12 ++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 48 ++++++++++++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  5 ++++
 3 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
index 8836186..ad3bbaf 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
@@ -824,8 +824,9 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	struct amdgpu_device *adev = p->adev;
 	struct amdgpu_vm *vm = &fpriv->vm;
 	struct amdgpu_bo_list_entry *e;
-	struct amdgpu_bo_va *bo_va;
+	struct amdgpu_bo_va *bo_va = NULL;
 	struct amdgpu_bo *bo;
+	struct amdgpu_ctx *ctx = p->ctx;
 	int r;
 
 	/* Only for UVD/VCE VM emulation */
@@ -907,10 +908,15 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 		return r;
 
 	if (amdgpu_sriov_vf(adev)) {
-		struct dma_fence *f;
-
 		bo_va = fpriv->csa_va;
 		BUG_ON(!bo_va);
+	} else if (adev->gpu_preemption) {
+		bo_va = ctx->csa_va;
+		BUG_ON(!bo_va);
+	}
+	if (bo_va) {
+		struct dma_fence *f;
+
 		r = amdgpu_vm_bo_update(adev, bo_va, false);
 		if (r)
 			return r;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 8802ff2..6fc3cbe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -68,9 +68,10 @@ static int amdgpu_ctx_priority_permit(struct drm_file *filp,
 }
 
 static int amdgpu_ctx_init(struct amdgpu_device *adev,
+			   struct amdgpu_fpriv *fpriv,
 			   enum drm_sched_priority priority,
 			   struct drm_file *filp,
-			   struct amdgpu_ctx *ctx)
+			   struct amdgpu_ctx *ctx, uint32_t id)
 {
 	unsigned num_entities = amdgput_ctx_total_num_entities();
 	unsigned i, j;
@@ -85,6 +86,25 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
 
 	memset(ctx, 0, sizeof(*ctx));
 	ctx->adev = adev;
+	ctx->resv_space_id = id;
+
+	if (!amdgpu_sriov_vf(adev) && adev->gpu_preemption) {
+		ctx->csa_mc_addr = amdgpu_csa_vaddr(adev, ctx->resv_space_id);
+		r = amdgpu_allocate_static_csa(adev, &ctx->csa_bo,
+					AMDGPU_GEM_DOMAIN_GTT,
+					AMDGPU_CSA_SIZE);
+		if (!r) {
+			r = amdgpu_map_static_csa(adev, &fpriv->vm, ctx->csa_bo,
+							&ctx->csa_va, ctx->csa_mc_addr,
+							AMDGPU_CSA_SIZE);
+			if (r) {
+				amdgpu_free_static_csa(&ctx->csa_bo);
+				return -EINVAL;
+			}
+		} else {
+			return -ENOMEM;
+		}
+	}
 
 	ctx->fences = kcalloc(amdgpu_sched_jobs * num_entities,
 			      sizeof(struct dma_fence*), GFP_KERNEL);
@@ -204,6 +224,16 @@ static void amdgpu_ctx_fini(struct kref *ref)
 	kfree(ctx->fences);
 	kfree(ctx->entities[0]);
 
+	if (!amdgpu_sriov_vf(adev) && adev->gpu_preemption && ctx->csa_bo) {
+		BUG_ON(amdgpu_bo_reserve(ctx->csa_bo, true));
+		amdgpu_vm_bo_rmv(adev, ctx->csa_va);
+		ctx->csa_va = NULL;
+		amdgpu_bo_unreserve(ctx->csa_bo);
+		amdgpu_free_static_csa(&ctx->csa_bo);
+		if (ctx->ctx_mgr)
+			__clear_bit(ctx->resv_space_id - 1, ctx->ctx_mgr->resv_vm_bitmap);
+	}
+
 	mutex_destroy(&ctx->lock);
 
 	kfree(ctx);
@@ -241,6 +271,7 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
 	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
 	struct amdgpu_ctx *ctx;
 	int r;
+	u32 resv_space_id = 0;
 
 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
@@ -253,14 +284,25 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
 		kfree(ctx);
 		return r;
 	}
-
 	*id = (uint32_t)r;
-	r = amdgpu_ctx_init(adev, priority, filp, ctx);
+	if (!amdgpu_sriov_vf(adev) && adev->gpu_preemption) {
+		resv_space_id = find_first_zero_bit(mgr->resv_vm_bitmap, AMDGPU_VM_MAX_NUM_CTX);
+		if (resv_space_id < AMDGPU_VM_MAX_NUM_CTX)
+			__set_bit(resv_space_id, mgr->resv_vm_bitmap);
+		else
+			return -ENOMEM;
+		resv_space_id += 1;
+	}
+
+	r = amdgpu_ctx_init(adev, fpriv, priority, filp, ctx, resv_space_id);
 	if (r) {
 		idr_remove(&mgr->ctx_handles, *id);
 		*id = 0;
 		kfree(ctx);
+		mutex_unlock(&mgr->lock);
+		return r;
 	}
+	ctx->ctx_mgr = mgr;
 	mutex_unlock(&mgr->lock);
 	return r;
 }
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index 94ac951..f29cd2c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
@@ -38,6 +38,10 @@ struct amdgpu_ctx_entity {
 struct amdgpu_ctx {
 	struct kref			refcount;
 	struct amdgpu_device		*adev;
+	struct amdgpu_ctx_mgr		*ctx_mgr;
+	struct amdgpu_bo		*csa_bo;
+	struct amdgpu_bo_va		*csa_va;
+	uint64_t			csa_mc_addr;
 
 	unsigned			reset_counter;
 	unsigned			reset_counter_query;
@@ -50,6 +54,7 @@ struct amdgpu_ctx {
 	enum drm_sched_priority		override_priority;
 	struct mutex			lock;
 	atomic_t			guilty;
+	u32				resv_space_id;
 };
 
 struct amdgpu_ctx_mgr {
-- 
1.9.1

_______________________________________________
amd-gfx mailing list
amd-gfx@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/amd-gfx




[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux