Skip to contentSkip to conversation

allow DRM_MASTER to change client's priorities

PATCH New Andres Rodriguez 2 participants 24 messages Jun 8, 2017 – Jun 26, 2017 16 messages not in the archive

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

PATCH 1/1 allow DRM_MASTER to change client's priorities

Hey Everyone,

This series is the first spin-off from my RFC "Exclusive gpu access for SteamVR usecases". It also includes an updated version of my priority patchset.

Some notable changes to the old priority patches:
* Patch 1: DRM_MASTER is now allowed to allocate high priority contexts
* Patch 1: Split HIGH into HIGH_SW and HIGH_HW, same for LOW_*
* Patch 2: raise ring priority when a job is submitted, instead of job_run

I've also fixed the problem pointed out in the RFC thread. When all requests to change a process's priority end, that process's contexts will return to the priority specified at allocation time.

Changes to raise the minimum required priority for the scheduler to schedule a job will follow in a separate series.

Series available in the wip-process-priorities-v1 branch of:
git://people.freedesktop.org/~lostgoat/linux

_______________________________________________
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): 20170608220623.9002-1-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH 1/8 drm/amdgpu: add parameter to allocate high priority contexts v11

Add a new context creation parameter to express a global context priority.

The priority ranking in descending order is as follows:
* AMDGPU_CTX_PRIORITY_HIGH_HW
* AMDGPU_CTX_PRIORITY_HIGH_SW
* AMDGPU_CTX_PRIORITY_NORMAL
* AMDGPU_CTX_PRIORITY_LOW_SW
* AMDGPU_CTX_PRIORITY_LOW_HW

The driver will attempt to schedule work to the hardware according to the priorities. No latency or throughput guarantees are provided by this patch.

This interface intends to service the EGL_IMG_context_priority extension, and vulkan equivalents.

Setting a priority above NORMAL requires CAP_SYS_NICE or DRM_MASTER.

v2: Instead of using flags, repurpose __pad
v3: Swap enum values of _NORMAL _HIGH for backwards compatibility
v4: Validate usermode priority and store it
v5: Move priority validation into amdgpu_ctx_ioctl(), headline reword
v6: add UAPI note regarding priorities requiring CAP_SYS_ADMIN
v7: remove ctx->priority
v8: added AMDGPU_CTX_PRIORITY_LOW, s/CAP_SYS_ADMIN/CAP_SYS_NICE
v9: change the priority parameter to __s32
v10: split priorities into _SW and _HW
v11: Allow DRM_MASTER without CAP_SYS_NICE

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c       | 60 +++++++++++++++++++++++++--
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  5 ++-
 include/uapi/drm/amdgpu_drm.h                 | 10 ++++-
 3 files changed, 69 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +56 -4
index a11e443..68a852f 100644
@@ -13,57 +13,83 @@
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors: monk liu <[email protected]>
  */
 
 #include <drm/drmP.h>
 #include "amdgpu.h"
 
-static int amdgpu_ctx_init(struct amdgpu_device *adev, struct amdgpu_ctx *ctx)
+static amdgpu_ctx_priority_permit(struct drm_file *filp,
+				  enum amd_sched_priority priority)
+{
+	/* NORMAL and below are accessible by everyone */
+	if (priority <= AMD_SCHED_PRIORITY_NORMAL)
+		return 0;
+
+	if (capable(CAP_SYS_NICE))
+		return 0;
+
+	if (drm_is_current_master(filp))
+		return 0;
+
+	return -EACCES;
+}
+
+static int amdgpu_ctx_init(struct amdgpu_device *adev,
+			   enum amd_sched_priority priority,
+			   struct drm_file *filp,
+			   struct amdgpu_ctx *ctx)
 {
 	unsigned i, j;
 	int r;
 
+	if (priority < 0 || priority >= AMD_SCHED_PRIORITY_MAX)
+		return -EINVAL;
+
+	r = amdgpu_ctx_priority_permit(filp, priority);
+	if (r)
+		return r;
+
 	memset(ctx, 0, sizeof(*ctx));
 	ctx->adev = adev;
 	kref_init(&ctx->refcount);
 	spin_lock_init(&ctx->ring_lock);
 	ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
 			      sizeof(struct dma_fence*), GFP_KERNEL);
 	if (!ctx->fences)
 		return -ENOMEM;
 
 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
 		ctx->rings[i].sequence = 1;
 		ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
 	}
 
 	ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
 
 	/* create context entity for each ring */
 	for (i = 0; i < adev->num_rings; i++) {
 		struct amdgpu_ring *ring = adev->rings[i];
 		struct amd_sched_rq *rq;
 
-		rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
+		rq = &ring->sched.sched_rq[priority];
 
 		if (ring == &adev->gfx.kiq.ring)
 			continue;
 
 		r = amd_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
 					  rq, amdgpu_sched_jobs);
 		if (r)
 			goto failed;
 	}
 
 	r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
 	if (r)
 		goto failed;
 
 	return 0;
@@ -88,49 +114,52 @@ static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
 		for (j = 0; j < amdgpu_sched_jobs; ++j)
 			dma_fence_put(ctx->rings[i].fences[j]);
 	kfree(ctx->fences);
 	ctx->fences = NULL;
 
 	for (i = 0; i < adev->num_rings; i++)
 		amd_sched_entity_fini(&adev->rings[i]->sched,
 				      &ctx->rings[i].entity);
 
 	amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
 }
 
 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
 			    struct amdgpu_fpriv *fpriv,
+			    struct drm_file *filp,
+			    enum amd_sched_priority priority,
 			    uint32_t *id)
 {
 	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
 	struct amdgpu_ctx *ctx;
 	int r;
 
 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return -ENOMEM;
 
 	mutex_lock(&mgr->lock);
 	r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
 	if (r < 0) {
 		mutex_unlock(&mgr->lock);
 		kfree(ctx);
 		return r;
 	}
+
 	*id = (uint32_t)r;
-	r = amdgpu_ctx_init(adev, ctx);
+	r = amdgpu_ctx_init(adev, priority, filp, ctx);
 	if (r) {
 		idr_remove(&mgr->ctx_handles, *id);
 		*id = 0;
 		kfree(ctx);
 	}
 	mutex_unlock(&mgr->lock);
 	return r;
 }
 
 static void amdgpu_ctx_do_release(struct kref *ref)
 {
 	struct amdgpu_ctx *ctx;
 
 	ctx = container_of(ref, struct amdgpu_ctx, refcount);
 
@@ -176,46 +205,69 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 	out->state.hangs = 0x0;
 
 	/* determine if a GPU reset has occured since the last call */
 	reset_counter = atomic_read(&adev->gpu_reset_counter);
 	/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
 	if (ctx->reset_counter == reset_counter)
 		out->state.reset_status = AMDGPU_CTX_NO_RESET;
 	else
 		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
 	ctx->reset_counter = reset_counter;
 
 	mutex_unlock(&mgr->lock);
 	return 0;
 }
 
+static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
+{
+	switch (amdgpu_priority) {
+	case AMDGPU_CTX_PRIORITY_HIGH_HW:
+		return AMD_SCHED_PRIORITY_HIGH_HW;
+	case AMDGPU_CTX_PRIORITY_HIGH_SW:
+		return AMD_SCHED_PRIORITY_HIGH_SW;
+	case AMDGPU_CTX_PRIORITY_NORMAL:
+		return AMD_SCHED_PRIORITY_NORMAL;
+	case AMDGPU_CTX_PRIORITY_LOW_SW:
+	case AMDGPU_CTX_PRIORITY_LOW_HW:
+		return AMD_SCHED_PRIORITY_LOW;
+	default:
+		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
+		return AMD_SCHED_PRIORITY_NORMAL;
+	}
+}
+
 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 		     struct drm_file *filp)
 {
 	int r;
 	uint32_t id;
+	enum amd_sched_priority priority;
 
 	union drm_amdgpu_ctx *args = data;
 	struct amdgpu_device *adev = dev->dev_private;
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 
 	r = 0;
 	id = args->in.ctx_id;
+	priority = amdgpu_to_sched_priority(args->in.priority);
+
+	if (priority >= AMD_SCHED_PRIORITY_MAX)
+		return -EINVAL;
 
 	switch (args->in.op) {
 	case AMDGPU_CTX_OP_ALLOC_CTX:
-		r = amdgpu_ctx_alloc(adev, fpriv, &id);
+		r = amdgpu_ctx_alloc(adev, fpriv, priority, filp, &id);
 		args->out.alloc.ctx_id = id;
 		break;
 	case AMDGPU_CTX_OP_FREE_CTX:
 		r = amdgpu_ctx_free(fpriv, id);
 		break;
 	case AMDGPU_CTX_OP_QUERY_STATE:
 		r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
 		break;
 	default:
 		return -EINVAL;
 	}
 
 	return r;
 }
 
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +4 -1
index f9d8f28..38e622c 100644
@@ -103,31 +103,34 @@ static inline bool amd_sched_invalidate_job(struct amd_sched_job *s_job, int thr
 }
 
 /**
  * Define the backend operations called by the scheduler,
  * these functions should be implemented in driver side
 */
 struct amd_sched_backend_ops {
 	struct dma_fence *(*dependency)(struct amd_sched_job *sched_job);
 	struct dma_fence *(*run_job)(struct amd_sched_job *sched_job);
 	void (*timedout_job)(struct amd_sched_job *sched_job);
 	void (*free_job)(struct amd_sched_job *sched_job);
 };
 
 enum amd_sched_priority {
 	AMD_SCHED_PRIORITY_MIN,
-	AMD_SCHED_PRIORITY_NORMAL = AMD_SCHED_PRIORITY_MIN,
+	AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
+	AMD_SCHED_PRIORITY_NORMAL,
+	AMD_SCHED_PRIORITY_HIGH_SW,
+	AMD_SCHED_PRIORITY_HIGH_HW,
 	AMD_SCHED_PRIORITY_KERNEL,
 	AMD_SCHED_PRIORITY_MAX
 };
 
 /**
  * One scheduler is implemented for each hardware ring
 */
 struct amd_gpu_scheduler {
 	const struct amd_sched_backend_ops	*ops;
 	uint32_t			hw_submission_limit;
 	long				timeout;
 	const char			*name;
 	struct amd_sched_rq		sched_rq[AMD_SCHED_PRIORITY_MAX];
 	wait_queue_head_t		wake_up_worker;
 	wait_queue_head_t		job_scheduled;
include/uapi/drm/amdgpu_drm.h +9 -1
index 34128f6..590fc2a 100644
@@ -150,37 +150,45 @@ union drm_amdgpu_bo_list {
 
 /* context related */
 #define AMDGPU_CTX_OP_ALLOC_CTX	1
 #define AMDGPU_CTX_OP_FREE_CTX	2
 #define AMDGPU_CTX_OP_QUERY_STATE	3
 
 /* GPU reset status */
 #define AMDGPU_CTX_NO_RESET		0
 /* this the context caused it */
 #define AMDGPU_CTX_GUILTY_RESET		1
 /* some other context caused it */
 #define AMDGPU_CTX_INNOCENT_RESET	2
 /* unknown cause */
 #define AMDGPU_CTX_UNKNOWN_RESET	3
 
+/* Context priority level */
+#define AMDGPU_CTX_PRIORITY_LOW_HW      -1023
+#define AMDGPU_CTX_PRIORITY_LOW_SW      -512
+#define AMDGPU_CTX_PRIORITY_NORMAL      0
+/* Selecting a priority above NORMAL requires CAP_SYS_NICE or DRM_MASTER */
+#define AMDGPU_CTX_PRIORITY_HIGH_SW     512
+#define AMDGPU_CTX_PRIORITY_HIGH_HW     1023
+
 struct drm_amdgpu_ctx_in {
 	/** AMDGPU_CTX_OP_* */
 	__u32	op;
 	/** For future use, no flags defined so far */
 	__u32	flags;
 	__u32	ctx_id;
-	__u32	_pad;
+	__s32	priority;
 };
 
 union drm_amdgpu_ctx_out {
 		struct {
 			__u32	ctx_id;
 			__u32	_pad;
 		} alloc;
 
 		struct {
 			/** For future use, no flags defined so far */
 			__u64	flags;
 			/** Number of resets caused by this context so far. */
 			__u32	hangs;
 			/** Reset status since the last call of the ioctl. */
 			__u32	reset_status;
Signature · 6 lines
2.9.3

_______________________________________________
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): 20170608220623.9002-2-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

PATCH 1/1 drm/amdgpu: add parameter to allocate high priority contexts v11

Add a new context creation parameter to express a global context priority.

The priority ranking in descending order is as follows:
* AMDGPU_CTX_PRIORITY_HIGH_HW
* AMDGPU_CTX_PRIORITY_HIGH_SW
* AMDGPU_CTX_PRIORITY_NORMAL
* AMDGPU_CTX_PRIORITY_LOW_SW
* AMDGPU_CTX_PRIORITY_LOW_HW

The driver will attempt to schedule work to the hardware according to the priorities. No latency or throughput guarantees are provided by this patch.

This interface intends to service the EGL_IMG_context_priority extension, and vulkan equivalents.

Setting a priority above NORMAL requires CAP_SYS_NICE or DRM_MASTER.

v2: Instead of using flags, repurpose __pad
v3: Swap enum values of _NORMAL _HIGH for backwards compatibility
v4: Validate usermode priority and store it
v5: Move priority validation into amdgpu_ctx_ioctl(), headline reword
v6: add UAPI note regarding priorities requiring CAP_SYS_ADMIN
v7: remove ctx->priority
v8: added AMDGPU_CTX_PRIORITY_LOW, s/CAP_SYS_ADMIN/CAP_SYS_NICE
v9: change the priority parameter to __s32
v10: split priorities into _SW and _HW
v11: Allow DRM_MASTER without CAP_SYS_NICE

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c       | 61 +++++++++++++++++++++++++--
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  5 ++-
 include/uapi/drm/amdgpu_drm.h                 | 10 ++++-
 3 files changed, 70 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +57 -4
index a11e443..9ec85d5 100644
@@ -11,59 +11,86 @@
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors: monk liu <[email protected]>
  */
 
 #include <drm/drmP.h>
+#include <drm/drm_auth.h>
 #include "amdgpu.h"
 
-static int amdgpu_ctx_init(struct amdgpu_device *adev, struct amdgpu_ctx *ctx)
+static int amdgpu_ctx_priority_permit(struct drm_file *filp,
+				      enum amd_sched_priority priority)
+{
+	/* NORMAL and below are accessible by everyone */
+	if (priority <= AMD_SCHED_PRIORITY_NORMAL)
+		return 0;
+
+	if (capable(CAP_SYS_NICE))
+		return 0;
+
+	if (drm_is_current_master(filp))
+		return 0;
+
+	return -EACCES;
+}
+
+static int amdgpu_ctx_init(struct amdgpu_device *adev,
+			   enum amd_sched_priority priority,
+			   struct drm_file *filp,
+			   struct amdgpu_ctx *ctx)
 {
 	unsigned i, j;
 	int r;
 
+	if (priority < 0 || priority >= AMD_SCHED_PRIORITY_MAX)
+		return -EINVAL;
+
+	r = amdgpu_ctx_priority_permit(filp, priority);
+	if (r)
+		return r;
+
 	memset(ctx, 0, sizeof(*ctx));
 	ctx->adev = adev;
 	kref_init(&ctx->refcount);
 	spin_lock_init(&ctx->ring_lock);
 	ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
 			      sizeof(struct dma_fence*), GFP_KERNEL);
 	if (!ctx->fences)
 		return -ENOMEM;
 
 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
 		ctx->rings[i].sequence = 1;
 		ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
 	}
 
 	ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
 
 	/* create context entity for each ring */
 	for (i = 0; i < adev->num_rings; i++) {
 		struct amdgpu_ring *ring = adev->rings[i];
 		struct amd_sched_rq *rq;
 
-		rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
+		rq = &ring->sched.sched_rq[priority];
 
 		if (ring == &adev->gfx.kiq.ring)
 			continue;
 
 		r = amd_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
 					  rq, amdgpu_sched_jobs);
 		if (r)
 			goto failed;
 	}
 
 	r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
 	if (r)
 		goto failed;
 
 	return 0;
@@ -88,49 +115,52 @@ static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
 		for (j = 0; j < amdgpu_sched_jobs; ++j)
 			dma_fence_put(ctx->rings[i].fences[j]);
 	kfree(ctx->fences);
 	ctx->fences = NULL;
 
 	for (i = 0; i < adev->num_rings; i++)
 		amd_sched_entity_fini(&adev->rings[i]->sched,
 				      &ctx->rings[i].entity);
 
 	amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
 }
 
 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
 			    struct amdgpu_fpriv *fpriv,
+			    struct drm_file *filp,
+			    enum amd_sched_priority priority,
 			    uint32_t *id)
 {
 	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
 	struct amdgpu_ctx *ctx;
 	int r;
 
 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return -ENOMEM;
 
 	mutex_lock(&mgr->lock);
 	r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
 	if (r < 0) {
 		mutex_unlock(&mgr->lock);
 		kfree(ctx);
 		return r;
 	}
+
 	*id = (uint32_t)r;
-	r = amdgpu_ctx_init(adev, ctx);
+	r = amdgpu_ctx_init(adev, priority, filp, ctx);
 	if (r) {
 		idr_remove(&mgr->ctx_handles, *id);
 		*id = 0;
 		kfree(ctx);
 	}
 	mutex_unlock(&mgr->lock);
 	return r;
 }
 
 static void amdgpu_ctx_do_release(struct kref *ref)
 {
 	struct amdgpu_ctx *ctx;
 
 	ctx = container_of(ref, struct amdgpu_ctx, refcount);
 
@@ -176,46 +206,69 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 	out->state.hangs = 0x0;
 
 	/* determine if a GPU reset has occured since the last call */
 	reset_counter = atomic_read(&adev->gpu_reset_counter);
 	/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
 	if (ctx->reset_counter == reset_counter)
 		out->state.reset_status = AMDGPU_CTX_NO_RESET;
 	else
 		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
 	ctx->reset_counter = reset_counter;
 
 	mutex_unlock(&mgr->lock);
 	return 0;
 }
 
+static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
+{
+	switch (amdgpu_priority) {
+	case AMDGPU_CTX_PRIORITY_HIGH_HW:
+		return AMD_SCHED_PRIORITY_HIGH_HW;
+	case AMDGPU_CTX_PRIORITY_HIGH_SW:
+		return AMD_SCHED_PRIORITY_HIGH_SW;
+	case AMDGPU_CTX_PRIORITY_NORMAL:
+		return AMD_SCHED_PRIORITY_NORMAL;
+	case AMDGPU_CTX_PRIORITY_LOW_SW:
+	case AMDGPU_CTX_PRIORITY_LOW_HW:
+		return AMD_SCHED_PRIORITY_LOW;
+	default:
+		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
+		return AMD_SCHED_PRIORITY_NORMAL;
+	}
+}
+
 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 		     struct drm_file *filp)
 {
 	int r;
 	uint32_t id;
+	enum amd_sched_priority priority;
 
 	union drm_amdgpu_ctx *args = data;
 	struct amdgpu_device *adev = dev->dev_private;
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 
 	r = 0;
 	id = args->in.ctx_id;
+	priority = amdgpu_to_sched_priority(args->in.priority);
+
+	if (priority >= AMD_SCHED_PRIORITY_MAX)
+		return -EINVAL;
 
 	switch (args->in.op) {
 	case AMDGPU_CTX_OP_ALLOC_CTX:
-		r = amdgpu_ctx_alloc(adev, fpriv, &id);
+		r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
 		args->out.alloc.ctx_id = id;
 		break;
 	case AMDGPU_CTX_OP_FREE_CTX:
 		r = amdgpu_ctx_free(fpriv, id);
 		break;
 	case AMDGPU_CTX_OP_QUERY_STATE:
 		r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
 		break;
 	default:
 		return -EINVAL;
 	}
 
 	return r;
 }
 
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +4 -1
index f9d8f28..38e622c 100644
@@ -103,31 +103,34 @@ static inline bool amd_sched_invalidate_job(struct amd_sched_job *s_job, int thr
 }
 
 /**
  * Define the backend operations called by the scheduler,
  * these functions should be implemented in driver side
 */
 struct amd_sched_backend_ops {
 	struct dma_fence *(*dependency)(struct amd_sched_job *sched_job);
 	struct dma_fence *(*run_job)(struct amd_sched_job *sched_job);
 	void (*timedout_job)(struct amd_sched_job *sched_job);
 	void (*free_job)(struct amd_sched_job *sched_job);
 };
 
 enum amd_sched_priority {
 	AMD_SCHED_PRIORITY_MIN,
-	AMD_SCHED_PRIORITY_NORMAL = AMD_SCHED_PRIORITY_MIN,
+	AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
+	AMD_SCHED_PRIORITY_NORMAL,
+	AMD_SCHED_PRIORITY_HIGH_SW,
+	AMD_SCHED_PRIORITY_HIGH_HW,
 	AMD_SCHED_PRIORITY_KERNEL,
 	AMD_SCHED_PRIORITY_MAX
 };
 
 /**
  * One scheduler is implemented for each hardware ring
 */
 struct amd_gpu_scheduler {
 	const struct amd_sched_backend_ops	*ops;
 	uint32_t			hw_submission_limit;
 	long				timeout;
 	const char			*name;
 	struct amd_sched_rq		sched_rq[AMD_SCHED_PRIORITY_MAX];
 	wait_queue_head_t		wake_up_worker;
 	wait_queue_head_t		job_scheduled;
include/uapi/drm/amdgpu_drm.h +9 -1
index 34128f6..590fc2a 100644
@@ -150,37 +150,45 @@ union drm_amdgpu_bo_list {
 
 /* context related */
 #define AMDGPU_CTX_OP_ALLOC_CTX	1
 #define AMDGPU_CTX_OP_FREE_CTX	2
 #define AMDGPU_CTX_OP_QUERY_STATE	3
 
 /* GPU reset status */
 #define AMDGPU_CTX_NO_RESET		0
 /* this the context caused it */
 #define AMDGPU_CTX_GUILTY_RESET		1
 /* some other context caused it */
 #define AMDGPU_CTX_INNOCENT_RESET	2
 /* unknown cause */
 #define AMDGPU_CTX_UNKNOWN_RESET	3
 
+/* Context priority level */
+#define AMDGPU_CTX_PRIORITY_LOW_HW      -1023
+#define AMDGPU_CTX_PRIORITY_LOW_SW      -512
+#define AMDGPU_CTX_PRIORITY_NORMAL      0
+/* Selecting a priority above NORMAL requires CAP_SYS_NICE or DRM_MASTER */
+#define AMDGPU_CTX_PRIORITY_HIGH_SW     512
+#define AMDGPU_CTX_PRIORITY_HIGH_HW     1023
+
 struct drm_amdgpu_ctx_in {
 	/** AMDGPU_CTX_OP_* */
 	__u32	op;
 	/** For future use, no flags defined so far */
 	__u32	flags;
 	__u32	ctx_id;
-	__u32	_pad;
+	__s32	priority;
 };
 
 union drm_amdgpu_ctx_out {
 		struct {
 			__u32	ctx_id;
 			__u32	_pad;
 		} alloc;
 
 		struct {
 			/** For future use, no flags defined so far */
 			__u64	flags;
 			/** Number of resets caused by this context so far. */
 			__u32	hangs;
 			/** Reset status since the last call of the ioctl. */
 			__u32	reset_status;
Signature · 6 lines
2.9.3

_______________________________________________
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): 20170608222005.17084-1-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#6 lore raw

Re: [PATCH] drm/amdgpu: add parameter to allocate high priority contexts v11

I had forgotten to squash some fixups to the original patch.

This should be the correct one.

Regards,
Andres

Quoted 337 lines · Andres Rodriguez

Add a new context creation parameter to express a global context priority.

The priority ranking in descending order is as follows:
* AMDGPU_CTX_PRIORITY_HIGH_HW
* AMDGPU_CTX_PRIORITY_HIGH_SW
* AMDGPU_CTX_PRIORITY_NORMAL
* AMDGPU_CTX_PRIORITY_LOW_SW
* AMDGPU_CTX_PRIORITY_LOW_HW

The driver will attempt to schedule work to the hardware according to the priorities. No latency or throughput guarantees are provided by this patch.

This interface intends to service the EGL_IMG_context_priority extension, and vulkan equivalents.

Setting a priority above NORMAL requires CAP_SYS_NICE or DRM_MASTER.

v2: Instead of using flags, repurpose __pad
v3: Swap enum values of _NORMAL _HIGH for backwards compatibility
v4: Validate usermode priority and store it
v5: Move priority validation into amdgpu_ctx_ioctl(), headline reword
v6: add UAPI note regarding priorities requiring CAP_SYS_ADMIN
v7: remove ctx->priority
v8: added AMDGPU_CTX_PRIORITY_LOW, s/CAP_SYS_ADMIN/CAP_SYS_NICE
v9: change the priority parameter to __s32
v10: split priorities into _SW and _HW
v11: Allow DRM_MASTER without CAP_SYS_NICE

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c       | 61 +++++++++++++++++++++++++--
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  5 ++-
  include/uapi/drm/amdgpu_drm.h                 | 10 ++++-
  3 files changed, 70 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +57 -4
index a11e443..9ec85d5 100644
@@ -11,59 +11,86 @@
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   * OTHER DEALINGS IN THE SOFTWARE.
   *
   * Authors: monk liu <[email protected]>
   */
 
  #include <drm/drmP.h>
+#include <drm/drm_auth.h>
  #include "amdgpu.h"
 
-static int amdgpu_ctx_init(struct amdgpu_device *adev, struct amdgpu_ctx *ctx)
+static int amdgpu_ctx_priority_permit(struct drm_file *filp,
+				      enum amd_sched_priority priority)
+{
+	/* NORMAL and below are accessible by everyone */
+	if (priority <= AMD_SCHED_PRIORITY_NORMAL)
+		return 0;
+
+	if (capable(CAP_SYS_NICE))
+		return 0;
+
+	if (drm_is_current_master(filp))
+		return 0;
+
+	return -EACCES;
+}
+
+static int amdgpu_ctx_init(struct amdgpu_device *adev,
+			   enum amd_sched_priority priority,
+			   struct drm_file *filp,
+			   struct amdgpu_ctx *ctx)
  {
  	unsigned i, j;
  	int r;
 
+	if (priority < 0 || priority >= AMD_SCHED_PRIORITY_MAX)
+		return -EINVAL;
+
+	r = amdgpu_ctx_priority_permit(filp, priority);
+	if (r)
+		return r;
+
  	memset(ctx, 0, sizeof(*ctx));
  	ctx->adev = adev;
  	kref_init(&ctx->refcount);
  	spin_lock_init(&ctx->ring_lock);
  	ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
  			      sizeof(struct dma_fence*), GFP_KERNEL);
  	if (!ctx->fences)
  		return -ENOMEM;
 
  	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  		ctx->rings[i].sequence = 1;
  		ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
  	}
 
  	ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
 
  	/* create context entity for each ring */
  	for (i = 0; i < adev->num_rings; i++) {
  		struct amdgpu_ring *ring = adev->rings[i];
  		struct amd_sched_rq *rq;
 
-		rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
+		rq = &ring->sched.sched_rq[priority];
 
  		if (ring == &adev->gfx.kiq.ring)
  			continue;
 
  		r = amd_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
  					  rq, amdgpu_sched_jobs);
  		if (r)
  			goto failed;
  	}
 
  	r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
  	if (r)
  		goto failed;
 
  	return 0;
@@ -88,49 +115,52 @@ static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
  	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  		for (j = 0; j < amdgpu_sched_jobs; ++j)
  			dma_fence_put(ctx->rings[i].fences[j]);
  	kfree(ctx->fences);
  	ctx->fences = NULL;
 
  	for (i = 0; i < adev->num_rings; i++)
  		amd_sched_entity_fini(&adev->rings[i]->sched,
  				      &ctx->rings[i].entity);
 
  	amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
  }
 
  static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
  			    struct amdgpu_fpriv *fpriv,
+			    struct drm_file *filp,
+			    enum amd_sched_priority priority,
  			    uint32_t *id)
  {
  	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  	struct amdgpu_ctx *ctx;
  	int r;
 
  	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  	if (!ctx)
  		return -ENOMEM;
 
  	mutex_lock(&mgr->lock);
  	r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
  	if (r < 0) {
  		mutex_unlock(&mgr->lock);
  		kfree(ctx);
  		return r;
  	}
+
  	*id = (uint32_t)r;
-	r = amdgpu_ctx_init(adev, ctx);
+	r = amdgpu_ctx_init(adev, priority, filp, ctx);
  	if (r) {
  		idr_remove(&mgr->ctx_handles, *id);
  		*id = 0;
  		kfree(ctx);
  	}
  	mutex_unlock(&mgr->lock);
  	return r;
  }
 
  static void amdgpu_ctx_do_release(struct kref *ref)
  {
  	struct amdgpu_ctx *ctx;
 
  	ctx = container_of(ref, struct amdgpu_ctx, refcount);
 
@@ -176,46 +206,69 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
  	out->state.hangs = 0x0;
 
  	/* determine if a GPU reset has occured since the last call */
  	reset_counter = atomic_read(&adev->gpu_reset_counter);
  	/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  	if (ctx->reset_counter == reset_counter)
  		out->state.reset_status = AMDGPU_CTX_NO_RESET;
  	else
  		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  	ctx->reset_counter = reset_counter;
 
  	mutex_unlock(&mgr->lock);
  	return 0;
  }
 
+static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
+{
+	switch (amdgpu_priority) {
+	case AMDGPU_CTX_PRIORITY_HIGH_HW:
+		return AMD_SCHED_PRIORITY_HIGH_HW;
+	case AMDGPU_CTX_PRIORITY_HIGH_SW:
+		return AMD_SCHED_PRIORITY_HIGH_SW;
+	case AMDGPU_CTX_PRIORITY_NORMAL:
+		return AMD_SCHED_PRIORITY_NORMAL;
+	case AMDGPU_CTX_PRIORITY_LOW_SW:
+	case AMDGPU_CTX_PRIORITY_LOW_HW:
+		return AMD_SCHED_PRIORITY_LOW;
+	default:
+		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
+		return AMD_SCHED_PRIORITY_NORMAL;
+	}
+}
+
  int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  		     struct drm_file *filp)
  {
  	int r;
  	uint32_t id;
+	enum amd_sched_priority priority;
 
  	union drm_amdgpu_ctx *args = data;
  	struct amdgpu_device *adev = dev->dev_private;
  	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 
  	r = 0;
  	id = args->in.ctx_id;
+	priority = amdgpu_to_sched_priority(args->in.priority);
+
+	if (priority >= AMD_SCHED_PRIORITY_MAX)
+		return -EINVAL;
 
  	switch (args->in.op) {
  	case AMDGPU_CTX_OP_ALLOC_CTX:
-		r = amdgpu_ctx_alloc(adev, fpriv, &id);
+		r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
  		args->out.alloc.ctx_id = id;
  		break;
  	case AMDGPU_CTX_OP_FREE_CTX:
  		r = amdgpu_ctx_free(fpriv, id);
  		break;
  	case AMDGPU_CTX_OP_QUERY_STATE:
  		r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  		break;
  	default:
  		return -EINVAL;
  	}
 
  	return r;
  }
 
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +4 -1
index f9d8f28..38e622c 100644
@@ -103,31 +103,34 @@ static inline bool amd_sched_invalidate_job(struct amd_sched_job *s_job, int thr
  }
 
  /**
   * Define the backend operations called by the scheduler,
   * these functions should be implemented in driver side
  */
  struct amd_sched_backend_ops {
  	struct dma_fence *(*dependency)(struct amd_sched_job *sched_job);
  	struct dma_fence *(*run_job)(struct amd_sched_job *sched_job);
  	void (*timedout_job)(struct amd_sched_job *sched_job);
  	void (*free_job)(struct amd_sched_job *sched_job);
  };
 
  enum amd_sched_priority {
  	AMD_SCHED_PRIORITY_MIN,
-	AMD_SCHED_PRIORITY_NORMAL = AMD_SCHED_PRIORITY_MIN,
+	AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
+	AMD_SCHED_PRIORITY_NORMAL,
+	AMD_SCHED_PRIORITY_HIGH_SW,
+	AMD_SCHED_PRIORITY_HIGH_HW,
  	AMD_SCHED_PRIORITY_KERNEL,
  	AMD_SCHED_PRIORITY_MAX
  };
 
  /**
   * One scheduler is implemented for each hardware ring
  */
  struct amd_gpu_scheduler {
  	const struct amd_sched_backend_ops	*ops;
  	uint32_t			hw_submission_limit;
  	long				timeout;
  	const char			*name;
  	struct amd_sched_rq		sched_rq[AMD_SCHED_PRIORITY_MAX];
  	wait_queue_head_t		wake_up_worker;
  	wait_queue_head_t		job_scheduled;
include/uapi/drm/amdgpu_drm.h +9 -1
index 34128f6..590fc2a 100644
@@ -150,37 +150,45 @@ union drm_amdgpu_bo_list {
 
  /* context related */
  #define AMDGPU_CTX_OP_ALLOC_CTX	1
  #define AMDGPU_CTX_OP_FREE_CTX	2
  #define AMDGPU_CTX_OP_QUERY_STATE	3
 
  /* GPU reset status */
  #define AMDGPU_CTX_NO_RESET		0
  /* this the context caused it */
  #define AMDGPU_CTX_GUILTY_RESET		1
  /* some other context caused it */
  #define AMDGPU_CTX_INNOCENT_RESET	2
  /* unknown cause */
  #define AMDGPU_CTX_UNKNOWN_RESET	3
 
+/* Context priority level */
+#define AMDGPU_CTX_PRIORITY_LOW_HW      -1023
+#define AMDGPU_CTX_PRIORITY_LOW_SW      -512
+#define AMDGPU_CTX_PRIORITY_NORMAL      0
+/* Selecting a priority above NORMAL requires CAP_SYS_NICE or DRM_MASTER */
+#define AMDGPU_CTX_PRIORITY_HIGH_SW     512
+#define AMDGPU_CTX_PRIORITY_HIGH_HW     1023
+
  struct drm_amdgpu_ctx_in {
  	/** AMDGPU_CTX_OP_* */
  	__u32	op;
  	/** For future use, no flags defined so far */
  	__u32	flags;
  	__u32	ctx_id;
-	__u32	_pad;
+	__s32	priority;
  };
 
  union drm_amdgpu_ctx_out {
  		struct {
  			__u32	ctx_id;

include/uapi/drm/amdgpu_drm.h union drm_amdgpu_bo_list { · line 194

Quoted 10 lines
include/uapi/drm/amdgpu_drm.h +0 -0
  			__u32	_pad;
  		} alloc;
 
  		struct {
  			/** For future use, no flags defined so far */
  			__u64	flags;
  			/** Number of resets caused by this context so far. */
  			__u32	hangs;
  			/** Reset status since the last call of the ioctl. */
  			__u32	reset_status;

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

#7 lore raw

PATCH 2/8 drm/amdgpu: add framework for HW specific priority settings v8

In reply to a message not in the archive

Add an initial framework for changing the HW priorities of rings. The framework allows requesting priority changes for the lifetime of an amdgpu_job. After the job completes the priority will decay to the next lowest priority for which a request is still valid.

A new ring function set_priority() can now be populated to take care of the HW specific programming sequence for priority changes.

v2: set priority before emitting IB, and take a ref on amdgpu_job
v3: use AMD_SCHED_PRIORITY_* instead of AMDGPU_CTX_PRIORITY_*
v4: plug amdgpu_ring_restore_priority_cb into amdgpu_job_free_cb
v5: use atomic for tracking job priorities instead of last_job
v6: rename amdgpu_ring_priority_[get/put]() and align parameters
v7: replace spinlocks with mutexes for KIQ compatibility
v8: raise ring priority during cs_ioctl, instead of job_run

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c       |  4 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c      | 76 ++++++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h      | 15 ++++++
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  7 +++
 5 files changed, 103 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -0
index b17635c..d84d026 100644
@@ -996,30 +996,32 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	if (r) {
 		amdgpu_job_free(job);
 		return r;
 	}
 
 	job->owner = p->filp;
 	job->fence_ctx = entity->fence_context;
 	p->fence = dma_fence_get(&job->base.s_fence->finished);
 	cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
 	job->uf_sequence = cs->out.handle;
 	amdgpu_job_free_resources(job);
 	amdgpu_cs_parser_fini(p, 0, true);
 
 	trace_amdgpu_cs_ioctl(job);
 	amd_sched_entity_push_job(&job->base);
+	amdgpu_ring_priority_get(job->ring,
+				 amd_sched_get_job_priority(&job->base));
 
 	return 0;
 }
 
 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 {
 	struct amdgpu_device *adev = dev->dev_private;
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 	union drm_amdgpu_cs *cs = data;
 	struct amdgpu_cs_parser parser = {};
 	bool reserved_buffers = false;
 	int i, r;
 
 	if (!adev->accel_working)
 		return -EBUSY;
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +4 -0
index 3d641e1..f10ce0d 100644
@@ -89,30 +89,31 @@ void amdgpu_job_free_resources(struct amdgpu_job *job)
 {
 	struct dma_fence *f;
 	unsigned i;
 
 	/* use sched fence if available */
 	f = job->base.s_fence ? &job->base.s_fence->finished : job->fence;
 
 	for (i = 0; i < job->num_ibs; ++i)
 		amdgpu_ib_free(job->adev, &job->ibs[i], f);
 }
 
 static void amdgpu_job_free_cb(struct amd_sched_job *s_job)
 {
 	struct amdgpu_job *job = container_of(s_job, struct amdgpu_job, base);
 
+	amdgpu_ring_priority_put(job->ring, amd_sched_get_job_priority(s_job));
 	dma_fence_put(job->fence);
 	amdgpu_sync_free(&job->sync);
 	amdgpu_sync_free(&job->dep_sync);
 	amdgpu_sync_free(&job->sched_sync);
 	kfree(job);
 }
 
 void amdgpu_job_free(struct amdgpu_job *job)
 {
 	amdgpu_job_free_resources(job);
 
 	dma_fence_put(job->fence);
 	amdgpu_sync_free(&job->sync);
 	amdgpu_sync_free(&job->dep_sync);
 	amdgpu_sync_free(&job->sched_sync);
@@ -126,30 +127,32 @@ int amdgpu_job_submit(struct amdgpu_job *job, struct amdgpu_ring *ring,
 	int r;
 	job->ring = ring;
 
 	if (!f)
 		return -EINVAL;
 
 	r = amd_sched_job_init(&job->base, &ring->sched, entity, owner);
 	if (r)
 		return r;
 
 	job->owner = owner;
 	job->fence_ctx = entity->fence_context;
 	*f = dma_fence_get(&job->base.s_fence->finished);
 	amdgpu_job_free_resources(job);
 	amd_sched_entity_push_job(&job->base);
+	amdgpu_ring_priority_get(job->ring,
+				 amd_sched_get_job_priority(&job->base));
 
 	return 0;
 }
 
 static struct dma_fence *amdgpu_job_dependency(struct amd_sched_job *sched_job)
 {
 	struct amdgpu_job *job = to_amdgpu_job(sched_job);
 	struct amdgpu_vm *vm = job->vm;
 
 	struct dma_fence *fence = amdgpu_sync_get_fence(&job->dep_sync);
 	int r;
 
 	if (amd_sched_dependency_optimized(fence, sched_job->s_entity)) {
 		r = amdgpu_sync_fence(job->adev, &job->sched_sync, fence);
 		if (r)
@@ -189,25 +192,26 @@ static struct dma_fence *amdgpu_job_run(struct amd_sched_job *sched_job)
 
 	trace_amdgpu_sched_run_job(job);
 	if (job->vm)
 		fpriv = container_of(job->vm, struct amdgpu_fpriv, vm);
 	/* skip ib schedule when vram is lost */
 	if (fpriv && amdgpu_kms_vram_lost(job->adev, fpriv))
 		DRM_ERROR("Skip scheduling IBs!\n");
 	else {
 		r = amdgpu_ib_schedule(job->ring, job->num_ibs, job->ibs, job, &fence);
 		if (r)
 			DRM_ERROR("Error scheduling IBs (%d)\n", r);
 	}
 	/* if gpu reset, hw fence will be replaced here */
 	dma_fence_put(job->fence);
 	job->fence = dma_fence_get(fence);
+
 	amdgpu_job_free_resources(job);
 	return fence;
 }
 
 const struct amd_sched_backend_ops amdgpu_sched_ops = {
 	.dependency = amdgpu_job_dependency,
 	.run_job = amdgpu_job_run,
 	.timedout_job = amdgpu_job_timedout,
 	.free_job = amdgpu_job_free_cb
 };
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +75 -1

153 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +15 -0
index bc8dec9..7348769 100644
@@ -12,30 +12,31 @@
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors: Christian König
  */
 #ifndef __AMDGPU_RING_H__
 #define __AMDGPU_RING_H__
 
+#include <drm/amdgpu_drm.h>
 #include "gpu_scheduler.h"
 
 /* max number of rings */
 #define AMDGPU_MAX_RINGS		18
 #define AMDGPU_MAX_GFX_RINGS		1
 #define AMDGPU_MAX_COMPUTE_RINGS	8
 #define AMDGPU_MAX_VCE_RINGS		3
 #define AMDGPU_MAX_UVD_ENC_RINGS	2
 
 /* some special values for the owner field */
 #define AMDGPU_FENCE_OWNER_UNDEFINED	((void*)0ul)
 #define AMDGPU_FENCE_OWNER_VM		((void*)1ul)
 
 #define AMDGPU_FENCE_FLAG_64BIT         (1 << 0)
 #define AMDGPU_FENCE_FLAG_INT           (1 << 1)
@@ -44,30 +45,31 @@ enum amdgpu_ring_type {
 	AMDGPU_RING_TYPE_GFX,
 	AMDGPU_RING_TYPE_COMPUTE,
 	AMDGPU_RING_TYPE_SDMA,
 	AMDGPU_RING_TYPE_UVD,
 	AMDGPU_RING_TYPE_VCE,
 	AMDGPU_RING_TYPE_KIQ,
 	AMDGPU_RING_TYPE_UVD_ENC,
 	AMDGPU_RING_TYPE_VCN_DEC,
 	AMDGPU_RING_TYPE_VCN_ENC
 };
 
 struct amdgpu_device;
 struct amdgpu_ring;
 struct amdgpu_ib;
 struct amdgpu_cs_parser;
+struct amdgpu_job;
 
 /*
  * Fences.
  */
 struct amdgpu_fence_driver {
 	uint64_t			gpu_addr;
 	volatile uint32_t		*cpu_addr;
 	/* sync_seq is protected by ring emission lock */
 	uint32_t			sync_seq;
 	atomic_t			last_seq;
 	bool				initialized;
 	struct amdgpu_irq_src		*irq_src;
 	unsigned			irq_type;
 	struct timer_list		fallback_timer;
 	unsigned			num_fences_mask;
@@ -135,30 +137,33 @@ struct amdgpu_ring_funcs {
 	void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
 	void (*insert_start)(struct amdgpu_ring *ring);
 	void (*insert_end)(struct amdgpu_ring *ring);
 	/* pad the indirect buffer to the necessary number of dw */
 	void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
 	unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
 	void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
 	/* note usage for clock and power gating */
 	void (*begin_use)(struct amdgpu_ring *ring);
 	void (*end_use)(struct amdgpu_ring *ring);
 	void (*emit_switch_buffer) (struct amdgpu_ring *ring);
 	void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
 	void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
 	void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
 	void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
+	/* priority functions */
+	void (*set_priority) (struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
 };
 
 struct amdgpu_ring {
 	struct amdgpu_device		*adev;
 	const struct amdgpu_ring_funcs	*funcs;
 	struct amdgpu_fence_driver	fence_drv;
 	struct amd_gpu_scheduler	sched;
 	struct list_head		lru_list;
 
 	struct amdgpu_bo	*ring_obj;
 	volatile uint32_t	*ring;
 	unsigned		rptr_offs;
 	u64			wptr;
 	u64			wptr_old;
 	unsigned		ring_size;
@@ -175,40 +180,50 @@ struct amdgpu_ring {
 	struct amdgpu_bo	*mqd_obj;
 	uint64_t                mqd_gpu_addr;
 	void                    *mqd_ptr;
 	uint64_t                eop_gpu_addr;
 	u32			doorbell_index;
 	bool			use_doorbell;
 	unsigned		wptr_offs;
 	unsigned		fence_offs;
 	uint64_t		current_ctx;
 	char			name[16];
 	unsigned		cond_exe_offs;
 	u64			cond_exe_gpu_addr;
 	volatile u32		*cond_exe_cpu_addr;
 	unsigned		vm_inv_eng;
 	bool			has_compute_vm_bug;
+
+	atomic_t		num_jobs[AMD_SCHED_PRIORITY_MAX];
+	struct mutex		priority_mutex;
+	/* protected by priority_mutex */
+	int			priority;
+
 #if defined(CONFIG_DEBUG_FS)
 	struct dentry *ent;
 #endif
 };
 
 int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
 void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
 void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
 void amdgpu_ring_commit(struct amdgpu_ring *ring);
 void amdgpu_ring_undo(struct amdgpu_ring *ring);
+void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
+void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
 int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 		     unsigned ring_size, struct amdgpu_irq_src *irq_src,
 		     unsigned irq_type);
 void amdgpu_ring_fini(struct amdgpu_ring *ring);
 int amdgpu_ring_lru_get(struct amdgpu_device *adev, int type, int *blacklist,
 			int num_blacklist, struct amdgpu_ring **ring);
 void amdgpu_ring_lru_touch(struct amdgpu_device *adev, struct amdgpu_ring *ring);
 static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
 {
 	int i = 0;
 	while (i <= ring->buf_mask)
 		ring->ring[i++] = ring->funcs->nop;
 
 }
 
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +7 -0
index 38e622c..dbcaa2e 100644
@@ -158,16 +158,23 @@ int amd_sched_fence_slab_init(void);
 void amd_sched_fence_slab_fini(void);
 
 struct amd_sched_fence *amd_sched_fence_create(
 	struct amd_sched_entity *s_entity, void *owner);
 void amd_sched_fence_scheduled(struct amd_sched_fence *fence);
 void amd_sched_fence_finished(struct amd_sched_fence *fence);
 int amd_sched_job_init(struct amd_sched_job *job,
 		       struct amd_gpu_scheduler *sched,
 		       struct amd_sched_entity *entity,
 		       void *owner);
 void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
 void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
 bool amd_sched_dependency_optimized(struct dma_fence* fence,
 				    struct amd_sched_entity *entity);
 void amd_sched_job_kickout(struct amd_sched_job *s_job);
+
+static inline enum amd_sched_priority
+amd_sched_get_job_priority(struct amd_sched_job *job)
+{
+	return (job->s_entity->rq - job->sched->sched_rq);
+}
+
 #endif
Signature · 6 lines
2.9.3

_______________________________________________
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): 20170608220623.9002-3-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#9 lore raw

Re: [PATCH 2/8] drm/amdgpu: add framework for HW specific priority settings v8

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 37 lines

Add an initial framework for changing the HW priorities of rings. The framework allows requesting priority changes for the lifetime of an amdgpu_job. After the job completes the priority will decay to the next lowest priority for which a request is still valid.

A new ring function set_priority() can now be populated to take care of the HW specific programming sequence for priority changes.

v2: set priority before emitting IB, and take a ref on amdgpu_job
v3: use AMD_SCHED_PRIORITY_* instead of AMDGPU_CTX_PRIORITY_*
v4: plug amdgpu_ring_restore_priority_cb into amdgpu_job_free_cb
v5: use atomic for tracking job priorities instead of last_job
v6: rename amdgpu_ring_priority_[get/put]() and align parameters
v7: replace spinlocks with mutexes for KIQ compatibility
v8: raise ring priority during cs_ioctl, instead of job_run

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_job.c       |  4 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c      | 76 ++++++++++++++++++++++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h      | 15 ++++++
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  7 +++
  5 files changed, 103 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
index b17635c..d84d026 100644
@@ -996,30 +996,32 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  	if (r) {
  		amdgpu_job_free(job);
  		return r;
  	}
 
  	job->owner = p->filp;
  	job->fence_ctx = entity->fence_context;

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1012

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -0
  	p->fence = dma_fence_get(&job->base.s_fence->finished);
  	cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  	job->uf_sequence = cs->out.handle;
  	amdgpu_job_free_resources(job);
  	amdgpu_cs_parser_fini(p, 0, true);
 
  	trace_amdgpu_cs_ioctl(job);
  	amd_sched_entity_push_job(&job->base);
+	amdgpu_ring_priority_get(job->ring,
+				 amd_sched_get_job_priority(&job->base));

After the call to amd_sched_entity_push_job() we no longer own the pointer, so the job could be already be freed when you dereference it.

I suggest to just move the call to amdgpu_ring_priority_get() before the one to amd_sched_entity_push_job(), cause the whole thing is racy in the first place.

Christian.

Quoted 441 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
  	return 0;
  }
 
  int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  {
  	struct amdgpu_device *adev = dev->dev_private;
  	struct amdgpu_fpriv *fpriv = filp->driver_priv;
  	union drm_amdgpu_cs *cs = data;
  	struct amdgpu_cs_parser parser = {};
  	bool reserved_buffers = false;
  	int i, r;
 
  	if (!adev->accel_working)
  		return -EBUSY;
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +4 -0
index 3d641e1..f10ce0d 100644
@@ -89,30 +89,31 @@ void amdgpu_job_free_resources(struct amdgpu_job *job)
  {
  	struct dma_fence *f;
  	unsigned i;
 
  	/* use sched fence if available */
  	f = job->base.s_fence ? &job->base.s_fence->finished : job->fence;
 
  	for (i = 0; i < job->num_ibs; ++i)
  		amdgpu_ib_free(job->adev, &job->ibs[i], f);
  }
 
  static void amdgpu_job_free_cb(struct amd_sched_job *s_job)
  {
  	struct amdgpu_job *job = container_of(s_job, struct amdgpu_job, base);
 
+	amdgpu_ring_priority_put(job->ring, amd_sched_get_job_priority(s_job));
  	dma_fence_put(job->fence);
  	amdgpu_sync_free(&job->sync);
  	amdgpu_sync_free(&job->dep_sync);
  	amdgpu_sync_free(&job->sched_sync);
  	kfree(job);
  }
 
  void amdgpu_job_free(struct amdgpu_job *job)
  {
  	amdgpu_job_free_resources(job);
 
  	dma_fence_put(job->fence);
  	amdgpu_sync_free(&job->sync);
  	amdgpu_sync_free(&job->dep_sync);
  	amdgpu_sync_free(&job->sched_sync);
@@ -126,30 +127,32 @@ int amdgpu_job_submit(struct amdgpu_job *job, struct amdgpu_ring *ring,
  	int r;
  	job->ring = ring;
 
  	if (!f)
  		return -EINVAL;
 
  	r = amd_sched_job_init(&job->base, &ring->sched, entity, owner);
  	if (r)
  		return r;
 
  	job->owner = owner;
  	job->fence_ctx = entity->fence_context;
  	*f = dma_fence_get(&job->base.s_fence->finished);
  	amdgpu_job_free_resources(job);
  	amd_sched_entity_push_job(&job->base);
+	amdgpu_ring_priority_get(job->ring,
+				 amd_sched_get_job_priority(&job->base));
 
  	return 0;
  }
 
  static struct dma_fence *amdgpu_job_dependency(struct amd_sched_job *sched_job)
  {
  	struct amdgpu_job *job = to_amdgpu_job(sched_job);
  	struct amdgpu_vm *vm = job->vm;
 
  	struct dma_fence *fence = amdgpu_sync_get_fence(&job->dep_sync);
  	int r;
 
  	if (amd_sched_dependency_optimized(fence, sched_job->s_entity)) {
  		r = amdgpu_sync_fence(job->adev, &job->sched_sync, fence);
  		if (r)
@@ -189,25 +192,26 @@ static struct dma_fence *amdgpu_job_run(struct amd_sched_job *sched_job)
 
  	trace_amdgpu_sched_run_job(job);
  	if (job->vm)
  		fpriv = container_of(job->vm, struct amdgpu_fpriv, vm);
  	/* skip ib schedule when vram is lost */
  	if (fpriv && amdgpu_kms_vram_lost(job->adev, fpriv))
  		DRM_ERROR("Skip scheduling IBs!\n");
  	else {
  		r = amdgpu_ib_schedule(job->ring, job->num_ibs, job->ibs, job, &fence);
  		if (r)
  			DRM_ERROR("Error scheduling IBs (%d)\n", r);
  	}
  	/* if gpu reset, hw fence will be replaced here */
  	dma_fence_put(job->fence);
  	job->fence = dma_fence_get(fence);
+
  	amdgpu_job_free_resources(job);
  	return fence;
  }
 
  const struct amd_sched_backend_ops amdgpu_sched_ops = {
  	.dependency = amdgpu_job_dependency,
  	.run_job = amdgpu_job_run,
  	.timedout_job = amdgpu_job_timedout,
  	.free_job = amdgpu_job_free_cb
  };
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +75 -1
index 75165e0..2d8b20a7 100644
@@ -143,45 +143,114 @@ void amdgpu_ring_commit(struct amdgpu_ring *ring)
   * amdgpu_ring_undo - reset the wptr
   *
   * @ring: amdgpu_ring structure holding ring information
   *
   * Reset the driver's copy of the wptr (all asics).
   */
  void amdgpu_ring_undo(struct amdgpu_ring *ring)
  {
  	ring->wptr = ring->wptr_old;
 
  	if (ring->funcs->end_use)
  		ring->funcs->end_use(ring);
  }
 
  /**
+ * amdgpu_ring_priority_put - restore a ring's priority
+ *
+ * @ring: amdgpu_ring structure holding the information
+ * @priority: target priority
+ *
+ * Release a request for executing at @priority
+ */
+void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority)
+{
+	int i;
+
+	if (!ring->funcs->set_priority)
+		return;
+
+	if (atomic_dec_return(&ring->num_jobs[priority]) > 0)
+		return;
+
+	/* no need to restore if the job is already at the lowest priority */
+	if (priority == AMD_SCHED_PRIORITY_NORMAL)
+		return;
+
+	mutex_lock(&ring->priority_mutex);
+	/* something higher prio is executing, no need to decay */
+	if (ring->priority > priority)
+		goto out_unlock;
+
+	/* decay priority to the next level with a job available */
+	for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
+		if (i == AMD_SCHED_PRIORITY_NORMAL
+				|| atomic_read(&ring->num_jobs[i])) {
+			ring->priority = i;
+			ring->funcs->set_priority(ring, i);
+			break;
+		}
+	}
+
+out_unlock:
+	mutex_unlock(&ring->priority_mutex);
+}
+
+/**
+ * amdgpu_ring_priority_get - change the ring's priority
+ *
+ * @ring: amdgpu_ring structure holding the information
+ * @priority: target priority
+ *
+ * Request a ring's priority to be raised to @priority (refcounted).
+ */
+void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority)
+{
+	if (!ring->funcs->set_priority)
+		return;
+
+	atomic_inc(&ring->num_jobs[priority]);
+
+	mutex_lock(&ring->priority_mutex);
+	if (priority <= ring->priority)
+		goto out_unlock;
+
+	ring->priority = priority;
+	ring->funcs->set_priority(ring, priority);
+
+out_unlock:
+	mutex_unlock(&ring->priority_mutex);
+}
+
+/**
   * amdgpu_ring_init - init driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   * @max_ndw: maximum number of dw for ring alloc
   * @nop: nop packet for this ring
   *
   * Initialize the driver information for the selected ring (all asics).
   * Returns 0 on success, error on failure.
   */
  int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  		     unsigned max_dw, struct amdgpu_irq_src *irq_src,
  		     unsigned irq_type)
  {
-	int r;
+	int r, i;
 
  	if (ring->adev == NULL) {
  		if (adev->num_rings >= AMDGPU_MAX_RINGS)
  			return -EINVAL;
 
  		ring->adev = adev;
  		ring->idx = adev->num_rings++;
  		adev->rings[ring->idx] = ring;
  		r = amdgpu_fence_driver_init_ring(ring,
  			amdgpu_sched_hw_submission);
  		if (r)
  			return r;
  	}
 
  	if (ring->funcs->support_64bit_ptrs) {
@@ -243,33 +312,38 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  	/* Allocate ring buffer */
  	if (ring->ring_obj == NULL) {
  		r = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
  					    AMDGPU_GEM_DOMAIN_GTT,
  					    &ring->ring_obj,
  					    &ring->gpu_addr,
  					    (void **)&ring->ring);
  		if (r) {
  			dev_err(adev->dev, "(%d) ring create failed\n", r);
  			return r;
  		}
  		amdgpu_ring_clear_ring(ring);
  	}
 
  	ring->max_dw = max_dw;
+	ring->priority = AMD_SCHED_PRIORITY_NORMAL;
+	mutex_init(&ring->priority_mutex);
  	INIT_LIST_HEAD(&ring->lru_list);
  	amdgpu_ring_lru_touch(adev, ring);
 
+	for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
+		atomic_set(&ring->num_jobs[i], 0);
+
  	if (amdgpu_debugfs_ring_init(adev, ring)) {
  		DRM_ERROR("Failed to register debugfs file for rings !\n");
  	}
 
  	return 0;
  }
 
  /**
   * amdgpu_ring_fini - tear down the driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   *
   * Tear down the driver information for the selected ring (all asics).
   */
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +15 -0
index bc8dec9..7348769 100644
@@ -12,30 +12,31 @@
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   * OTHER DEALINGS IN THE SOFTWARE.
   *
   * Authors: Christian König
   */
  #ifndef __AMDGPU_RING_H__
  #define __AMDGPU_RING_H__
 
+#include <drm/amdgpu_drm.h>
  #include "gpu_scheduler.h"
 
  /* max number of rings */
  #define AMDGPU_MAX_RINGS		18
  #define AMDGPU_MAX_GFX_RINGS		1
  #define AMDGPU_MAX_COMPUTE_RINGS	8
  #define AMDGPU_MAX_VCE_RINGS		3
  #define AMDGPU_MAX_UVD_ENC_RINGS	2
 
  /* some special values for the owner field */
  #define AMDGPU_FENCE_OWNER_UNDEFINED	((void*)0ul)
  #define AMDGPU_FENCE_OWNER_VM		((void*)1ul)
 
  #define AMDGPU_FENCE_FLAG_64BIT         (1 << 0)
  #define AMDGPU_FENCE_FLAG_INT           (1 << 1)
@@ -44,30 +45,31 @@ enum amdgpu_ring_type {
  	AMDGPU_RING_TYPE_GFX,
  	AMDGPU_RING_TYPE_COMPUTE,
  	AMDGPU_RING_TYPE_SDMA,
  	AMDGPU_RING_TYPE_UVD,
  	AMDGPU_RING_TYPE_VCE,
  	AMDGPU_RING_TYPE_KIQ,
  	AMDGPU_RING_TYPE_UVD_ENC,
  	AMDGPU_RING_TYPE_VCN_DEC,
  	AMDGPU_RING_TYPE_VCN_ENC
  };
 
  struct amdgpu_device;
  struct amdgpu_ring;
  struct amdgpu_ib;
  struct amdgpu_cs_parser;
+struct amdgpu_job;
 
  /*
   * Fences.
   */
  struct amdgpu_fence_driver {
  	uint64_t			gpu_addr;
  	volatile uint32_t		*cpu_addr;
  	/* sync_seq is protected by ring emission lock */
  	uint32_t			sync_seq;
  	atomic_t			last_seq;
  	bool				initialized;
  	struct amdgpu_irq_src		*irq_src;
  	unsigned			irq_type;
  	struct timer_list		fallback_timer;
  	unsigned			num_fences_mask;
@@ -135,30 +137,33 @@ struct amdgpu_ring_funcs {
  	void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
  	void (*insert_start)(struct amdgpu_ring *ring);
  	void (*insert_end)(struct amdgpu_ring *ring);
  	/* pad the indirect buffer to the necessary number of dw */
  	void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
  	unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
  	void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
  	/* note usage for clock and power gating */
  	void (*begin_use)(struct amdgpu_ring *ring);
  	void (*end_use)(struct amdgpu_ring *ring);
  	void (*emit_switch_buffer) (struct amdgpu_ring *ring);
  	void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
  	void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
  	void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
  	void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
+	/* priority functions */
+	void (*set_priority) (struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
  };
 
  struct amdgpu_ring {
  	struct amdgpu_device		*adev;
  	const struct amdgpu_ring_funcs	*funcs;
  	struct amdgpu_fence_driver	fence_drv;
  	struct amd_gpu_scheduler	sched;
  	struct list_head		lru_list;
 
  	struct amdgpu_bo	*ring_obj;
  	volatile uint32_t	*ring;
  	unsigned		rptr_offs;
  	u64			wptr;
  	u64			wptr_old;
  	unsigned		ring_size;
@@ -175,40 +180,50 @@ struct amdgpu_ring {
  	struct amdgpu_bo	*mqd_obj;
  	uint64_t                mqd_gpu_addr;
  	void                    *mqd_ptr;
  	uint64_t                eop_gpu_addr;
  	u32			doorbell_index;
  	bool			use_doorbell;
  	unsigned		wptr_offs;
  	unsigned		fence_offs;
  	uint64_t		current_ctx;
  	char			name[16];
  	unsigned		cond_exe_offs;
  	u64			cond_exe_gpu_addr;
  	volatile u32		*cond_exe_cpu_addr;
  	unsigned		vm_inv_eng;
  	bool			has_compute_vm_bug;
+
+	atomic_t		num_jobs[AMD_SCHED_PRIORITY_MAX];
+	struct mutex		priority_mutex;
+	/* protected by priority_mutex */
+	int			priority;
+
  #if defined(CONFIG_DEBUG_FS)
  	struct dentry *ent;
  #endif
  };
 
  int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
  void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
  void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
  void amdgpu_ring_commit(struct amdgpu_ring *ring);
  void amdgpu_ring_undo(struct amdgpu_ring *ring);
+void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
+void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
  int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  		     unsigned ring_size, struct amdgpu_irq_src *irq_src,
  		     unsigned irq_type);
  void amdgpu_ring_fini(struct amdgpu_ring *ring);
  int amdgpu_ring_lru_get(struct amdgpu_device *adev, int type, int *blacklist,
  			int num_blacklist, struct amdgpu_ring **ring);
  void amdgpu_ring_lru_touch(struct amdgpu_device *adev, struct amdgpu_ring *ring);
  static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
  {
  	int i = 0;
  	while (i <= ring->buf_mask)
  		ring->ring[i++] = ring->funcs->nop;
 
  }
 
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +0 -0
index 38e622c..dbcaa2e 100644
@@ -158,16 +158,23 @@ int amd_sched_fence_slab_init(void);
  void amd_sched_fence_slab_fini(void);
 
  struct amd_sched_fence *amd_sched_fence_create(
  	struct amd_sched_entity *s_entity, void *owner);
  void amd_sched_fence_scheduled(struct amd_sched_fence *fence);
  void amd_sched_fence_finished(struct amd_sched_fence *fence);
  int amd_sched_job_init(struct amd_sched_job *job,
  		       struct amd_gpu_scheduler *sched,
  		       struct amd_sched_entity *entity,
  		       void *owner);
  void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
  void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
  bool amd_sched_dependency_optimized(struct dma_fence* fence,

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h int amd_sched_fence_slab_init(void); · line 180

Quoted 10 lines
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +7 -0
  				    struct amd_sched_entity *entity);
  void amd_sched_job_kickout(struct amd_sched_job *s_job);
+
+static inline enum amd_sched_priority
+amd_sched_get_job_priority(struct amd_sched_job *job)
+{
+	return (job->s_entity->rq - job->sched->sched_rq);
+}
+
  #endif

_______________________________________________
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): 9df964e4-07cf-cdff-dd08-09ccda955d55-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#11 lore raw

PATCH 2/8 drm/amdgpu: add framework for HW specific priority settings v9

Add an initial framework for changing the HW priorities of rings. The framework allows requesting priority changes for the lifetime of an amdgpu_job. After the job completes the priority will decay to the next lowest priority for which a request is still valid.

A new ring function set_priority() can now be populated to take care of the HW specific programming sequence for priority changes.

v2: set priority before emitting IB, and take a ref on amdgpu_job
v3: use AMD_SCHED_PRIORITY_* instead of AMDGPU_CTX_PRIORITY_*
v4: plug amdgpu_ring_restore_priority_cb into amdgpu_job_free_cb
v5: use atomic for tracking job priorities instead of last_job
v6: rename amdgpu_ring_priority_[get/put]() and align parameters
v7: replace spinlocks with mutexes for KIQ compatibility
v8: raise ring priority during cs_ioctl, instead of job_run
v9: priority_get() before push_job()

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c       |  4 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c      | 76 ++++++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h      | 15 ++++++
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  7 +++
 5 files changed, 103 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -0
index b17635c..6eda0f6 100644
@@ -1005,6 +1005,8 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	job->uf_sequence = cs->out.handle;
 	amdgpu_job_free_resources(job);
 	amdgpu_cs_parser_fini(p, 0, true);
+	amdgpu_ring_priority_get(job->ring,
+				 amd_sched_get_job_priority(&job->base));
 
 	trace_amdgpu_cs_ioctl(job);
 	amd_sched_entity_push_job(&job->base);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +4 -0
index 3d641e1..63b0f3e 100644
@@ -101,6 +101,7 @@ static void amdgpu_job_free_cb(struct amd_sched_job *s_job)
 {
 	struct amdgpu_job *job = container_of(s_job, struct amdgpu_job, base);
 
+	amdgpu_ring_priority_put(job->ring, amd_sched_get_job_priority(s_job));
 	dma_fence_put(job->fence);
 	amdgpu_sync_free(&job->sync);
 	amdgpu_sync_free(&job->dep_sync);
@@ -137,6 +138,8 @@ int amdgpu_job_submit(struct amdgpu_job *job, struct amdgpu_ring *ring,
 	job->fence_ctx = entity->fence_context;
 	*f = dma_fence_get(&job->base.s_fence->finished);
 	amdgpu_job_free_resources(job);
+	amdgpu_ring_priority_get(job->ring,
+				 amd_sched_get_job_priority(&job->base));
 	amd_sched_entity_push_job(&job->base);
 
 	return 0;
@@ -201,6 +204,7 @@ static struct dma_fence *amdgpu_job_run(struct amd_sched_job *sched_job)
 	/* if gpu reset, hw fence will be replaced here */
 	dma_fence_put(job->fence);
 	job->fence = dma_fence_get(fence);
+
 	amdgpu_job_free_resources(job);
 	return fence;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +75 -1

97 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +15 -0
index bc8dec9..7348769 100644
@@ -24,6 +24,7 @@
 #ifndef __AMDGPU_RING_H__
 #define __AMDGPU_RING_H__
 
+#include <drm/amdgpu_drm.h>
 #include "gpu_scheduler.h"
 
 /* max number of rings */
@@ -56,6 +57,7 @@ struct amdgpu_device;
 struct amdgpu_ring;
 struct amdgpu_ib;
 struct amdgpu_cs_parser;
+struct amdgpu_job;
 
 /*
  * Fences.
@@ -147,6 +149,9 @@ struct amdgpu_ring_funcs {
 	void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
 	void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
 	void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
+	/* priority functions */
+	void (*set_priority) (struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
 };
 
 struct amdgpu_ring {
@@ -187,6 +192,12 @@ struct amdgpu_ring {
 	volatile u32		*cond_exe_cpu_addr;
 	unsigned		vm_inv_eng;
 	bool			has_compute_vm_bug;
+
+	atomic_t		num_jobs[AMD_SCHED_PRIORITY_MAX];
+	struct mutex		priority_mutex;
+	/* protected by priority_mutex */
+	int			priority;
+
 #if defined(CONFIG_DEBUG_FS)
 	struct dentry *ent;
 #endif
@@ -197,6 +208,10 @@ void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
 void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
 void amdgpu_ring_commit(struct amdgpu_ring *ring);
 void amdgpu_ring_undo(struct amdgpu_ring *ring);
+void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
+void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
+			      enum amd_sched_priority priority);
 int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 		     unsigned ring_size, struct amdgpu_irq_src *irq_src,
 		     unsigned irq_type);
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +7 -0
index 38e622c..dbcaa2e 100644
@@ -170,4 +170,11 @@ void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
 bool amd_sched_dependency_optimized(struct dma_fence* fence,
 				    struct amd_sched_entity *entity);
 void amd_sched_job_kickout(struct amd_sched_job *s_job);
+
+static inline enum amd_sched_priority
+amd_sched_get_job_priority(struct amd_sched_job *job)
+{
+	return (job->s_entity->rq - job->sched->sched_rq);
+}
+
 #endif
Signature · 6 lines
2.9.3

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

PATCH 3/8 drm/amdgpu: implement ring set_priority for gfx_v8 compute v9

In reply to a message not in the archive

Programming CP_HQD_QUEUE_PRIORITY enables a queue to take priority over other queues on the same pipe. Multiple queues on a pipe are timesliced so this gives us full precedence over other queues.

Programming CP_HQD_PIPE_PRIORITY changes the SPI_ARB_PRIORITY of the wave as follows:

        0x2: CS_H
        0x1: CS_M
        0x0: CS_L

The SPI block will then dispatch work according to the policy set by SPI_ARB_PRIORITY. In the current policy CS_H is higher priority than gfx.

In order to prevent getting stuck in loops of resources bouncing between GFX and high priority compute and introducing further latency, we statically reserve a portion of the pipe.

v2: fix srbm_select to ring->queue and use ring->funcs->type
v3: use AMD_SCHED_PRIORITY_* instead of AMDGPU_CTX_PRIORITY_*
v4: switch int to enum amd_sched_priority
v5: corresponding changes for srbm_lock
v6: change CU reservation to PIPE_PERCENT allocation
v7: use kiq instead of MMIO
v8: back to MMIO, and make the implementation sleep safe.
v9: corresponding changes for splitting HIGH into _HW/_SW

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |   4 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |   2 +
 drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c      | 105 +++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +4 -0
index 3a0561c..04ea1b9 100644
@@ -1097,30 +1097,34 @@ struct amdgpu_gfx {
 	/* gfx status */
 	uint32_t			gfx_current_status;
 	/* ce ram size*/
 	unsigned			ce_ram_size;
 	struct amdgpu_cu_info		cu_info;
 	const struct amdgpu_gfx_funcs	*funcs;
 
 	/* reset mask */
 	uint32_t                        grbm_soft_reset;
 	uint32_t                        srbm_soft_reset;
 	bool                            in_reset;
 	/* s3/s4 mask */
 	bool                            in_suspend;
 	/* NGG */
 	struct amdgpu_ngg		ngg;
+
+	/* pipe reservation */
+	struct mutex			pipe_reserve_mutex;
+	DECLARE_BITMAP			(pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
 };
 
 int amdgpu_ib_get(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		  unsigned size, struct amdgpu_ib *ib);
 void amdgpu_ib_free(struct amdgpu_device *adev, struct amdgpu_ib *ib,
 		    struct dma_fence *f);
 int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned num_ibs,
 		       struct amdgpu_ib *ibs, struct amdgpu_job *job,
 		       struct dma_fence **f);
 int amdgpu_ib_pool_init(struct amdgpu_device *adev);
 void amdgpu_ib_pool_fini(struct amdgpu_device *adev);
 int amdgpu_ib_ring_tests(struct amdgpu_device *adev);
 
 /*
  * CS.
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 0296c9e..424ac3d 100644
@@ -2012,58 +2012,60 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 	adev->dev = &pdev->dev;
 	adev->ddev = ddev;
 	adev->pdev = pdev;
 	adev->flags = flags;
 	adev->asic_type = flags & AMD_ASIC_MASK;
 	adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
 	adev->mc.gtt_size = 512 * 1024 * 1024;
 	adev->accel_working = false;
 	adev->num_rings = 0;
 	adev->mman.buffer_funcs = NULL;
 	adev->mman.buffer_funcs_ring = NULL;
 	adev->vm_manager.vm_pte_funcs = NULL;
 	adev->vm_manager.vm_pte_num_rings = 0;
 	adev->gart.gart_funcs = NULL;
 	adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
+	bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
 
 	adev->smc_rreg = &amdgpu_invalid_rreg;
 	adev->smc_wreg = &amdgpu_invalid_wreg;
 	adev->pcie_rreg = &amdgpu_invalid_rreg;
 	adev->pcie_wreg = &amdgpu_invalid_wreg;
 	adev->pciep_rreg = &amdgpu_invalid_rreg;
 	adev->pciep_wreg = &amdgpu_invalid_wreg;
 	adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
 	adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
 	adev->didt_rreg = &amdgpu_invalid_rreg;
 	adev->didt_wreg = &amdgpu_invalid_wreg;
 	adev->gc_cac_rreg = &amdgpu_invalid_rreg;
 	adev->gc_cac_wreg = &amdgpu_invalid_wreg;
 	adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
 	adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
 
 
 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
 		 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
 
 	/* mutex initialization are all done here so we
 	 * can recall function without having locking issues */
 	atomic_set(&adev->irq.ih.lock, 0);
 	mutex_init(&adev->firmware.mutex);
 	mutex_init(&adev->pm.mutex);
 	mutex_init(&adev->gfx.gpu_clock_mutex);
 	mutex_init(&adev->srbm_mutex);
+	mutex_init(&adev->gfx.pipe_reserve_mutex);
 	mutex_init(&adev->grbm_idx_mutex);
 	mutex_init(&adev->mn_lock);
 	hash_init(adev->mn_hash);
 
 	amdgpu_check_arguments(adev);
 
 	/* Registers mapping */
 	/* TODO: block userspace mapping of io register */
 	spin_lock_init(&adev->mmio_idx_lock);
 	spin_lock_init(&adev->smc_idx_lock);
 	spin_lock_init(&adev->pcie_idx_lock);
 	spin_lock_init(&adev->uvd_ctx_idx_lock);
 	spin_lock_init(&adev->didt_idx_lock);
 	spin_lock_init(&adev->gc_cac_idx_lock);
 	spin_lock_init(&adev->audio_endpt_idx_lock);
drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +105 -0

165 lines not on this page · raw message

Signature · 6 lines
2.9.3

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

PATCH 4/8 drm/amdgpu: make amdgpu_to_sched_priority detect invalid parameters

In reply to a message not in the archive

Returning invalid priorities as _NORMAL is a backwards compatibility quirk of amdgpu_ctx_ioctl(). Move this detail one layer up where it belongs.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c       | 8 +++++---
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h | 3 ++-
 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +5 -3
index 68a852f..0285eef 100644
@@ -219,51 +219,53 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 
 static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
 {
 	switch (amdgpu_priority) {
 	case AMDGPU_CTX_PRIORITY_HIGH_HW:
 		return AMD_SCHED_PRIORITY_HIGH_HW;
 	case AMDGPU_CTX_PRIORITY_HIGH_SW:
 		return AMD_SCHED_PRIORITY_HIGH_SW;
 	case AMDGPU_CTX_PRIORITY_NORMAL:
 		return AMD_SCHED_PRIORITY_NORMAL;
 	case AMDGPU_CTX_PRIORITY_LOW_SW:
 	case AMDGPU_CTX_PRIORITY_LOW_HW:
 		return AMD_SCHED_PRIORITY_LOW;
 	default:
 		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
-		return AMD_SCHED_PRIORITY_NORMAL;
+		return AMD_SCHED_PRIORITY_INVALID;
 	}
 }
 
 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 		     struct drm_file *filp)
 {
 	int r;
 	uint32_t id;
 	enum amd_sched_priority priority;
 
 	union drm_amdgpu_ctx *args = data;
 	struct amdgpu_device *adev = dev->dev_private;
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 
 	r = 0;
 	id = args->in.ctx_id;
 	priority = amdgpu_to_sched_priority(args->in.priority);
 
-	if (priority >= AMD_SCHED_PRIORITY_MAX)
-		return -EINVAL;
+	/* For backwards compatibility reasons, we need to accept
+	 * ioctls with garbage in the priority field */
+	if (priority == AMD_SCHED_PRIORITY_INVALID)
+		priority = AMD_SCHED_PRIORITY_NORMAL;
 
 	switch (args->in.op) {
 	case AMDGPU_CTX_OP_ALLOC_CTX:
 		r = amdgpu_ctx_alloc(adev, fpriv, priority, filp, &id);
 		args->out.alloc.ctx_id = id;
 		break;
 	case AMDGPU_CTX_OP_FREE_CTX:
 		r = amdgpu_ctx_free(fpriv, id);
 		break;
 	case AMDGPU_CTX_OP_QUERY_STATE:
 		r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
 		break;
 	default:
 		return -EINVAL;
 	}
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +2 -1
index dbcaa2e..da040bc 100644
@@ -108,31 +108,32 @@ static inline bool amd_sched_invalidate_job(struct amd_sched_job *s_job, int thr
 */
 struct amd_sched_backend_ops {
 	struct dma_fence *(*dependency)(struct amd_sched_job *sched_job);
 	struct dma_fence *(*run_job)(struct amd_sched_job *sched_job);
 	void (*timedout_job)(struct amd_sched_job *sched_job);
 	void (*free_job)(struct amd_sched_job *sched_job);
 };
 
 enum amd_sched_priority {
 	AMD_SCHED_PRIORITY_MIN,
 	AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
 	AMD_SCHED_PRIORITY_NORMAL,
 	AMD_SCHED_PRIORITY_HIGH_SW,
 	AMD_SCHED_PRIORITY_HIGH_HW,
 	AMD_SCHED_PRIORITY_KERNEL,
-	AMD_SCHED_PRIORITY_MAX
+	AMD_SCHED_PRIORITY_MAX,
+	AMD_SCHED_PRIORITY_INVALID = -1
 };
 
 /**
  * One scheduler is implemented for each hardware ring
 */
 struct amd_gpu_scheduler {
 	const struct amd_sched_backend_ops	*ops;
 	uint32_t			hw_submission_limit;
 	long				timeout;
 	const char			*name;
 	struct amd_sched_rq		sched_rq[AMD_SCHED_PRIORITY_MAX];
 	wait_queue_head_t		wake_up_worker;
 	wait_queue_head_t		job_scheduled;
 	atomic_t			hw_rq_count;
 	atomic64_t			job_id_count;
Signature · 6 lines
2.9.3

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

PATCH 5/8 drm/amdgpu: move priority decay logic into amd_sched_priority_ctr

In reply to a message not in the archive

So that it can be re-used.

A priority counter can be used to track priority requests. If no active requests exists, the counter will default to ()->default_priority.

The re-factored version is now allowed to decay below NORMAL. This allows us to create requests that lower the priority. This didn't matter for rings, as no priority below NORMAL has an effect.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c      |  69 +++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h      |   6 +-
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.c | 122 ++++++++++++++++++++++++++
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  21 +++++
 4 files changed, 164 insertions(+), 54 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +20 -49
index 2d8b20a7..159ab0e 100644
@@ -142,115 +142,86 @@ void amdgpu_ring_commit(struct amdgpu_ring *ring)
 /**
  * amdgpu_ring_undo - reset the wptr
  *
  * @ring: amdgpu_ring structure holding ring information
  *
  * Reset the driver's copy of the wptr (all asics).
  */
 void amdgpu_ring_undo(struct amdgpu_ring *ring)
 {
 	ring->wptr = ring->wptr_old;
 
 	if (ring->funcs->end_use)
 		ring->funcs->end_use(ring);
 }
 
+static void amdgpu_ring_priority_set(struct amd_sched_priority_ctr *p,
+				     enum amd_sched_priority priority)
+{
+	struct amdgpu_ring *ring = container_of(p, struct amdgpu_ring,
+						priority_ctr);
+
+	if (ring->funcs->set_priority)
+		ring->funcs->set_priority(ring, priority);
+}
+
 /**
  * amdgpu_ring_priority_put - restore a ring's priority
  *
  * @ring: amdgpu_ring structure holding the information
  * @priority: target priority
  *
  * Release a request for executing at @priority
  */
 void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
 			      enum amd_sched_priority priority)
 {
-	int i;
-
-	if (!ring->funcs->set_priority)
-		return;
-
-	if (atomic_dec_return(&ring->num_jobs[priority]) > 0)
-		return;
-
-	/* no need to restore if the job is already at the lowest priority */
-	if (priority == AMD_SCHED_PRIORITY_NORMAL)
-		return;
-
-	mutex_lock(&ring->priority_mutex);
-	/* something higher prio is executing, no need to decay */
-	if (ring->priority > priority)
-		goto out_unlock;
-
-	/* decay priority to the next level with a job available */
-	for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
-		if (i == AMD_SCHED_PRIORITY_NORMAL
-				|| atomic_read(&ring->num_jobs[i])) {
-			ring->priority = i;
-			ring->funcs->set_priority(ring, i);
-			break;
-		}
-	}
-
-out_unlock:
-	mutex_unlock(&ring->priority_mutex);
+	if (ring->funcs->set_priority)
+		amd_sched_priority_ctr_put(&ring->priority_ctr, priority);
 }
 
 /**
  * amdgpu_ring_priority_get - change the ring's priority
  *
  * @ring: amdgpu_ring structure holding the information
  * @priority: target priority
  *
  * Request a ring's priority to be raised to @priority (refcounted).
  */
 void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
 			      enum amd_sched_priority priority)
 {
-	if (!ring->funcs->set_priority)
-		return;
-
-	atomic_inc(&ring->num_jobs[priority]);
-
-	mutex_lock(&ring->priority_mutex);
-	if (priority <= ring->priority)
-		goto out_unlock;
-
-	ring->priority = priority;
-	ring->funcs->set_priority(ring, priority);
-
-out_unlock:
-	mutex_unlock(&ring->priority_mutex);
+	if (ring->funcs->set_priority)
+		amd_sched_priority_ctr_get(&ring->priority_ctr, priority);
 }
 
 /**
  * amdgpu_ring_init - init driver ring struct.
  *
  * @adev: amdgpu_device pointer
  * @ring: amdgpu_ring structure holding ring information
  * @max_ndw: maximum number of dw for ring alloc
  * @nop: nop packet for this ring
  *
  * Initialize the driver information for the selected ring (all asics).
  * Returns 0 on success, error on failure.
  */
 int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 		     unsigned max_dw, struct amdgpu_irq_src *irq_src,
 		     unsigned irq_type)
 {
-	int r, i;
+	int r;
 
 	if (ring->adev == NULL) {
 		if (adev->num_rings >= AMDGPU_MAX_RINGS)
 			return -EINVAL;
 
 		ring->adev = adev;
 		ring->idx = adev->num_rings++;
 		adev->rings[ring->idx] = ring;
 		r = amdgpu_fence_driver_init_ring(ring,
 			amdgpu_sched_hw_submission);
 		if (r)
 			return r;
 	}
 
 	if (ring->funcs->support_64bit_ptrs) {
@@ -312,55 +283,55 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	/* Allocate ring buffer */
 	if (ring->ring_obj == NULL) {
 		r = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
 					    AMDGPU_GEM_DOMAIN_GTT,
 					    &ring->ring_obj,
 					    &ring->gpu_addr,
 					    (void **)&ring->ring);
 		if (r) {
 			dev_err(adev->dev, "(%d) ring create failed\n", r);
 			return r;
 		}
 		amdgpu_ring_clear_ring(ring);
 	}
 
 	ring->max_dw = max_dw;
-	ring->priority = AMD_SCHED_PRIORITY_NORMAL;
-	mutex_init(&ring->priority_mutex);
 	INIT_LIST_HEAD(&ring->lru_list);
 	amdgpu_ring_lru_touch(adev, ring);
-
-	for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
-		atomic_set(&ring->num_jobs[i], 0);
+	amd_sched_priority_ctr_init(&ring->priority_ctr,
+				    AMD_SCHED_PRIORITY_NORMAL,
+				    amdgpu_ring_priority_set);
 
 	if (amdgpu_debugfs_ring_init(adev, ring)) {
 		DRM_ERROR("Failed to register debugfs file for rings !\n");
 	}
 
 	return 0;
 }
 
 /**
  * amdgpu_ring_fini - tear down the driver ring struct.
  *
  * @adev: amdgpu_device pointer
  * @ring: amdgpu_ring structure holding ring information
  *
  * Tear down the driver information for the selected ring (all asics).
  */
 void amdgpu_ring_fini(struct amdgpu_ring *ring)
 {
+	amd_sched_priority_ctr_fini(&ring->priority_ctr);
+
 	ring->ready = false;
 
 	if (ring->funcs->support_64bit_ptrs) {
 		amdgpu_wb_free_64bit(ring->adev, ring->cond_exe_offs);
 		amdgpu_wb_free_64bit(ring->adev, ring->fence_offs);
 		amdgpu_wb_free_64bit(ring->adev, ring->rptr_offs);
 		amdgpu_wb_free_64bit(ring->adev, ring->wptr_offs);
 	} else {
 		amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
 		amdgpu_wb_free(ring->adev, ring->fence_offs);
 		amdgpu_wb_free(ring->adev, ring->rptr_offs);
 		amdgpu_wb_free(ring->adev, ring->wptr_offs);
 	}
 
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -5
index 7348769..2a04c0d 100644
@@ -147,30 +147,31 @@ struct amdgpu_ring_funcs {
 	void (*emit_switch_buffer) (struct amdgpu_ring *ring);
 	void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
 	void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
 	void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
 	void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
 	/* priority functions */
 	void (*set_priority) (struct amdgpu_ring *ring,
 			      enum amd_sched_priority priority);
 };
 
 struct amdgpu_ring {
 	struct amdgpu_device		*adev;
 	const struct amdgpu_ring_funcs	*funcs;
 	struct amdgpu_fence_driver	fence_drv;
 	struct amd_gpu_scheduler	sched;
+	struct amd_sched_priority_ctr	priority_ctr;
 	struct list_head		lru_list;
 
 	struct amdgpu_bo	*ring_obj;
 	volatile uint32_t	*ring;
 	unsigned		rptr_offs;
 	u64			wptr;
 	u64			wptr_old;
 	unsigned		ring_size;
 	unsigned		max_dw;
 	int			count_dw;
 	uint64_t		gpu_addr;
 	uint64_t		ptr_mask;
 	uint32_t		buf_mask;
 	bool			ready;
 	u32			idx;
@@ -181,35 +182,30 @@ struct amdgpu_ring {
 	uint64_t                mqd_gpu_addr;
 	void                    *mqd_ptr;
 	uint64_t                eop_gpu_addr;
 	u32			doorbell_index;
 	bool			use_doorbell;
 	unsigned		wptr_offs;
 	unsigned		fence_offs;
 	uint64_t		current_ctx;
 	char			name[16];
 	unsigned		cond_exe_offs;
 	u64			cond_exe_gpu_addr;
 	volatile u32		*cond_exe_cpu_addr;
 	unsigned		vm_inv_eng;
 	bool			has_compute_vm_bug;
 
-	atomic_t		num_jobs[AMD_SCHED_PRIORITY_MAX];
-	struct mutex		priority_mutex;
-	/* protected by priority_mutex */
-	int			priority;
-
 #if defined(CONFIG_DEBUG_FS)
 	struct dentry *ent;
 #endif
 };
 
 int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
 void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
 void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
 void amdgpu_ring_commit(struct amdgpu_ring *ring);
 void amdgpu_ring_undo(struct amdgpu_ring *ring);
 void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
 			      enum amd_sched_priority priority);
 void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
 			      enum amd_sched_priority priority);
 int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +122 -0

152 lines not on this page · raw message

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +21 -0
index da040bc..b9283b5 100644
@@ -17,30 +17,31 @@
  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  *
  */
 
 #ifndef _GPU_SCHEDULER_H_
 #define _GPU_SCHEDULER_H_
 
 #include <linux/kfifo.h>
 #include <linux/dma-fence.h>
 
 struct amd_gpu_scheduler;
 struct amd_sched_rq;
+struct amd_sched_priority_ctr;
 
 /**
  * A scheduler entity is a wrapper around a job queue or a group
  * of other entities. Entities take turns emitting jobs from their
  * job queues to corresponding hardware ring based on scheduling
  * policy.
 */
 struct amd_sched_entity {
 	struct list_head		list;
 	struct amd_sched_rq		*rq;
 	struct amd_gpu_scheduler	*sched;
 
 	spinlock_t			queue_lock;
 	struct kfifo                    job_queue;
 
@@ -112,30 +113,41 @@ struct amd_sched_backend_ops {
 	void (*timedout_job)(struct amd_sched_job *sched_job);
 	void (*free_job)(struct amd_sched_job *sched_job);
 };
 
 enum amd_sched_priority {
 	AMD_SCHED_PRIORITY_MIN,
 	AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
 	AMD_SCHED_PRIORITY_NORMAL,
 	AMD_SCHED_PRIORITY_HIGH_SW,
 	AMD_SCHED_PRIORITY_HIGH_HW,
 	AMD_SCHED_PRIORITY_KERNEL,
 	AMD_SCHED_PRIORITY_MAX,
 	AMD_SCHED_PRIORITY_INVALID = -1
 };
 
+typedef void (*amd_sched_set_priority_func_t)(struct amd_sched_priority_ctr *p,
+					      enum amd_sched_priority priority);
+
+struct amd_sched_priority_ctr {
+	struct mutex			mutex;
+	atomic_t			requests[AMD_SCHED_PRIORITY_MAX];
+	enum amd_sched_priority		priority;
+	enum amd_sched_priority		default_priority;
+	amd_sched_set_priority_func_t	set_priority;
+};
+
 /**
  * One scheduler is implemented for each hardware ring
 */
 struct amd_gpu_scheduler {
 	const struct amd_sched_backend_ops	*ops;
 	uint32_t			hw_submission_limit;
 	long				timeout;
 	const char			*name;
 	struct amd_sched_rq		sched_rq[AMD_SCHED_PRIORITY_MAX];
 	wait_queue_head_t		wake_up_worker;
 	wait_queue_head_t		job_scheduled;
 	atomic_t			hw_rq_count;
 	atomic64_t			job_id_count;
 	struct task_struct		*thread;
 	struct list_head	ring_mirror_list;
@@ -166,16 +178,25 @@ int amd_sched_job_init(struct amd_sched_job *job,
 		       struct amd_gpu_scheduler *sched,
 		       struct amd_sched_entity *entity,
 		       void *owner);
 void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
 void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
 bool amd_sched_dependency_optimized(struct dma_fence* fence,
 				    struct amd_sched_entity *entity);
 void amd_sched_job_kickout(struct amd_sched_job *s_job);
 
 static inline enum amd_sched_priority
 amd_sched_get_job_priority(struct amd_sched_job *job)
 {
 	return (job->s_entity->rq - job->sched->sched_rq);
 }
 
+void amd_sched_priority_ctr_init(struct amd_sched_priority_ctr *p,
+				 enum amd_sched_priority default_priority,
+				 amd_sched_set_priority_func_t set_priority);
+void amd_sched_priority_ctr_fini(struct amd_sched_priority_ctr *p);
+void amd_sched_priority_ctr_get(struct amd_sched_priority_ctr *p,
+				enum amd_sched_priority priority);
+void amd_sched_priority_ctr_put(struct amd_sched_priority_ctr *p,
+				enum amd_sched_priority priority);
+
 #endif
Signature · 6 lines
2.9.3

_______________________________________________
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): 20170608220623.9002-6-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#16 lore raw

Re: [PATCH 5/8] drm/amdgpu: move priority decay logic into amd_sched_priority_ctr

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 8 lines

So that it can be re-used.

A priority counter can be used to track priority requests. If no active requests exists, the counter will default to ()->default_priority.

The re-factored version is now allowed to decay below NORMAL. This allows us to create requests that lower the priority. This didn't matter for rings, as no priority below NORMAL has an effect.

NAK to the whole approach. This handling is amdgpu specific and not related to the gpu scheduler in any way.

Christian.

Quoted 528 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c      |  69 +++++----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h      |   6 +-
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.c | 122 ++++++++++++++++++++++++++
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  21 +++++
  4 files changed, 164 insertions(+), 54 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +20 -49
index 2d8b20a7..159ab0e 100644
@@ -142,115 +142,86 @@ void amdgpu_ring_commit(struct amdgpu_ring *ring)
  /**
   * amdgpu_ring_undo - reset the wptr
   *
   * @ring: amdgpu_ring structure holding ring information
   *
   * Reset the driver's copy of the wptr (all asics).
   */
  void amdgpu_ring_undo(struct amdgpu_ring *ring)
  {
  	ring->wptr = ring->wptr_old;
 
  	if (ring->funcs->end_use)
  		ring->funcs->end_use(ring);
  }
 
+static void amdgpu_ring_priority_set(struct amd_sched_priority_ctr *p,
+				     enum amd_sched_priority priority)
+{
+	struct amdgpu_ring *ring = container_of(p, struct amdgpu_ring,
+						priority_ctr);
+
+	if (ring->funcs->set_priority)
+		ring->funcs->set_priority(ring, priority);
+}
+
  /**
   * amdgpu_ring_priority_put - restore a ring's priority
   *
   * @ring: amdgpu_ring structure holding the information
   * @priority: target priority
   *
   * Release a request for executing at @priority
   */
  void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
  			      enum amd_sched_priority priority)
  {
-	int i;
-
-	if (!ring->funcs->set_priority)
-		return;
-
-	if (atomic_dec_return(&ring->num_jobs[priority]) > 0)
-		return;
-
-	/* no need to restore if the job is already at the lowest priority */
-	if (priority == AMD_SCHED_PRIORITY_NORMAL)
-		return;
-
-	mutex_lock(&ring->priority_mutex);
-	/* something higher prio is executing, no need to decay */
-	if (ring->priority > priority)
-		goto out_unlock;
-
-	/* decay priority to the next level with a job available */
-	for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
-		if (i == AMD_SCHED_PRIORITY_NORMAL
-				|| atomic_read(&ring->num_jobs[i])) {
-			ring->priority = i;
-			ring->funcs->set_priority(ring, i);
-			break;
-		}
-	}
-
-out_unlock:
-	mutex_unlock(&ring->priority_mutex);
+	if (ring->funcs->set_priority)
+		amd_sched_priority_ctr_put(&ring->priority_ctr, priority);
  }
 
  /**
   * amdgpu_ring_priority_get - change the ring's priority
   *
   * @ring: amdgpu_ring structure holding the information
   * @priority: target priority
   *
   * Request a ring's priority to be raised to @priority (refcounted).
   */
  void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
  			      enum amd_sched_priority priority)
  {
-	if (!ring->funcs->set_priority)
-		return;
-
-	atomic_inc(&ring->num_jobs[priority]);
-
-	mutex_lock(&ring->priority_mutex);
-	if (priority <= ring->priority)
-		goto out_unlock;
-
-	ring->priority = priority;
-	ring->funcs->set_priority(ring, priority);
-
-out_unlock:
-	mutex_unlock(&ring->priority_mutex);
+	if (ring->funcs->set_priority)
+		amd_sched_priority_ctr_get(&ring->priority_ctr, priority);
  }
 
  /**
   * amdgpu_ring_init - init driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   * @max_ndw: maximum number of dw for ring alloc
   * @nop: nop packet for this ring
   *
   * Initialize the driver information for the selected ring (all asics).
   * Returns 0 on success, error on failure.
   */
  int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  		     unsigned max_dw, struct amdgpu_irq_src *irq_src,
  		     unsigned irq_type)
  {
-	int r, i;
+	int r;
 
  	if (ring->adev == NULL) {
  		if (adev->num_rings >= AMDGPU_MAX_RINGS)
  			return -EINVAL;
 
  		ring->adev = adev;
  		ring->idx = adev->num_rings++;
  		adev->rings[ring->idx] = ring;
  		r = amdgpu_fence_driver_init_ring(ring,
  			amdgpu_sched_hw_submission);
  		if (r)
  			return r;
  	}
 
  	if (ring->funcs->support_64bit_ptrs) {
@@ -312,55 +283,55 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  	/* Allocate ring buffer */
  	if (ring->ring_obj == NULL) {
  		r = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
  					    AMDGPU_GEM_DOMAIN_GTT,
  					    &ring->ring_obj,
  					    &ring->gpu_addr,
  					    (void **)&ring->ring);
  		if (r) {
  			dev_err(adev->dev, "(%d) ring create failed\n", r);
  			return r;
  		}
  		amdgpu_ring_clear_ring(ring);
  	}
 
  	ring->max_dw = max_dw;
-	ring->priority = AMD_SCHED_PRIORITY_NORMAL;
-	mutex_init(&ring->priority_mutex);
  	INIT_LIST_HEAD(&ring->lru_list);
  	amdgpu_ring_lru_touch(adev, ring);
-
-	for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
-		atomic_set(&ring->num_jobs[i], 0);
+	amd_sched_priority_ctr_init(&ring->priority_ctr,
+				    AMD_SCHED_PRIORITY_NORMAL,
+				    amdgpu_ring_priority_set);
 
  	if (amdgpu_debugfs_ring_init(adev, ring)) {
  		DRM_ERROR("Failed to register debugfs file for rings !\n");
  	}
 
  	return 0;
  }
 
  /**
   * amdgpu_ring_fini - tear down the driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   *
   * Tear down the driver information for the selected ring (all asics).
   */
  void amdgpu_ring_fini(struct amdgpu_ring *ring)
  {
+	amd_sched_priority_ctr_fini(&ring->priority_ctr);
+
  	ring->ready = false;
 
  	if (ring->funcs->support_64bit_ptrs) {
  		amdgpu_wb_free_64bit(ring->adev, ring->cond_exe_offs);
  		amdgpu_wb_free_64bit(ring->adev, ring->fence_offs);
  		amdgpu_wb_free_64bit(ring->adev, ring->rptr_offs);
  		amdgpu_wb_free_64bit(ring->adev, ring->wptr_offs);
  	} else {
  		amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
  		amdgpu_wb_free(ring->adev, ring->fence_offs);
  		amdgpu_wb_free(ring->adev, ring->rptr_offs);
  		amdgpu_wb_free(ring->adev, ring->wptr_offs);
  	}
 
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -5
index 7348769..2a04c0d 100644
@@ -147,30 +147,31 @@ struct amdgpu_ring_funcs {
  	void (*emit_switch_buffer) (struct amdgpu_ring *ring);
  	void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
  	void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
  	void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
  	void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
  	/* priority functions */
  	void (*set_priority) (struct amdgpu_ring *ring,
  			      enum amd_sched_priority priority);
  };
 
  struct amdgpu_ring {
  	struct amdgpu_device		*adev;
  	const struct amdgpu_ring_funcs	*funcs;
  	struct amdgpu_fence_driver	fence_drv;
  	struct amd_gpu_scheduler	sched;
+	struct amd_sched_priority_ctr	priority_ctr;
  	struct list_head		lru_list;
 
  	struct amdgpu_bo	*ring_obj;
  	volatile uint32_t	*ring;
  	unsigned		rptr_offs;
  	u64			wptr;
  	u64			wptr_old;
  	unsigned		ring_size;
  	unsigned		max_dw;
  	int			count_dw;
  	uint64_t		gpu_addr;
  	uint64_t		ptr_mask;
  	uint32_t		buf_mask;
  	bool			ready;
  	u32			idx;
@@ -181,35 +182,30 @@ struct amdgpu_ring {
  	uint64_t                mqd_gpu_addr;
  	void                    *mqd_ptr;
  	uint64_t                eop_gpu_addr;
  	u32			doorbell_index;
  	bool			use_doorbell;
  	unsigned		wptr_offs;
  	unsigned		fence_offs;
  	uint64_t		current_ctx;
  	char			name[16];
  	unsigned		cond_exe_offs;
  	u64			cond_exe_gpu_addr;
  	volatile u32		*cond_exe_cpu_addr;
  	unsigned		vm_inv_eng;
  	bool			has_compute_vm_bug;
 
-	atomic_t		num_jobs[AMD_SCHED_PRIORITY_MAX];
-	struct mutex		priority_mutex;
-	/* protected by priority_mutex */
-	int			priority;
-
  #if defined(CONFIG_DEBUG_FS)
  	struct dentry *ent;
  #endif
  };
 
  int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
  void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
  void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
  void amdgpu_ring_commit(struct amdgpu_ring *ring);
  void amdgpu_ring_undo(struct amdgpu_ring *ring);
  void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
  			      enum amd_sched_priority priority);
  void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
  			      enum amd_sched_priority priority);
  int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +122 -0
index 38cea6f..a203736 100644
@@ -488,30 +488,152 @@ int amd_sched_job_init(struct amd_sched_job *job,
  	job->sched = sched;
  	job->s_entity = entity;
  	job->s_fence = amd_sched_fence_create(entity, owner);
  	if (!job->s_fence)
  		return -ENOMEM;
  	job->id = atomic64_inc_return(&sched->job_id_count);
 
  	INIT_WORK(&job->finish_work, amd_sched_job_finish);
  	INIT_LIST_HEAD(&job->node);
  	INIT_DELAYED_WORK(&job->work_tdr, amd_sched_job_timedout);
 
  	return 0;
  }
 
  /**
+ * Initialize a priority_ctr
+ *
+ * @p: priority_ctr to initialize
+ * @default_priority: priority to be used when no active requests exist
+ * @set_priority: callback for changing the underlying resource's priority
+ *
+ * The @set_priority callback will be invoked whenever a transition
+ * happens into a new priority state, e.g. NORMAL->HIGH or HIGH->NORMAL. It
+ * is guaranteed to be invoked exactly once per state transition.
+ *
+ */
+void amd_sched_priority_ctr_init(struct amd_sched_priority_ctr *p,
+				 enum amd_sched_priority default_priority,
+				 amd_sched_set_priority_func_t set_priority)
+{
+	int i;
+
+	for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
+		atomic_set(&p->requests[i], 0);
+
+	p->default_priority = default_priority;
+	p->priority = p->default_priority;
+	p->set_priority = set_priority;
+	mutex_init(&p->mutex);
+}
+
+/**
+ * Cleanup a priority_ctr
+ *
+ * @p: priority_ctr to cleanup
+ *
+ * Must be called when the priority_ctr is no longer needed
+ */
+void amd_sched_priority_ctr_fini(struct amd_sched_priority_ctr *p)
+{
+	p->priority = AMD_SCHED_PRIORITY_INVALID;
+}
+
+/**
+ * Begin a request to set the current priority to @priority
+ *
+ * @p: priority_ctr
+ * @priority: requested priority
+ *
+ * Requests are reference counted.
+ *
+ * The current priority will be the highest priority for which an
+ * active request exists.
+ *
+ * If no active requests exist, the resource will default to
+ * p->default_priority
+ */
+void amd_sched_priority_ctr_get(struct amd_sched_priority_ctr *p,
+				enum amd_sched_priority priority)
+{
+	if (WARN_ON(priority > AMD_SCHED_PRIORITY_MAX
+			|| priority < AMD_SCHED_PRIORITY_MIN))
+		return;
+
+	if (atomic_inc_return(&p->requests[priority]) > 1)
+		return;
+
+	mutex_lock(&p->mutex);
+
+	/* A request with higher priority already exists */
+	if (priority <= p->priority)
+		goto out_unlock;
+
+	p->set_priority(p, priority);
+	p->priority = priority;
+
+out_unlock:
+	mutex_unlock(&p->mutex);
+}
+
+/**
+ * End a request to set the current priority to @priority
+ *
+ * @p: priority_ctr
+ * @priority: requested priority
+ *
+ * Refer to @amd_sched_priority_ctr_get for more details.
+ */
+void amd_sched_priority_ctr_put(struct amd_sched_priority_ctr *p,
+				enum amd_sched_priority priority)
+{
+	int i;
+	enum amd_sched_priority decayed_priority = AMD_SCHED_PRIORITY_INVALID;
+
+	if (WARN_ON(priority > AMD_SCHED_PRIORITY_MAX
+			|| priority < AMD_SCHED_PRIORITY_MIN))
+		return;
+
+	if (atomic_dec_return(&p->requests[priority]) > 0)
+		return;
+
+	mutex_lock(&p->mutex);
+
+	/* something higher prio is executing, no need to decay */
+	if (p->priority > priority)
+		goto out_unlock;
+
+	/* decay priority to the next level with an active request */
+	for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
+		if (atomic_read(&p->requests[i])) {
+			decayed_priority = i;
+			break;
+		}
+	}
+
+	/* If no requests are active, use the default priority */
+	if (decayed_priority == AMD_SCHED_PRIORITY_INVALID)
+		decayed_priority = p->default_priority;
+
+	p->set_priority(p, decayed_priority);
+	p->priority = decayed_priority;
+
+out_unlock:
+	mutex_unlock(&p->mutex);
+}
+
+/**
   * Return ture if we can push more jobs to the hw.
   */
  static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  {
  	return atomic_read(&sched->hw_rq_count) <
  		sched->hw_submission_limit;
  }
 
  /**
   * Wake up the scheduler when it is ready
   */
  static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  {
  	if (amd_sched_ready(sched))
  		wake_up_interruptible(&sched->wake_up_worker);
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +12 -0
index da040bc..b9283b5 100644
@@ -17,30 +17,31 @@
   * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   * OTHER DEALINGS IN THE SOFTWARE.
   *
   */
 
  #ifndef _GPU_SCHEDULER_H_
  #define _GPU_SCHEDULER_H_
 
  #include <linux/kfifo.h>
  #include <linux/dma-fence.h>
 
  struct amd_gpu_scheduler;
  struct amd_sched_rq;
+struct amd_sched_priority_ctr;
 
  /**
   * A scheduler entity is a wrapper around a job queue or a group
   * of other entities. Entities take turns emitting jobs from their
   * job queues to corresponding hardware ring based on scheduling
   * policy.
  */
  struct amd_sched_entity {
  	struct list_head		list;
  	struct amd_sched_rq		*rq;
  	struct amd_gpu_scheduler	*sched;
 
  	spinlock_t			queue_lock;
  	struct kfifo                    job_queue;
 
@@ -112,30 +113,41 @@ struct amd_sched_backend_ops {
  	void (*timedout_job)(struct amd_sched_job *sched_job);
  	void (*free_job)(struct amd_sched_job *sched_job);
  };
 
  enum amd_sched_priority {
  	AMD_SCHED_PRIORITY_MIN,
  	AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
  	AMD_SCHED_PRIORITY_NORMAL,
  	AMD_SCHED_PRIORITY_HIGH_SW,
  	AMD_SCHED_PRIORITY_HIGH_HW,
  	AMD_SCHED_PRIORITY_KERNEL,
  	AMD_SCHED_PRIORITY_MAX,
  	AMD_SCHED_PRIORITY_INVALID = -1
  };
 
+typedef void (*amd_sched_set_priority_func_t)(struct amd_sched_priority_ctr *p,
+					      enum amd_sched_priority priority);
+
+struct amd_sched_priority_ctr {
+	struct mutex			mutex;
+	atomic_t			requests[AMD_SCHED_PRIORITY_MAX];
+	enum amd_sched_priority		priority;
+	enum amd_sched_priority		default_priority;
+	amd_sched_set_priority_func_t	set_priority;
+};
+
  /**
   * One scheduler is implemented for each hardware ring
  */
  struct amd_gpu_scheduler {
  	const struct amd_sched_backend_ops	*ops;
  	uint32_t			hw_submission_limit;
  	long				timeout;
  	const char			*name;
  	struct amd_sched_rq		sched_rq[AMD_SCHED_PRIORITY_MAX];
  	wait_queue_head_t		wake_up_worker;
  	wait_queue_head_t		job_scheduled;
  	atomic_t			hw_rq_count;
  	atomic64_t			job_id_count;
  	struct task_struct		*thread;
  	struct list_head	ring_mirror_list;
@@ -166,16 +178,25 @@ int amd_sched_job_init(struct amd_sched_job *job,
  		       struct amd_gpu_scheduler *sched,
  		       struct amd_sched_entity *entity,
  		       void *owner);
  void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
  void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
  bool amd_sched_dependency_optimized(struct dma_fence* fence,
  				    struct amd_sched_entity *entity);
  void amd_sched_job_kickout(struct amd_sched_job *s_job);
 
  static inline enum amd_sched_priority
  amd_sched_get_job_priority(struct amd_sched_job *job)
  {
  	return (job->s_entity->rq - job->sched->sched_rq);
  }
 

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h int amd_sched_job_init(struct amd_sched_job *job, · line 202

Quoted 10 lines
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +9 -0
+void amd_sched_priority_ctr_init(struct amd_sched_priority_ctr *p,
+				 enum amd_sched_priority default_priority,
+				 amd_sched_set_priority_func_t set_priority);
+void amd_sched_priority_ctr_fini(struct amd_sched_priority_ctr *p);
+void amd_sched_priority_ctr_get(struct amd_sched_priority_ctr *p,
+				enum amd_sched_priority priority);
+void amd_sched_priority_ctr_put(struct amd_sched_priority_ctr *p,
+				enum amd_sched_priority priority);
+
  #endif

_______________________________________________
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): 71de1089-f4c1-47c4-c82f-da15ac43506e-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#18 lore raw

Re: [PATCH 5/8] drm/amdgpu: move priority decay logic into amd_sched_priority_ctr

Quoted 12 lines · Christian König

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 8 lines

So that it can be re-used.

A priority counter can be used to track priority requests. If no active requests exists, the counter will default to ()->default_priority.

The re-factored version is now allowed to decay below NORMAL. This allows us to create requests that lower the priority. This didn't matter for rings, as no priority below NORMAL has an effect.

NAK to the whole approach. This handling is amdgpu specific and not related to the gpu scheduler in any way.

Hey Christian,

I moved this to gpu_scheduler.c since it's original purpose is to track the scheduler's min_priority. Sorry I didn't provide any context for that in the original cover letter.

In my previous patch, "[PATCH 3/3] drm/amdgpu: add a mechanism to acquire gpu exclusivity", the functions amd_sched_min_priority_get/put() are almost identical copies of amdgpu_ring_priority_get/put(). To remove the duplication I introduced amd_sched_priority_ctr.

I later realized that I could re-use the same mechanism to track the context priority changes if I added a default_priority parameter. It also has the added benefit of keeping the requests refcounted.

I agree the usage of amd_sched_priority_ctr seems a little overkill. I originally used the approach of combining a ctx->init_priority with a ctx->master_priority, and that was pretty simple. However, re-using a concept that was already implemented, instead of introducing a new one had its own arguments for simplicity as well.

There is also one theoretical future scenario where the refcounting could be useful. Most VR apps output a 'mirror' window to the system compositor. Therefore they are clients of the system compositor and the VR compositor simultaneously. If both compositors were to use the ctx_set_priority() API on this app, the second request would override the first. With amd_sched_priority_ctr we would honor the highest of the two requests. In combination with the ability to set a minimum required priority to schedule gpu work, we can potentially run into undesired consequences.

Anyways, I only have a slight preference for this approach. So if you'd like me to go back to the muxing of two priorities I'm happy to go for it (and move this patch to the followup series for min_priority tracking).

Regards,
Andres

Quoted 514 lines

Christian.

Quoted 512 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c      |  69 +++++----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h      |   6 +-
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.c | 122 
++++++++++++++++++++++++++
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  21 +++++
  4 files changed, 164 insertions(+), 54 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +10 -9
index 2d8b20a7..159ab0e 100644
@@ -142,115 +142,86 @@ void amdgpu_ring_commit(struct amdgpu_ring *ring)
  /**
   * amdgpu_ring_undo - reset the wptr
   *
   * @ring: amdgpu_ring structure holding ring information
   *
   * Reset the driver's copy of the wptr (all asics).
   */
  void amdgpu_ring_undo(struct amdgpu_ring *ring)
  {
      ring->wptr = ring->wptr_old;
      if (ring->funcs->end_use)
          ring->funcs->end_use(ring);
  }
+static void amdgpu_ring_priority_set(struct amd_sched_priority_ctr *p,
+                     enum amd_sched_priority priority)
+{
+    struct amdgpu_ring *ring = container_of(p, struct amdgpu_ring,
+                        priority_ctr);
+
+    if (ring->funcs->set_priority)
+        ring->funcs->set_priority(ring, priority);
+}
+
  /**
   * amdgpu_ring_priority_put - restore a ring's priority
   *
   * @ring: amdgpu_ring structure holding the information
   * @priority: target priority
   *
   * Release a request for executing at @priority
   */
  void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority)
  {
-    int i;
-
-    if (!ring->funcs->set_priority)
-        return;
-
-    if (atomic_dec_return(&ring->num_jobs[priority]) > 0)
-        return;
-
-    /* no need to restore if the job is already at the lowest 

priority */

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +4 -34
-    if (priority == AMD_SCHED_PRIORITY_NORMAL)
-        return;
-
-    mutex_lock(&ring->priority_mutex);
-    /* something higher prio is executing, no need to decay */
-    if (ring->priority > priority)
-        goto out_unlock;
-
-    /* decay priority to the next level with a job available */
-    for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
-        if (i == AMD_SCHED_PRIORITY_NORMAL
-                || atomic_read(&ring->num_jobs[i])) {
-            ring->priority = i;
-            ring->funcs->set_priority(ring, i);
-            break;
-        }
-    }
-
-out_unlock:
-    mutex_unlock(&ring->priority_mutex);
+    if (ring->funcs->set_priority)
+        amd_sched_priority_ctr_put(&ring->priority_ctr, priority);
  }
  /**
   * amdgpu_ring_priority_get - change the ring's priority
   *
   * @ring: amdgpu_ring structure holding the information
   * @priority: target priority
   *
   * Request a ring's priority to be raised to @priority (refcounted).
   */
  void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority)
  {
-    if (!ring->funcs->set_priority)
-        return;
-
-    atomic_inc(&ring->num_jobs[priority]);
-
-    mutex_lock(&ring->priority_mutex);
-    if (priority <= ring->priority)
-        goto out_unlock;
-
-    ring->priority = priority;
-    ring->funcs->set_priority(ring, priority);
-
-out_unlock:
-    mutex_unlock(&ring->priority_mutex);
+    if (ring->funcs->set_priority)
+        amd_sched_priority_ctr_get(&ring->priority_ctr, priority);
  }
  /**
   * amdgpu_ring_init - init driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   * @max_ndw: maximum number of dw for ring alloc
   * @nop: nop packet for this ring
   *
   * Initialize the driver information for the selected ring (all asics).
   * Returns 0 on success, error on failure.
   */
  int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring 

*ring,
unsigned max_dw, struct amdgpu_irq_src *irq_src, unsigned irq_type)
{

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +6 -6
-    int r, i;
+    int r;
      if (ring->adev == NULL) {
          if (adev->num_rings >= AMDGPU_MAX_RINGS)
              return -EINVAL;
          ring->adev = adev;
          ring->idx = adev->num_rings++;
          adev->rings[ring->idx] = ring;
          r = amdgpu_fence_driver_init_ring(ring,
              amdgpu_sched_hw_submission);
          if (r)
              return r;
      }
      if (ring->funcs->support_64bit_ptrs) {
@@ -312,55 +283,55 @@ int amdgpu_ring_init(struct amdgpu_device *adev, 
 struct amdgpu_ring *ring,
      /* Allocate ring buffer */
      if (ring->ring_obj == NULL) {
          r = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
                          AMDGPU_GEM_DOMAIN_GTT,
                          &ring->ring_obj,
                          &ring->gpu_addr,
                          (void **)&ring->ring);
          if (r) {
              dev_err(adev->dev, "(%d) ring create failed\n", r);
              return r;
          }
          amdgpu_ring_clear_ring(ring);
      }
      ring->max_dw = max_dw;
-    ring->priority = AMD_SCHED_PRIORITY_NORMAL;
-    mutex_init(&ring->priority_mutex);
      INIT_LIST_HEAD(&ring->lru_list);
      amdgpu_ring_lru_touch(adev, ring);
-
-    for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
-        atomic_set(&ring->num_jobs[i], 0);
+    amd_sched_priority_ctr_init(&ring->priority_ctr,
+                    AMD_SCHED_PRIORITY_NORMAL,
+                    amdgpu_ring_priority_set);
      if (amdgpu_debugfs_ring_init(adev, ring)) {
          DRM_ERROR("Failed to register debugfs file for rings !\n");
      }
      return 0;
  }
  /**
   * amdgpu_ring_fini - tear down the driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   *
   * Tear down the driver information for the selected ring (all asics).
   */
  void amdgpu_ring_fini(struct amdgpu_ring *ring)
  {
+    amd_sched_priority_ctr_fini(&ring->priority_ctr);
+
      ring->ready = false;
      if (ring->funcs->support_64bit_ptrs) {
          amdgpu_wb_free_64bit(ring->adev, ring->cond_exe_offs);
          amdgpu_wb_free_64bit(ring->adev, ring->fence_offs);
          amdgpu_wb_free_64bit(ring->adev, ring->rptr_offs);
          amdgpu_wb_free_64bit(ring->adev, ring->wptr_offs);
      } else {
          amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
          amdgpu_wb_free(ring->adev, ring->fence_offs);
          amdgpu_wb_free(ring->adev, ring->rptr_offs);
          amdgpu_wb_free(ring->adev, ring->wptr_offs);
      }
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -5
index 7348769..2a04c0d 100644
@@ -147,30 +147,31 @@ struct amdgpu_ring_funcs {
      void (*emit_switch_buffer) (struct amdgpu_ring *ring);
      void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
      void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
      void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, 
 uint32_t val);
      void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
      /* priority functions */
      void (*set_priority) (struct amdgpu_ring *ring,
                    enum amd_sched_priority priority);
  };
  struct amdgpu_ring {
      struct amdgpu_device        *adev;
      const struct amdgpu_ring_funcs    *funcs;
      struct amdgpu_fence_driver    fence_drv;
      struct amd_gpu_scheduler    sched;
+    struct amd_sched_priority_ctr    priority_ctr;
      struct list_head        lru_list;
      struct amdgpu_bo    *ring_obj;
      volatile uint32_t    *ring;
      unsigned        rptr_offs;
      u64            wptr;
      u64            wptr_old;
      unsigned        ring_size;
      unsigned        max_dw;
      int            count_dw;
      uint64_t        gpu_addr;
      uint64_t        ptr_mask;
      uint32_t        buf_mask;
      bool            ready;
      u32            idx;
@@ -181,35 +182,30 @@ struct amdgpu_ring {
      uint64_t                mqd_gpu_addr;
      void                    *mqd_ptr;
      uint64_t                eop_gpu_addr;
      u32            doorbell_index;
      bool            use_doorbell;
      unsigned        wptr_offs;
      unsigned        fence_offs;
      uint64_t        current_ctx;
      char            name[16];
      unsigned        cond_exe_offs;
      u64            cond_exe_gpu_addr;
      volatile u32        *cond_exe_cpu_addr;
      unsigned        vm_inv_eng;
      bool            has_compute_vm_bug;
-    atomic_t        num_jobs[AMD_SCHED_PRIORITY_MAX];
-    struct mutex        priority_mutex;
-    /* protected by priority_mutex */
-    int            priority;
-
  #if defined(CONFIG_DEBUG_FS)
      struct dentry *ent;
  #endif
  };
  int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
  void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
  void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct 
 amdgpu_ib *ib);
  void amdgpu_ring_commit(struct amdgpu_ring *ring);
  void amdgpu_ring_undo(struct amdgpu_ring *ring);
  void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority);
  void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority);
  int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring 
 *ring,
drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +5 -0
index 38cea6f..a203736 100644
@@ -488,30 +488,152 @@ int amd_sched_job_init(struct amd_sched_job *job,
      job->sched = sched;
      job->s_entity = entity;
      job->s_fence = amd_sched_fence_create(entity, owner);
      if (!job->s_fence)
          return -ENOMEM;
      job->id = atomic64_inc_return(&sched->job_id_count);
      INIT_WORK(&job->finish_work, amd_sched_job_finish);
      INIT_LIST_HEAD(&job->node);
      INIT_DELAYED_WORK(&job->work_tdr, amd_sched_job_timedout);
      return 0;
  }
  /**
+ * Initialize a priority_ctr
+ *
+ * @p: priority_ctr to initialize
+ * @default_priority: priority to be used when no active requests exist
+ * @set_priority: callback for changing the underlying resource's 

priority
+ *
+ * The @set_priority callback will be invoked whenever a transition
+ * happens into a new priority state, e.g. NORMAL->HIGH or HIGH->NORMAL. It
+ * is guaranteed to be invoked exactly once per state transition.
+ *
+ */

drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +77 -0
+void amd_sched_priority_ctr_init(struct amd_sched_priority_ctr *p,
+                 enum amd_sched_priority default_priority,
+                 amd_sched_set_priority_func_t set_priority)
+{
+    int i;
+
+    for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
+        atomic_set(&p->requests[i], 0);
+
+    p->default_priority = default_priority;
+    p->priority = p->default_priority;
+    p->set_priority = set_priority;
+    mutex_init(&p->mutex);
+}
+
+/**
+ * Cleanup a priority_ctr
+ *
+ * @p: priority_ctr to cleanup
+ *
+ * Must be called when the priority_ctr is no longer needed
+ */
+void amd_sched_priority_ctr_fini(struct amd_sched_priority_ctr *p)
+{
+    p->priority = AMD_SCHED_PRIORITY_INVALID;
+}
+
+/**
+ * Begin a request to set the current priority to @priority
+ *
+ * @p: priority_ctr
+ * @priority: requested priority
+ *
+ * Requests are reference counted.
+ *
+ * The current priority will be the highest priority for which an
+ * active request exists.
+ *
+ * If no active requests exist, the resource will default to
+ * p->default_priority
+ */
+void amd_sched_priority_ctr_get(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority)
+{
+    if (WARN_ON(priority > AMD_SCHED_PRIORITY_MAX
+            || priority < AMD_SCHED_PRIORITY_MIN))
+        return;
+
+    if (atomic_inc_return(&p->requests[priority]) > 1)
+        return;
+
+    mutex_lock(&p->mutex);
+
+    /* A request with higher priority already exists */
+    if (priority <= p->priority)
+        goto out_unlock;
+
+    p->set_priority(p, priority);
+    p->priority = priority;
+
+out_unlock:
+    mutex_unlock(&p->mutex);
+}
+
+/**
+ * End a request to set the current priority to @priority
+ *
+ * @p: priority_ctr
+ * @priority: requested priority
+ *
+ * Refer to @amd_sched_priority_ctr_get for more details.
+ */
+void amd_sched_priority_ctr_put(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority)
+{
+    int i;
+    enum amd_sched_priority decayed_priority = 

AMD_SCHED_PRIORITY_INVALID;

drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +34 -0
+
+    if (WARN_ON(priority > AMD_SCHED_PRIORITY_MAX
+            || priority < AMD_SCHED_PRIORITY_MIN))
+        return;
+
+    if (atomic_dec_return(&p->requests[priority]) > 0)
+        return;
+
+    mutex_lock(&p->mutex);
+
+    /* something higher prio is executing, no need to decay */
+    if (p->priority > priority)
+        goto out_unlock;
+
+    /* decay priority to the next level with an active request */
+    for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
+        if (atomic_read(&p->requests[i])) {
+            decayed_priority = i;
+            break;
+        }
+    }
+
+    /* If no requests are active, use the default priority */
+    if (decayed_priority == AMD_SCHED_PRIORITY_INVALID)
+        decayed_priority = p->default_priority;
+
+    p->set_priority(p, decayed_priority);
+    p->priority = decayed_priority;
+
+out_unlock:
+    mutex_unlock(&p->mutex);
+}
+
+/**
   * Return ture if we can push more jobs to the hw.
   */
  static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  {
      return atomic_read(&sched->hw_rq_count) <
          sched->hw_submission_limit;
  }
  /**
   * Wake up the scheduler when it is ready
   */
  static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  {
      if (amd_sched_ready(sched))
          wake_up_interruptible(&sched->wake_up_worker);
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +0 -0
index da040bc..b9283b5 100644
@@ -17,30 +17,31 @@
   * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, 

DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _GPU_SCHEDULER_H_
#define _GPU_SCHEDULER_H_
#include <linux/kfifo.h>
#include <linux/dma-fence.h>
struct amd_gpu_scheduler;
struct amd_sched_rq;

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +14 -0
+struct amd_sched_priority_ctr;
  /**
   * A scheduler entity is a wrapper around a job queue or a group
   * of other entities. Entities take turns emitting jobs from their
   * job queues to corresponding hardware ring based on scheduling
   * policy.
  */
  struct amd_sched_entity {
      struct list_head        list;
      struct amd_sched_rq        *rq;
      struct amd_gpu_scheduler    *sched;
      spinlock_t            queue_lock;
      struct kfifo                    job_queue;
@@ -112,30 +113,41 @@ struct amd_sched_backend_ops {
      void (*timedout_job)(struct amd_sched_job *sched_job);
      void (*free_job)(struct amd_sched_job *sched_job);
  };
  enum amd_sched_priority {
      AMD_SCHED_PRIORITY_MIN,
      AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
      AMD_SCHED_PRIORITY_NORMAL,
      AMD_SCHED_PRIORITY_HIGH_SW,
      AMD_SCHED_PRIORITY_HIGH_HW,
      AMD_SCHED_PRIORITY_KERNEL,
      AMD_SCHED_PRIORITY_MAX,
      AMD_SCHED_PRIORITY_INVALID = -1
  };
+typedef void (*amd_sched_set_priority_func_t)(struct 
 amd_sched_priority_ctr *p,
+                          enum amd_sched_priority priority);
+
+struct amd_sched_priority_ctr {
+    struct mutex            mutex;
+    atomic_t            requests[AMD_SCHED_PRIORITY_MAX];
+    enum amd_sched_priority        priority;
+    enum amd_sched_priority        default_priority;
+    amd_sched_set_priority_func_t    set_priority;
+};
+
  /**
   * One scheduler is implemented for each hardware ring
  */
  struct amd_gpu_scheduler {
      const struct amd_sched_backend_ops    *ops;
      uint32_t            hw_submission_limit;
      long                timeout;
      const char            *name;
      struct amd_sched_rq        sched_rq[AMD_SCHED_PRIORITY_MAX];
      wait_queue_head_t        wake_up_worker;
      wait_queue_head_t        job_scheduled;
      atomic_t            hw_rq_count;
      atomic64_t            job_id_count;
      struct task_struct        *thread;
      struct list_head    ring_mirror_list;
@@ -166,16 +178,25 @@ int amd_sched_job_init(struct amd_sched_job *job,
                 struct amd_gpu_scheduler *sched,
                 struct amd_sched_entity *entity,
                 void *owner);
  void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
  void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
  bool amd_sched_dependency_optimized(struct dma_fence* fence,
                      struct amd_sched_entity *entity);
  void amd_sched_job_kickout(struct amd_sched_job *s_job);
  static inline enum amd_sched_priority
  amd_sched_get_job_priority(struct amd_sched_job *job)
  {
      return (job->s_entity->rq - job->sched->sched_rq);
  }
+void amd_sched_priority_ctr_init(struct amd_sched_priority_ctr *p,
+                 enum amd_sched_priority default_priority,
Quoted 10 lines

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h int amd_sched_job_init(struct amd_sched_job *job, · line 202

Quoted 8 lines
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +7 -0
+                 amd_sched_set_priority_func_t set_priority);
+void amd_sched_priority_ctr_fini(struct amd_sched_priority_ctr *p);
+void amd_sched_priority_ctr_get(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority);
+void amd_sched_priority_ctr_put(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority);
+
  #endif

_______________________________________________
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): ea42b3d8-cc3f-88f7-7c3d-46cf6dacf0a7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#20 lore raw

Re: [PATCH 5/8] drm/amdgpu: move priority decay logic into amd_sched_priority_ctr

Quoted 22 lines · Andres Rodriguez
Quoted 12 lines · Christian König

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 8 lines

So that it can be re-used.

A priority counter can be used to track priority requests. If no active requests exists, the counter will default to ()->default_priority.

The re-factored version is now allowed to decay below NORMAL. This allows us to create requests that lower the priority. This didn't matter for rings, as no priority below NORMAL has an effect.

NAK to the whole approach. This handling is amdgpu specific and not related to the gpu scheduler in any way.

Hey Christian,

I moved this to gpu_scheduler.c since it's original purpose is to track the scheduler's min_priority. Sorry I didn't provide any context for that in the original cover letter.

In my previous patch, "[PATCH 3/3] drm/amdgpu: add a mechanism to acquire gpu exclusivity", the functions amd_sched_min_priority_get/put() are almost identical copies of amdgpu_ring_priority_get/put(). To remove the duplication I introduced amd_sched_priority_ctr.

Quoted 8 lines

I later realized that I could re-use the same mechanism to track the context priority changes if I added a default_priority parameter. It also has the added benefit of keeping the requests refcounted.

I agree the usage of amd_sched_priority_ctr seems a little overkill. I originally used the approach of combining a ctx->init_priority with a ctx->master_priority, and that was pretty simple. However, re-using a concept that was already implemented, instead of introducing a new one had its own arguments for simplicity as well.

There is also one theoretical future scenario where the refcounting could be useful. Most VR apps output a 'mirror' window to the system compositor. Therefore they are clients of the system compositor and the VR compositor simultaneously. If both compositors were to use the ctx_set_priority() API on this app, the second request would override the first. With amd_sched_priority_ctr we would honor the highest of the two requests. In combination with the ability to set a minimum required priority to schedule gpu work, we can potentially run into undesired consequences.

Anyways, I only have a slight preference for this approach. So if you'd like me to go back to the muxing of two priorities I'm happy to go for it (and move this patch to the followup series for min_priority tracking).

Quick ping on the query above. The query can be summarized as: which ioctl interface do we prefer for the override?

* AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET/PUT - refcounted override

or

* AMDGPU_SCHED_OP_PROCESS_PRIORITY_SET - simple set

Regards,
Andres

Quoted 528 lines

Regards,
Andres

Quoted 525 lines

Christian.

Quoted 521 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c      |  69 +++++----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h      |   6 +-
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.c | 122 
++++++++++++++++++++++++++
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  21 +++++
  4 files changed, 164 insertions(+), 54 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +10 -9
index 2d8b20a7..159ab0e 100644
@@ -142,115 +142,86 @@ void amdgpu_ring_commit(struct amdgpu_ring *ring)
  /**
   * amdgpu_ring_undo - reset the wptr
   *
   * @ring: amdgpu_ring structure holding ring information
   *
   * Reset the driver's copy of the wptr (all asics).
   */
  void amdgpu_ring_undo(struct amdgpu_ring *ring)
  {
      ring->wptr = ring->wptr_old;
      if (ring->funcs->end_use)
          ring->funcs->end_use(ring);
  }
+static void amdgpu_ring_priority_set(struct amd_sched_priority_ctr *p,
+                     enum amd_sched_priority priority)
+{
+    struct amdgpu_ring *ring = container_of(p, struct amdgpu_ring,
+                        priority_ctr);
+
+    if (ring->funcs->set_priority)
+        ring->funcs->set_priority(ring, priority);
+}
+
  /**
   * amdgpu_ring_priority_put - restore a ring's priority
   *
   * @ring: amdgpu_ring structure holding the information
   * @priority: target priority
   *
   * Release a request for executing at @priority
   */
  void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority)
  {
-    int i;
-
-    if (!ring->funcs->set_priority)
-        return;
-
-    if (atomic_dec_return(&ring->num_jobs[priority]) > 0)
-        return;
-
-    /* no need to restore if the job is already at the lowest 

priority */

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +4 -34
-    if (priority == AMD_SCHED_PRIORITY_NORMAL)
-        return;
-
-    mutex_lock(&ring->priority_mutex);
-    /* something higher prio is executing, no need to decay */
-    if (ring->priority > priority)
-        goto out_unlock;
-
-    /* decay priority to the next level with a job available */
-    for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
-        if (i == AMD_SCHED_PRIORITY_NORMAL
-                || atomic_read(&ring->num_jobs[i])) {
-            ring->priority = i;
-            ring->funcs->set_priority(ring, i);
-            break;
-        }
-    }
-
-out_unlock:
-    mutex_unlock(&ring->priority_mutex);
+    if (ring->funcs->set_priority)
+        amd_sched_priority_ctr_put(&ring->priority_ctr, priority);
  }
  /**
   * amdgpu_ring_priority_get - change the ring's priority
   *
   * @ring: amdgpu_ring structure holding the information
   * @priority: target priority
   *
   * Request a ring's priority to be raised to @priority (refcounted).
   */
  void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority)
  {
-    if (!ring->funcs->set_priority)
-        return;
-
-    atomic_inc(&ring->num_jobs[priority]);
-
-    mutex_lock(&ring->priority_mutex);
-    if (priority <= ring->priority)
-        goto out_unlock;
-
-    ring->priority = priority;
-    ring->funcs->set_priority(ring, priority);
-
-out_unlock:
-    mutex_unlock(&ring->priority_mutex);
+    if (ring->funcs->set_priority)
+        amd_sched_priority_ctr_get(&ring->priority_ctr, priority);
  }
  /**
   * amdgpu_ring_init - init driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   * @max_ndw: maximum number of dw for ring alloc
   * @nop: nop packet for this ring
   *
   * Initialize the driver information for the selected ring (all 

asics).
* Returns 0 on success, error on failure.
*/
int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring
*ring,
unsigned max_dw, struct amdgpu_irq_src *irq_src, unsigned irq_type)
{

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +6 -6
-    int r, i;
+    int r;
      if (ring->adev == NULL) {
          if (adev->num_rings >= AMDGPU_MAX_RINGS)
              return -EINVAL;
          ring->adev = adev;
          ring->idx = adev->num_rings++;
          adev->rings[ring->idx] = ring;
          r = amdgpu_fence_driver_init_ring(ring,
              amdgpu_sched_hw_submission);
          if (r)
              return r;
      }
      if (ring->funcs->support_64bit_ptrs) {
@@ -312,55 +283,55 @@ int amdgpu_ring_init(struct amdgpu_device 
 *adev, struct amdgpu_ring *ring,
      /* Allocate ring buffer */
      if (ring->ring_obj == NULL) {
          r = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
                          AMDGPU_GEM_DOMAIN_GTT,
                          &ring->ring_obj,
                          &ring->gpu_addr,
                          (void **)&ring->ring);
          if (r) {
              dev_err(adev->dev, "(%d) ring create failed\n", r);
              return r;
          }
          amdgpu_ring_clear_ring(ring);
      }
      ring->max_dw = max_dw;
-    ring->priority = AMD_SCHED_PRIORITY_NORMAL;
-    mutex_init(&ring->priority_mutex);
      INIT_LIST_HEAD(&ring->lru_list);
      amdgpu_ring_lru_touch(adev, ring);
-
-    for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
-        atomic_set(&ring->num_jobs[i], 0);
+    amd_sched_priority_ctr_init(&ring->priority_ctr,
+                    AMD_SCHED_PRIORITY_NORMAL,
+                    amdgpu_ring_priority_set);
      if (amdgpu_debugfs_ring_init(adev, ring)) {
          DRM_ERROR("Failed to register debugfs file for rings !\n");
      }
      return 0;
  }
  /**
   * amdgpu_ring_fini - tear down the driver ring struct.
   *
   * @adev: amdgpu_device pointer
   * @ring: amdgpu_ring structure holding ring information
   *
   * Tear down the driver information for the selected ring (all asics).
   */
  void amdgpu_ring_fini(struct amdgpu_ring *ring)
  {
+    amd_sched_priority_ctr_fini(&ring->priority_ctr);
+
      ring->ready = false;
      if (ring->funcs->support_64bit_ptrs) {
          amdgpu_wb_free_64bit(ring->adev, ring->cond_exe_offs);
          amdgpu_wb_free_64bit(ring->adev, ring->fence_offs);
          amdgpu_wb_free_64bit(ring->adev, ring->rptr_offs);
          amdgpu_wb_free_64bit(ring->adev, ring->wptr_offs);
      } else {
          amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
          amdgpu_wb_free(ring->adev, ring->fence_offs);
          amdgpu_wb_free(ring->adev, ring->rptr_offs);
          amdgpu_wb_free(ring->adev, ring->wptr_offs);
      }
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -5
index 7348769..2a04c0d 100644
@@ -147,30 +147,31 @@ struct amdgpu_ring_funcs {
      void (*emit_switch_buffer) (struct amdgpu_ring *ring);
      void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
      void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
      void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, 
 uint32_t val);
      void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
      /* priority functions */
      void (*set_priority) (struct amdgpu_ring *ring,
                    enum amd_sched_priority priority);
  };
  struct amdgpu_ring {
      struct amdgpu_device        *adev;
      const struct amdgpu_ring_funcs    *funcs;
      struct amdgpu_fence_driver    fence_drv;
      struct amd_gpu_scheduler    sched;
+    struct amd_sched_priority_ctr    priority_ctr;
      struct list_head        lru_list;
      struct amdgpu_bo    *ring_obj;
      volatile uint32_t    *ring;
      unsigned        rptr_offs;
      u64            wptr;
      u64            wptr_old;
      unsigned        ring_size;
      unsigned        max_dw;
      int            count_dw;
      uint64_t        gpu_addr;
      uint64_t        ptr_mask;
      uint32_t        buf_mask;
      bool            ready;
      u32            idx;
@@ -181,35 +182,30 @@ struct amdgpu_ring {
      uint64_t                mqd_gpu_addr;
      void                    *mqd_ptr;
      uint64_t                eop_gpu_addr;
      u32            doorbell_index;
      bool            use_doorbell;
      unsigned        wptr_offs;
      unsigned        fence_offs;
      uint64_t        current_ctx;
      char            name[16];
      unsigned        cond_exe_offs;
      u64            cond_exe_gpu_addr;
      volatile u32        *cond_exe_cpu_addr;
      unsigned        vm_inv_eng;
      bool            has_compute_vm_bug;
-    atomic_t        num_jobs[AMD_SCHED_PRIORITY_MAX];
-    struct mutex        priority_mutex;
-    /* protected by priority_mutex */
-    int            priority;
-
  #if defined(CONFIG_DEBUG_FS)
      struct dentry *ent;
  #endif
  };
  int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
  void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
  void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct 
 amdgpu_ib *ib);
  void amdgpu_ring_commit(struct amdgpu_ring *ring);
  void amdgpu_ring_undo(struct amdgpu_ring *ring);
  void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority);
  void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
                    enum amd_sched_priority priority);
  int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring 
 *ring,
drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +5 -0
index 38cea6f..a203736 100644
@@ -488,30 +488,152 @@ int amd_sched_job_init(struct amd_sched_job *job,
      job->sched = sched;
      job->s_entity = entity;
      job->s_fence = amd_sched_fence_create(entity, owner);
      if (!job->s_fence)
          return -ENOMEM;
      job->id = atomic64_inc_return(&sched->job_id_count);
      INIT_WORK(&job->finish_work, amd_sched_job_finish);
      INIT_LIST_HEAD(&job->node);
      INIT_DELAYED_WORK(&job->work_tdr, amd_sched_job_timedout);
      return 0;
  }
  /**
+ * Initialize a priority_ctr
+ *
+ * @p: priority_ctr to initialize
+ * @default_priority: priority to be used when no active requests exist
+ * @set_priority: callback for changing the underlying resource's 

priority
+ *
+ * The @set_priority callback will be invoked whenever a transition
+ * happens into a new priority state, e.g. NORMAL->HIGH or HIGH->NORMAL. It
+ * is guaranteed to be invoked exactly once per state transition.
+ *
+ */

drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +77 -0
+void amd_sched_priority_ctr_init(struct amd_sched_priority_ctr *p,
+                 enum amd_sched_priority default_priority,
+                 amd_sched_set_priority_func_t set_priority)
+{
+    int i;
+
+    for (i = 0; i < AMD_SCHED_PRIORITY_MAX; ++i)
+        atomic_set(&p->requests[i], 0);
+
+    p->default_priority = default_priority;
+    p->priority = p->default_priority;
+    p->set_priority = set_priority;
+    mutex_init(&p->mutex);
+}
+
+/**
+ * Cleanup a priority_ctr
+ *
+ * @p: priority_ctr to cleanup
+ *
+ * Must be called when the priority_ctr is no longer needed
+ */
+void amd_sched_priority_ctr_fini(struct amd_sched_priority_ctr *p)
+{
+    p->priority = AMD_SCHED_PRIORITY_INVALID;
+}
+
+/**
+ * Begin a request to set the current priority to @priority
+ *
+ * @p: priority_ctr
+ * @priority: requested priority
+ *
+ * Requests are reference counted.
+ *
+ * The current priority will be the highest priority for which an
+ * active request exists.
+ *
+ * If no active requests exist, the resource will default to
+ * p->default_priority
+ */
+void amd_sched_priority_ctr_get(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority)
+{
+    if (WARN_ON(priority > AMD_SCHED_PRIORITY_MAX
+            || priority < AMD_SCHED_PRIORITY_MIN))
+        return;
+
+    if (atomic_inc_return(&p->requests[priority]) > 1)
+        return;
+
+    mutex_lock(&p->mutex);
+
+    /* A request with higher priority already exists */
+    if (priority <= p->priority)
+        goto out_unlock;
+
+    p->set_priority(p, priority);
+    p->priority = priority;
+
+out_unlock:
+    mutex_unlock(&p->mutex);
+}
+
+/**
+ * End a request to set the current priority to @priority
+ *
+ * @p: priority_ctr
+ * @priority: requested priority
+ *
+ * Refer to @amd_sched_priority_ctr_get for more details.
+ */
+void amd_sched_priority_ctr_put(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority)
+{
+    int i;
+    enum amd_sched_priority decayed_priority = 

AMD_SCHED_PRIORITY_INVALID;

drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +34 -0
+
+    if (WARN_ON(priority > AMD_SCHED_PRIORITY_MAX
+            || priority < AMD_SCHED_PRIORITY_MIN))
+        return;
+
+    if (atomic_dec_return(&p->requests[priority]) > 0)
+        return;
+
+    mutex_lock(&p->mutex);
+
+    /* something higher prio is executing, no need to decay */
+    if (p->priority > priority)
+        goto out_unlock;
+
+    /* decay priority to the next level with an active request */
+    for (i = priority; i >= AMD_SCHED_PRIORITY_MIN; i--) {
+        if (atomic_read(&p->requests[i])) {
+            decayed_priority = i;
+            break;
+        }
+    }
+
+    /* If no requests are active, use the default priority */
+    if (decayed_priority == AMD_SCHED_PRIORITY_INVALID)
+        decayed_priority = p->default_priority;
+
+    p->set_priority(p, decayed_priority);
+    p->priority = decayed_priority;
+
+out_unlock:
+    mutex_unlock(&p->mutex);
+}
+
+/**
   * Return ture if we can push more jobs to the hw.
   */
  static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  {
      return atomic_read(&sched->hw_rq_count) <
          sched->hw_submission_limit;
  }
  /**
   * Wake up the scheduler when it is ready
   */
  static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  {
      if (amd_sched_ready(sched))
          wake_up_interruptible(&sched->wake_up_worker);
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +0 -0
index da040bc..b9283b5 100644
@@ -17,30 +17,31 @@
   * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, 

DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _GPU_SCHEDULER_H_
#define _GPU_SCHEDULER_H_
#include <linux/kfifo.h>
#include <linux/dma-fence.h>
struct amd_gpu_scheduler;
struct amd_sched_rq;

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +21 -0
+struct amd_sched_priority_ctr;
  /**
   * A scheduler entity is a wrapper around a job queue or a group
   * of other entities. Entities take turns emitting jobs from their
   * job queues to corresponding hardware ring based on scheduling
   * policy.
  */
  struct amd_sched_entity {
      struct list_head        list;
      struct amd_sched_rq        *rq;
      struct amd_gpu_scheduler    *sched;
      spinlock_t            queue_lock;
      struct kfifo                    job_queue;
@@ -112,30 +113,41 @@ struct amd_sched_backend_ops {
      void (*timedout_job)(struct amd_sched_job *sched_job);
      void (*free_job)(struct amd_sched_job *sched_job);
  };
  enum amd_sched_priority {
      AMD_SCHED_PRIORITY_MIN,
      AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
      AMD_SCHED_PRIORITY_NORMAL,
      AMD_SCHED_PRIORITY_HIGH_SW,
      AMD_SCHED_PRIORITY_HIGH_HW,
      AMD_SCHED_PRIORITY_KERNEL,
      AMD_SCHED_PRIORITY_MAX,
      AMD_SCHED_PRIORITY_INVALID = -1
  };
+typedef void (*amd_sched_set_priority_func_t)(struct 
 amd_sched_priority_ctr *p,
+                          enum amd_sched_priority priority);
+
+struct amd_sched_priority_ctr {
+    struct mutex            mutex;
+    atomic_t            requests[AMD_SCHED_PRIORITY_MAX];
+    enum amd_sched_priority        priority;
+    enum amd_sched_priority        default_priority;
+    amd_sched_set_priority_func_t    set_priority;
+};
+
  /**
   * One scheduler is implemented for each hardware ring
  */
  struct amd_gpu_scheduler {
      const struct amd_sched_backend_ops    *ops;
      uint32_t            hw_submission_limit;
      long                timeout;
      const char            *name;
      struct amd_sched_rq        sched_rq[AMD_SCHED_PRIORITY_MAX];
      wait_queue_head_t        wake_up_worker;
      wait_queue_head_t        job_scheduled;
      atomic_t            hw_rq_count;
      atomic64_t            job_id_count;
      struct task_struct        *thread;
      struct list_head    ring_mirror_list;
@@ -166,16 +178,25 @@ int amd_sched_job_init(struct amd_sched_job *job,
                 struct amd_gpu_scheduler *sched,
                 struct amd_sched_entity *entity,
                 void *owner);
  void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
  void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
  bool amd_sched_dependency_optimized(struct dma_fence* fence,
                      struct amd_sched_entity *entity);
  void amd_sched_job_kickout(struct amd_sched_job *s_job);
  static inline enum amd_sched_priority
  amd_sched_get_job_priority(struct amd_sched_job *job)
  {
      return (job->s_entity->rq - job->sched->sched_rq);
  }
+void amd_sched_priority_ctr_init(struct amd_sched_priority_ctr *p,
+                 enum amd_sched_priority default_priority,
+                 amd_sched_set_priority_func_t set_priority);
+void amd_sched_priority_ctr_fini(struct amd_sched_priority_ctr *p);
+void amd_sched_priority_ctr_get(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority);
+void amd_sched_priority_ctr_put(struct amd_sched_priority_ctr *p,
+                enum amd_sched_priority priority);
+
  #endif

_______________________________________________
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): 5bbec90f-4746-c314-cca4-d33f4701615f-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#22 lore raw

Re: [PATCH 5/8] drm/amdgpu: move priority decay logic into amd_sched_priority_ctr

Sorry for the delay, back from a rather long sick leave today and trying to catch up with my work.

Quoted 7 lines

Quick ping on the query above. The query can be summarized as: which ioctl interface do we prefer for the override?

* AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET/PUT - refcounted override

or

* AMDGPU_SCHED_OP_PROCESS_PRIORITY_SET - simple set

Please keep it simple and stupid. No extra fiddling here, that stuff is complicated enough.

So I would strongly suggest to have a simple set interface which the DRM master can use to set the priority.

If we have multiple DRM masters which feel responsible for this they should to sync up in userspace what to do.

Regards,
Christian.
_______________________________________________
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): b86706a7-cdcd-8eaa-197a-9e78b2f16515-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#24 lore raw

Re: [PATCH 5/8] drm/amdgpu: move priority decay logic into amd_sched_priority_ctr

Quoted 2 lines · Christian König

Sorry for the delay, back from a rather long sick leave today and trying to catch up with my work.

Don't worry too much. Get well soon!

Quoted 6 lines
Quoted 6 lines

Quick ping on the query above. The query can be summarized as: which ioctl interface do we prefer for the override?

* AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET/PUT - refcounted override

or

Quoted 7 lines
Quoted 1 line

* AMDGPU_SCHED_OP_PROCESS_PRIORITY_SET - simple set

Please keep it simple and stupid. No extra fiddling here, that stuff is complicated enough.

So I would strongly suggest to have a simple set interface which the DRM master can use to set the priority.

If we have multiple DRM masters which feel responsible for this they should to sync up in userspace what to do.

Sounds good to me. I'll send out a _SET version of this series.

Regards,
Andres

Quoted 3 lines

Regards,
Christian.

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

#25 lore raw

PATCH 6/8 drm/amd/sched: allow clients to edit an entity's rq

In reply to a message not in the archive

This is useful for changing an entity's priority at runtime.

---
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.c | 26 +++++++++++++++++++++++---
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  6 +++++-
 2 files changed, 28 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +23 -3
index a203736..c19bb85 100644
@@ -121,30 +121,31 @@ amd_sched_rq_select_entity(struct amd_sched_rq *rq)
 int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
 			  struct amd_sched_entity *entity,
 			  struct amd_sched_rq *rq,
 			  uint32_t jobs)
 {
 	int r;
 
 	if (!(sched && entity && rq))
 		return -EINVAL;
 
 	memset(entity, 0, sizeof(struct amd_sched_entity));
 	INIT_LIST_HEAD(&entity->list);
 	entity->rq = rq;
 	entity->sched = sched;
 
+	spin_lock_init(&entity->rq_lock);
 	spin_lock_init(&entity->queue_lock);
 	r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
 	if (r)
 		return r;
 
 	atomic_set(&entity->fence_seq, 0);
 	entity->fence_context = dma_fence_context_alloc(2);
 
 	return 0;
 }
 
 /**
  * Query if entity is initialized
  *
  * @sched       Pointer to scheduler instance
@@ -192,62 +193,79 @@ static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
 
 	return true;
 }
 
 /**
  * Destroy a context entity
  *
  * @sched       Pointer to scheduler instance
  * @entity	The pointer to a valid scheduler entity
  *
  * Cleanup and free the allocated resources.
  */
 void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
 			   struct amd_sched_entity *entity)
 {
-	struct amd_sched_rq *rq = entity->rq;
-
 	if (!amd_sched_entity_is_initialized(sched, entity))
 		return;
 
 	/**
 	 * The client will not queue more IBs during this fini, consume existing
 	 * queued IBs
 	*/
 	wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
 
-	amd_sched_rq_remove_entity(rq, entity);
+	amd_sched_entity_set_rq(entity, NULL);
+
 	kfifo_free(&entity->job_queue);
 }
 
 static void amd_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
 {
 	struct amd_sched_entity *entity =
 		container_of(cb, struct amd_sched_entity, cb);
 	entity->dependency = NULL;
 	dma_fence_put(f);
 	amd_sched_wakeup(entity->sched);
 }
 
 static void amd_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
 {
 	struct amd_sched_entity *entity =
 		container_of(cb, struct amd_sched_entity, cb);
 	entity->dependency = NULL;
 	dma_fence_put(f);
 }
 
+void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
+			     struct amd_sched_rq *rq)
+{
+	if (entity->rq == rq)
+		return;
+
+	spin_lock(&entity->rq_lock);
+
+	if (entity->rq)
+		amd_sched_rq_remove_entity(entity->rq, entity);
+
+	entity->rq = rq;
+	if (rq)
+		amd_sched_rq_add_entity(rq, entity);
+
+	spin_unlock(&entity->rq_lock);
+}
+
 bool amd_sched_dependency_optimized(struct dma_fence* fence,
 				    struct amd_sched_entity *entity)
 {
 	struct amd_gpu_scheduler *sched = entity->sched;
 	struct amd_sched_fence *s_fence;
 
 	if (!fence || dma_fence_is_signaled(fence))
 		return false;
 	if (fence->context == entity->fence_context)
 		return true;
 	s_fence = to_amd_sched_fence(fence);
 	if (s_fence && s_fence->sched == sched)
 		return true;
 
 	return false;
@@ -321,31 +339,33 @@ static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
 	struct amd_sched_entity *entity = sched_job->s_entity;
 	bool added, first = false;
 
 	spin_lock(&entity->queue_lock);
 	added = kfifo_in(&entity->job_queue, &sched_job,
 			sizeof(sched_job)) == sizeof(sched_job);
 
 	if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
 		first = true;
 
 	spin_unlock(&entity->queue_lock);
 
 	/* first job wakes up scheduler */
 	if (first) {
 		/* Add the entity to the run queue */
+		spin_lock(&entity->rq_lock);
 		amd_sched_rq_add_entity(entity->rq, entity);
+		spin_unlock(&entity->rq_lock);
 		amd_sched_wakeup(sched);
 	}
 	return added;
 }
 
 /* job_finish is called after hw fence signaled, and
  * the job had already been deleted from ring_mirror_list
  */
 static void amd_sched_job_finish(struct work_struct *work)
 {
 	struct amd_sched_job *s_job = container_of(work, struct amd_sched_job,
 						   finish_work);
 	struct amd_gpu_scheduler *sched = s_job->sched;
 
 	/* remove job from ring_mirror_list */
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +5 -1
index b9283b5..51d626d 100644
@@ -27,33 +27,35 @@
 #include <linux/kfifo.h>
 #include <linux/dma-fence.h>
 
 struct amd_gpu_scheduler;
 struct amd_sched_rq;
 struct amd_sched_priority_ctr;
 
 /**
  * A scheduler entity is a wrapper around a job queue or a group
  * of other entities. Entities take turns emitting jobs from their
  * job queues to corresponding hardware ring based on scheduling
  * policy.
 */
 struct amd_sched_entity {
 	struct list_head		list;
-	struct amd_sched_rq		*rq;
 	struct amd_gpu_scheduler	*sched;
 
+	spinlock_t			rq_lock;
+	struct amd_sched_rq		*rq;
+
 	spinlock_t			queue_lock;
 	struct kfifo                    job_queue;
 
 	atomic_t			fence_seq;
 	uint64_t                        fence_context;
 
 	struct dma_fence		*dependency;
 	struct dma_fence_cb		cb;
 };
 
 /**
  * Run queue is a set of entities scheduling command submissions for
  * one specific ring. It implements the scheduling policy that selects
  * the next entity to emit commands from.
 */
@@ -154,30 +156,32 @@ struct amd_gpu_scheduler {
 	spinlock_t			job_list_lock;
 };
 
 int amd_sched_init(struct amd_gpu_scheduler *sched,
 		   const struct amd_sched_backend_ops *ops,
 		   uint32_t hw_submission, long timeout, const char *name);
 void amd_sched_fini(struct amd_gpu_scheduler *sched);
 
 int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
 			  struct amd_sched_entity *entity,
 			  struct amd_sched_rq *rq,
 			  uint32_t jobs);
 void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
 			   struct amd_sched_entity *entity);
 void amd_sched_entity_push_job(struct amd_sched_job *sched_job);
+void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
+			     struct amd_sched_rq *rq);
 
 int amd_sched_fence_slab_init(void);
 void amd_sched_fence_slab_fini(void);
 
 struct amd_sched_fence *amd_sched_fence_create(
 	struct amd_sched_entity *s_entity, void *owner);
 void amd_sched_fence_scheduled(struct amd_sched_fence *fence);
 void amd_sched_fence_finished(struct amd_sched_fence *fence);
 int amd_sched_job_init(struct amd_sched_job *job,
 		       struct amd_gpu_scheduler *sched,
 		       struct amd_sched_entity *entity,
 		       void *owner);
 void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
 void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
 bool amd_sched_dependency_optimized(struct dma_fence* fence,
Signature · 6 lines
2.9.3

_______________________________________________
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): 20170608220623.9002-7-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#27 lore raw

Re: [PATCH 6/8] drm/amd/sched: allow clients to edit an entity's rq

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 177 lines

This is useful for changing an entity's priority at runtime.

---
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.c | 26 +++++++++++++++++++++++---
  drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  6 +++++-
  2 files changed, 28 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +23 -3
index a203736..c19bb85 100644
@@ -121,30 +121,31 @@ amd_sched_rq_select_entity(struct amd_sched_rq *rq)
  int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  			  struct amd_sched_entity *entity,
  			  struct amd_sched_rq *rq,
  			  uint32_t jobs)
  {
  	int r;
 
  	if (!(sched && entity && rq))
  		return -EINVAL;
 
  	memset(entity, 0, sizeof(struct amd_sched_entity));
  	INIT_LIST_HEAD(&entity->list);
  	entity->rq = rq;
  	entity->sched = sched;
 
+	spin_lock_init(&entity->rq_lock);
  	spin_lock_init(&entity->queue_lock);
  	r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
  	if (r)
  		return r;
 
  	atomic_set(&entity->fence_seq, 0);
  	entity->fence_context = dma_fence_context_alloc(2);
 
  	return 0;
  }
 
  /**
   * Query if entity is initialized
   *
   * @sched       Pointer to scheduler instance
@@ -192,62 +193,79 @@ static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
 
  	return true;
  }
 
  /**
   * Destroy a context entity
   *
   * @sched       Pointer to scheduler instance
   * @entity	The pointer to a valid scheduler entity
   *
   * Cleanup and free the allocated resources.
   */
  void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  			   struct amd_sched_entity *entity)
  {
-	struct amd_sched_rq *rq = entity->rq;
-
  	if (!amd_sched_entity_is_initialized(sched, entity))
  		return;
 
  	/**
  	 * The client will not queue more IBs during this fini, consume existing
  	 * queued IBs
  	*/
  	wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
 
-	amd_sched_rq_remove_entity(rq, entity);
+	amd_sched_entity_set_rq(entity, NULL);
+
  	kfifo_free(&entity->job_queue);
  }
 
  static void amd_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
  {
  	struct amd_sched_entity *entity =
  		container_of(cb, struct amd_sched_entity, cb);
  	entity->dependency = NULL;
  	dma_fence_put(f);
  	amd_sched_wakeup(entity->sched);
  }
 
  static void amd_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
  {
  	struct amd_sched_entity *entity =
  		container_of(cb, struct amd_sched_entity, cb);
  	entity->dependency = NULL;
  	dma_fence_put(f);
  }
 
+void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
+			     struct amd_sched_rq *rq)
+{
+	if (entity->rq == rq)
+		return;
+
+	spin_lock(&entity->rq_lock);
+
+	if (entity->rq)
+		amd_sched_rq_remove_entity(entity->rq, entity);
+
+	entity->rq = rq;
+	if (rq)
+		amd_sched_rq_add_entity(rq, entity);
+
+	spin_unlock(&entity->rq_lock);
+}
+
  bool amd_sched_dependency_optimized(struct dma_fence* fence,
  				    struct amd_sched_entity *entity)
  {
  	struct amd_gpu_scheduler *sched = entity->sched;
  	struct amd_sched_fence *s_fence;
 
  	if (!fence || dma_fence_is_signaled(fence))
  		return false;
  	if (fence->context == entity->fence_context)
  		return true;
  	s_fence = to_amd_sched_fence(fence);
  	if (s_fence && s_fence->sched == sched)
  		return true;
 
  	return false;
@@ -321,31 +339,33 @@ static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
  	struct amd_sched_entity *entity = sched_job->s_entity;
  	bool added, first = false;
 
  	spin_lock(&entity->queue_lock);
  	added = kfifo_in(&entity->job_queue, &sched_job,
  			sizeof(sched_job)) == sizeof(sched_job);
 
  	if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
  		first = true;
 
  	spin_unlock(&entity->queue_lock);
 
  	/* first job wakes up scheduler */
  	if (first) {
  		/* Add the entity to the run queue */
+		spin_lock(&entity->rq_lock);
  		amd_sched_rq_add_entity(entity->rq, entity);
+		spin_unlock(&entity->rq_lock);
  		amd_sched_wakeup(sched);
  	}
  	return added;
  }
 
  /* job_finish is called after hw fence signaled, and
   * the job had already been deleted from ring_mirror_list
   */
  static void amd_sched_job_finish(struct work_struct *work)
  {
  	struct amd_sched_job *s_job = container_of(work, struct amd_sched_job,
  						   finish_work);
  	struct amd_gpu_scheduler *sched = s_job->sched;
 
  	/* remove job from ring_mirror_list */
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +0 -0
index b9283b5..51d626d 100644
@@ -27,33 +27,35 @@
  #include <linux/kfifo.h>
  #include <linux/dma-fence.h>
 
  struct amd_gpu_scheduler;
  struct amd_sched_rq;
  struct amd_sched_priority_ctr;
 
  /**
   * A scheduler entity is a wrapper around a job queue or a group
   * of other entities. Entities take turns emitting jobs from their
   * job queues to corresponding hardware ring based on scheduling

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h line 46

Quoted 10 lines
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +3 -1
   * policy.
  */
  struct amd_sched_entity {
  	struct list_head		list;
-	struct amd_sched_rq		*rq;
  	struct amd_gpu_scheduler	*sched;
 
+	spinlock_t			rq_lock;
+	struct amd_sched_rq		*rq;
+

Keep the order here, the "list" memory belongs to the rq member.

Apart from that the patch looks good to me,
Christian.

Quoted 38 lines
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +2 -0
  	spinlock_t			queue_lock;
  	struct kfifo                    job_queue;
 
  	atomic_t			fence_seq;
  	uint64_t                        fence_context;
 
  	struct dma_fence		*dependency;
  	struct dma_fence_cb		cb;
  };
 
  /**
   * Run queue is a set of entities scheduling command submissions for
   * one specific ring. It implements the scheduling policy that selects
   * the next entity to emit commands from.
  */
@@ -154,30 +156,32 @@ struct amd_gpu_scheduler {
  	spinlock_t			job_list_lock;
  };
 
  int amd_sched_init(struct amd_gpu_scheduler *sched,
  		   const struct amd_sched_backend_ops *ops,
  		   uint32_t hw_submission, long timeout, const char *name);
  void amd_sched_fini(struct amd_gpu_scheduler *sched);
 
  int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  			  struct amd_sched_entity *entity,
  			  struct amd_sched_rq *rq,
  			  uint32_t jobs);
  void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  			   struct amd_sched_entity *entity);
  void amd_sched_entity_push_job(struct amd_sched_job *sched_job);
+void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
+			     struct amd_sched_rq *rq);
 
  int amd_sched_fence_slab_init(void);
  void amd_sched_fence_slab_fini(void);
 
  struct amd_sched_fence *amd_sched_fence_create(

drivers/gpu/drm/amd/scheduler/gpu_scheduler.h struct amd_gpu_scheduler { · line 187

Quoted 10 lines
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +0 -0
  	struct amd_sched_entity *s_entity, void *owner);
  void amd_sched_fence_scheduled(struct amd_sched_fence *fence);
  void amd_sched_fence_finished(struct amd_sched_fence *fence);
  int amd_sched_job_init(struct amd_sched_job *job,
  		       struct amd_gpu_scheduler *sched,
  		       struct amd_sched_entity *entity,
  		       void *owner);
  void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
  void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
  bool amd_sched_dependency_optimized(struct dma_fence* fence,

_______________________________________________
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): e03e3e71-5ca7-f5f0-9fc6-165b16171f7a-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#29 lore raw

PATCH 6/8 drm/amd/sched: allow clients to edit an entity's rq v2

This is useful for changing an entity's priority at runtime.

v2: don't modify the order of amd_sched_entity members

---
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.c | 26 +++++++++++++++++++++++---
 drivers/gpu/drm/amd/scheduler/gpu_scheduler.h |  3 +++
 2 files changed, 26 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/scheduler/gpu_scheduler.c +23 -3
index a203736..c19bb85 100644
@@ -133,6 +133,7 @@ int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
 	entity->rq = rq;
 	entity->sched = sched;
 
+	spin_lock_init(&entity->rq_lock);
 	spin_lock_init(&entity->queue_lock);
 	r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
 	if (r)
@@ -204,8 +205,6 @@ static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
 void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
 			   struct amd_sched_entity *entity)
 {
-	struct amd_sched_rq *rq = entity->rq;
-
 	if (!amd_sched_entity_is_initialized(sched, entity))
 		return;
 
@@ -215,7 +214,8 @@ void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
 	*/
 	wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
 
-	amd_sched_rq_remove_entity(rq, entity);
+	amd_sched_entity_set_rq(entity, NULL);
+
 	kfifo_free(&entity->job_queue);
 }
 
@@ -236,6 +236,24 @@ static void amd_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb
 	dma_fence_put(f);
 }
 
+void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
+			     struct amd_sched_rq *rq)
+{
+	if (entity->rq == rq)
+		return;
+
+	spin_lock(&entity->rq_lock);
+
+	if (entity->rq)
+		amd_sched_rq_remove_entity(entity->rq, entity);
+
+	entity->rq = rq;
+	if (rq)
+		amd_sched_rq_add_entity(rq, entity);
+
+	spin_unlock(&entity->rq_lock);
+}
+
 bool amd_sched_dependency_optimized(struct dma_fence* fence,
 				    struct amd_sched_entity *entity)
 {
@@ -333,7 +351,9 @@ static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
 	/* first job wakes up scheduler */
 	if (first) {
 		/* Add the entity to the run queue */
+		spin_lock(&entity->rq_lock);
 		amd_sched_rq_add_entity(entity->rq, entity);
+		spin_unlock(&entity->rq_lock);
 		amd_sched_wakeup(sched);
 	}
 	return added;
drivers/gpu/drm/amd/scheduler/gpu_scheduler.h +3 -0
index b9283b5..558d3a3 100644
@@ -40,6 +40,7 @@ struct amd_sched_priority_ctr;
 struct amd_sched_entity {
 	struct list_head		list;
 	struct amd_sched_rq		*rq;
+	spinlock_t			rq_lock;
 	struct amd_gpu_scheduler	*sched;
 
 	spinlock_t			queue_lock;
@@ -166,6 +167,8 @@ int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
 void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
 			   struct amd_sched_entity *entity);
 void amd_sched_entity_push_job(struct amd_sched_job *sched_job);
+void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
+			     struct amd_sched_rq *rq);
 
 int amd_sched_fence_slab_init(void);
 void amd_sched_fence_slab_fini(void);
Signature · 6 lines
2.9.3

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

PATCH 7/8 drm/amdgpu: add plumbing for ctx priority changes

In reply to a message not in the archive

Introduce amdgpu_ctx_priority_get/put(). This a refcounted mechanism to change a context's priority.

A context's priority will be set to the highest priority for which a request exists.

If no active requests exist, the context will default to the priority requested at context allocation time.

Note that the priority requested at allocation does not create a request, therefore it can be overridden by a get() request for a lower priority.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  8 +++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 43 +++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +7 -1
index 04ea1b9..b998f42 100644
@@ -813,47 +813,53 @@ int amdgpu_queue_mgr_map(struct amdgpu_device *adev,
 
 struct amdgpu_ctx_ring {
 	uint64_t		sequence;
 	struct dma_fence	**fences;
 	struct amd_sched_entity	entity;
 };
 
 struct amdgpu_ctx {
 	struct kref		refcount;
 	struct amdgpu_device    *adev;
 	struct amdgpu_queue_mgr queue_mgr;
 	unsigned		reset_counter;
 	spinlock_t		ring_lock;
 	struct dma_fence	**fences;
 	struct amdgpu_ctx_ring	rings[AMDGPU_MAX_RINGS];
-	bool preamble_presented;
+	bool 			preamble_presented;
+
+	struct amd_sched_priority_ctr	priority_ctr;
 };
 
 struct amdgpu_ctx_mgr {
 	struct amdgpu_device	*adev;
 	struct mutex		lock;
 	/* protected by lock */
 	struct idr		ctx_handles;
 };
 
 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id);
 int amdgpu_ctx_put(struct amdgpu_ctx *ctx);
 
 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
 			      struct dma_fence *fence);
 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
 				   struct amdgpu_ring *ring, uint64_t seq);
+void amdgpu_ctx_priority_get(struct amdgpu_ctx *ctx,
+			     enum amd_sched_priority priority);
+void amdgpu_ctx_priority_put(struct amdgpu_ctx *ctx,
+			     enum amd_sched_priority priority);
 
 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 		     struct drm_file *filp);
 
 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
 
 /*
  * file private structure
  */
 
 struct amdgpu_fpriv {
 	struct amdgpu_vm	vm;
 	struct amdgpu_bo_va	*prt_va;
 	struct mutex		bo_list_lock;
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +43 -0

156 lines not on this page · raw message

Signature · 6 lines
2.9.3

_______________________________________________
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): 20170608220623.9002-8-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#32 lore raw

Re: [PATCH 7/8] drm/amdgpu: add plumbing for ctx priority changes

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 6 lines

Introduce amdgpu_ctx_priority_get/put(). This a refcounted mechanism to change a context's priority.

A context's priority will be set to the highest priority for which a request exists.

Quoted 6 lines

If no active requests exist, the context will default to the priority requested at context allocation time.

Note that the priority requested at allocation does not create a request, therefore it can be overridden by a get() request for a lower priority.

So you just use the maximum priority between the per process and the per context priority?

Sounds logical, since the per context priority can only be used by the DRM master as well.

But you don't need the complicated amd_sched_priority_ctr handling for this.

Just add a min priority field into amdgpu_ctx_mgr and use that to override the context priority.

Christian.

Quoted 219 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  8 +++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 43 +++++++++++++++++++++++++++++++++
  2 files changed, 50 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +7 -1
index 04ea1b9..b998f42 100644
@@ -813,47 +813,53 @@ int amdgpu_queue_mgr_map(struct amdgpu_device *adev,
 
  struct amdgpu_ctx_ring {
  	uint64_t		sequence;
  	struct dma_fence	**fences;
  	struct amd_sched_entity	entity;
  };
 
  struct amdgpu_ctx {
  	struct kref		refcount;
  	struct amdgpu_device    *adev;
  	struct amdgpu_queue_mgr queue_mgr;
  	unsigned		reset_counter;
  	spinlock_t		ring_lock;
  	struct dma_fence	**fences;
  	struct amdgpu_ctx_ring	rings[AMDGPU_MAX_RINGS];
-	bool preamble_presented;
+	bool 			preamble_presented;
+
+	struct amd_sched_priority_ctr	priority_ctr;
  };
 
  struct amdgpu_ctx_mgr {
  	struct amdgpu_device	*adev;
  	struct mutex		lock;
  	/* protected by lock */
  	struct idr		ctx_handles;
  };
 
  struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id);
  int amdgpu_ctx_put(struct amdgpu_ctx *ctx);
 
  uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
  			      struct dma_fence *fence);
  struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  				   struct amdgpu_ring *ring, uint64_t seq);
+void amdgpu_ctx_priority_get(struct amdgpu_ctx *ctx,
+			     enum amd_sched_priority priority);
+void amdgpu_ctx_priority_put(struct amdgpu_ctx *ctx,
+			     enum amd_sched_priority priority);
 
  int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  		     struct drm_file *filp);
 
  void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
 
  /*
   * file private structure
   */
 
  struct amdgpu_fpriv {
  	struct amdgpu_vm	vm;
  	struct amdgpu_bo_va	*prt_va;
  	struct mutex		bo_list_lock;
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +43 -0
index 0285eef..cc15b7e 100644
@@ -29,30 +29,53 @@ static amdgpu_ctx_priority_permit(struct drm_file *filp,
  				  enum amd_sched_priority priority)
  {
  	/* NORMAL and below are accessible by everyone */
  	if (priority <= AMD_SCHED_PRIORITY_NORMAL)
  		return 0;
 
  	if (capable(CAP_SYS_NICE))
  		return 0;
 
  	if (drm_is_current_master(filp))
  		return 0;
 
  	return -EACCES;
  }
 
+static void amdgpu_ctx_priority_set(struct amd_sched_priority_ctr *p,
+				    enum amd_sched_priority priority)
+{
+	int i;
+	struct amd_sched_rq *rq;
+	struct amd_sched_entity *entity;
+	struct amdgpu_ring *ring;
+	struct amdgpu_ctx *ctx = container_of(p, struct amdgpu_ctx,
+					      priority_ctr);
+	struct amdgpu_device *adev = ctx->adev;
+
+	for (i = 0; i < adev->num_rings; i++) {
+		ring = adev->rings[i];
+		entity = &ctx->rings[i].entity;
+		rq = &ring->sched.sched_rq[priority];
+
+		if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
+			continue;
+
+		amd_sched_entity_set_rq(entity, rq);
+	}
+}
+
  static int amdgpu_ctx_init(struct amdgpu_device *adev,
  			   enum amd_sched_priority priority,
  			   struct drm_file *filp,
  			   struct amdgpu_ctx *ctx)
  {
  	unsigned i, j;
  	int r;
 
  	if (priority < 0 || priority >= AMD_SCHED_PRIORITY_MAX)
  		return -EINVAL;
 
  	r = amdgpu_ctx_priority_permit(filp, priority);
  	if (r)
  		return r;
 
@@ -76,53 +99,59 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
  	for (i = 0; i < adev->num_rings; i++) {
  		struct amdgpu_ring *ring = adev->rings[i];
  		struct amd_sched_rq *rq;
 
  		rq = &ring->sched.sched_rq[priority];
 
  		if (ring == &adev->gfx.kiq.ring)
  			continue;
 
  		r = amd_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
  					  rq, amdgpu_sched_jobs);
  		if (r)
  			goto failed;
  	}
 
+	amd_sched_priority_ctr_init(&ctx->priority_ctr,
+				    priority,
+				    amdgpu_ctx_priority_set);
+
  	r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
  	if (r)
  		goto failed;
 
  	return 0;
 
  failed:
  	for (j = 0; j < i; j++)
  		amd_sched_entity_fini(&adev->rings[j]->sched,
  				      &ctx->rings[j].entity);
  	kfree(ctx->fences);
  	ctx->fences = NULL;
  	return r;
  }
 
  static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
  {
  	struct amdgpu_device *adev = ctx->adev;
  	unsigned i, j;
 
  	if (!adev)
  		return;
 
+	amd_sched_priority_ctr_fini(&ctx->priority_ctr);
+
  	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  		for (j = 0; j < amdgpu_sched_jobs; ++j)
  			dma_fence_put(ctx->rings[i].fences[j]);
  	kfree(ctx->fences);
  	ctx->fences = NULL;
 
  	for (i = 0; i < adev->num_rings; i++)
  		amd_sched_entity_fini(&adev->rings[i]->sched,
  				      &ctx->rings[i].entity);
 
  	amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
  }
 
  static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
  			    struct amdgpu_fpriv *fpriv,
@@ -345,30 +374,44 @@ struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  		return ERR_PTR(-EINVAL);
  	}
 
 
  	if (seq + amdgpu_sched_jobs < cring->sequence) {
  		spin_unlock(&ctx->ring_lock);
  		return NULL;
  	}
 
  	fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
  	spin_unlock(&ctx->ring_lock);
 
  	return fence;
  }
 
+void amdgpu_ctx_priority_get(struct amdgpu_ctx *ctx,
+			     enum amd_sched_priority priority)
+{
+	amd_sched_priority_ctr_get(&ctx->priority_ctr, priority);
+
+}
+
+void amdgpu_ctx_priority_put(struct amdgpu_ctx *ctx,
+			     enum amd_sched_priority priority)
+{
+	amd_sched_priority_ctr_put(&ctx->priority_ctr, priority);
+
+}
+
  void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
  {
  	mutex_init(&mgr->lock);
  	idr_init(&mgr->ctx_handles);
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, · line 417

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
 
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  {
  	struct amdgpu_ctx *ctx;
  	struct idr *idp;
  	uint32_t id;
 
  	idp = &mgr->ctx_handles;
 
  	idr_for_each_entry(idp, ctx, id) {

_______________________________________________
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): d3f5cc21-8405-e309-888f-b805bf5edd1e-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#34 lore raw

Re: [PATCH 7/8] drm/amdgpu: add plumbing for ctx priority changes

Quoted 7 lines · Christian König

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 6 lines

Introduce amdgpu_ctx_priority_get/put(). This a refcounted mechanism to change a context's priority.

A context's priority will be set to the highest priority for which a request exists.

Quoted 8 lines
Quoted 6 lines

If no active requests exist, the context will default to the priority requested at context allocation time.

Note that the priority requested at allocation does not create a request, therefore it can be overridden by a get() request for a lower priority.

So you just use the maximum priority between the per process and the per context priority?

Not exactly. If we use the maximum of the two, then we won't be able to reduce the priority.

As long as we have a valid process priority, it will force override the context priority.

The actual priority of a context would be:
priority = (process_prio != 'INVALID') ? process_prio : context_prio

Where process_prio is the maximum level for which a DRM_MASTER has an active request.

Quoted 3 lines

Sounds logical, since the per context priority can only be used by the DRM master as well.

I think there is a small typo here (s/context/process). Based on your usage of the context priority in the next paragraph I think we are on the same page.

Quoted 3 lines

But you don't need the complicated amd_sched_priority_ctr handling for this.

Just add a min priority field into amdgpu_ctx_mgr and use that to override the context priority.

Agreed. My initial implementation was pretty much the one line of code above.

I just happened to have amd_sched_priority_ctr around for tracking the minimum scheduler priority, and it seems to fit this use case pretty well.

Regards,
Andres

Quoted 196 lines

Christian.

Quoted 193 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  8 +++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 43 
+++++++++++++++++++++++++++++++++
  2 files changed, 50 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +7 -1
index 04ea1b9..b998f42 100644
@@ -813,47 +813,53 @@ int amdgpu_queue_mgr_map(struct amdgpu_device 
 *adev,
  struct amdgpu_ctx_ring {
      uint64_t        sequence;
      struct dma_fence    **fences;
      struct amd_sched_entity    entity;
  };
  struct amdgpu_ctx {
      struct kref        refcount;
      struct amdgpu_device    *adev;
      struct amdgpu_queue_mgr queue_mgr;
      unsigned        reset_counter;
      spinlock_t        ring_lock;
      struct dma_fence    **fences;
      struct amdgpu_ctx_ring    rings[AMDGPU_MAX_RINGS];
-    bool preamble_presented;
+    bool             preamble_presented;
+
+    struct amd_sched_priority_ctr    priority_ctr;
  };
  struct amdgpu_ctx_mgr {
      struct amdgpu_device    *adev;
      struct mutex        lock;
      /* protected by lock */
      struct idr        ctx_handles;
  };
  struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, 
 uint32_t id);
  int amdgpu_ctx_put(struct amdgpu_ctx *ctx);
  uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct 
 amdgpu_ring *ring,
                    struct dma_fence *fence);
  struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
                     struct amdgpu_ring *ring, uint64_t seq);
+void amdgpu_ctx_priority_get(struct amdgpu_ctx *ctx,
+                 enum amd_sched_priority priority);
+void amdgpu_ctx_priority_put(struct amdgpu_ctx *ctx,
+                 enum amd_sched_priority priority);
  int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
               struct drm_file *filp);
  void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
  /*
   * file private structure
   */
  struct amdgpu_fpriv {
      struct amdgpu_vm    vm;
      struct amdgpu_bo_va    *prt_va;
      struct mutex        bo_list_lock;
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +43 -0
index 0285eef..cc15b7e 100644
@@ -29,30 +29,53 @@ static amdgpu_ctx_priority_permit(struct drm_file 
 *filp,
                    enum amd_sched_priority priority)
  {
      /* NORMAL and below are accessible by everyone */
      if (priority <= AMD_SCHED_PRIORITY_NORMAL)
          return 0;
      if (capable(CAP_SYS_NICE))
          return 0;
      if (drm_is_current_master(filp))
          return 0;
      return -EACCES;
  }
+static void amdgpu_ctx_priority_set(struct amd_sched_priority_ctr *p,
+                    enum amd_sched_priority priority)
+{
+    int i;
+    struct amd_sched_rq *rq;
+    struct amd_sched_entity *entity;
+    struct amdgpu_ring *ring;
+    struct amdgpu_ctx *ctx = container_of(p, struct amdgpu_ctx,
+                          priority_ctr);
+    struct amdgpu_device *adev = ctx->adev;
+
+    for (i = 0; i < adev->num_rings; i++) {
+        ring = adev->rings[i];
+        entity = &ctx->rings[i].entity;
+        rq = &ring->sched.sched_rq[priority];
+
+        if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
+            continue;
+
+        amd_sched_entity_set_rq(entity, rq);
+    }
+}
+
  static int amdgpu_ctx_init(struct amdgpu_device *adev,
                 enum amd_sched_priority priority,
                 struct drm_file *filp,
                 struct amdgpu_ctx *ctx)
  {
      unsigned i, j;
      int r;
      if (priority < 0 || priority >= AMD_SCHED_PRIORITY_MAX)
          return -EINVAL;
      r = amdgpu_ctx_priority_permit(filp, priority);
      if (r)
          return r;
@@ -76,53 +99,59 @@ static int amdgpu_ctx_init(struct amdgpu_device 
 *adev,
      for (i = 0; i < adev->num_rings; i++) {
          struct amdgpu_ring *ring = adev->rings[i];
          struct amd_sched_rq *rq;
          rq = &ring->sched.sched_rq[priority];
          if (ring == &adev->gfx.kiq.ring)
              continue;
          r = amd_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
                        rq, amdgpu_sched_jobs);
          if (r)
              goto failed;
      }
+    amd_sched_priority_ctr_init(&ctx->priority_ctr,
+                    priority,
+                    amdgpu_ctx_priority_set);
+
      r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
      if (r)
          goto failed;
      return 0;
  failed:
      for (j = 0; j < i; j++)
          amd_sched_entity_fini(&adev->rings[j]->sched,
                        &ctx->rings[j].entity);
      kfree(ctx->fences);
      ctx->fences = NULL;
      return r;
  }
  static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
  {
      struct amdgpu_device *adev = ctx->adev;
      unsigned i, j;
      if (!adev)
          return;
+    amd_sched_priority_ctr_fini(&ctx->priority_ctr);
+
      for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
          for (j = 0; j < amdgpu_sched_jobs; ++j)
              dma_fence_put(ctx->rings[i].fences[j]);
      kfree(ctx->fences);
      ctx->fences = NULL;
      for (i = 0; i < adev->num_rings; i++)
          amd_sched_entity_fini(&adev->rings[i]->sched,
                        &ctx->rings[i].entity);
      amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
  }
  static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
                  struct amdgpu_fpriv *fpriv,
@@ -345,30 +374,44 @@ struct dma_fence *amdgpu_ctx_get_fence(struct 
 amdgpu_ctx *ctx,
          return ERR_PTR(-EINVAL);
      }
      if (seq + amdgpu_sched_jobs < cring->sequence) {
          spin_unlock(&ctx->ring_lock);
          return NULL;
      }
      fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 
 1)]);
      spin_unlock(&ctx->ring_lock);
      return fence;
  }
+void amdgpu_ctx_priority_get(struct amdgpu_ctx *ctx,
+                 enum amd_sched_priority priority)
+{
+    amd_sched_priority_ctr_get(&ctx->priority_ctr, priority);
+
+}
+
+void amdgpu_ctx_priority_put(struct amdgpu_ctx *ctx,
+                 enum amd_sched_priority priority)
+{
+    amd_sched_priority_ctr_put(&ctx->priority_ctr, priority);
+
+}
+
  void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
  {
      mutex_init(&mgr->lock);
      idr_init(&mgr->ctx_handles);
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, · line 417

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
  }
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  {
      struct amdgpu_ctx *ctx;
      struct idr *idp;
      uint32_t id;
      idp = &mgr->ctx_handles;
      idr_for_each_entry(idp, ctx, id) {

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

#35 lore raw

PATCH 8/8 drm/amdgpu: add interface for editing a foreign process's priority

In reply to a message not in the archive

The AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET/PUT ioctls are used to set the priority of a different process in the current system.

When all requests are dropped, the foreign process's contexts will be restored to the priority specified at context creation time.

An fd is used to identify the remote process. This is simpler than passing a pid number, which is vulnerable to re-use, etc.

This functionality is limited to DRM_MASTER since abuse of this interface can have a negative impact on the system's performance.

---
 drivers/gpu/drm/amd/amdgpu/Makefile       |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   |  19 +----
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c   |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 117 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h |  34 +++++++++
 include/uapi/drm/amdgpu_drm.h             |  25 +++++++
 6 files changed, 180 insertions(+), 19 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index b62d9e9..e4d3b07 100644
@@ -13,31 +13,31 @@ ccflags-y := -Iinclude/drm -I$(FULL_AMD_PATH)/include/asic_reg \
 
 amdgpu-y := amdgpu_drv.o
 
 # add KMS driver
 amdgpu-y += amdgpu_device.o amdgpu_kms.o \
 	amdgpu_atombios.o atombios_crtc.o amdgpu_connectors.o \
 	atom.o amdgpu_fence.o amdgpu_ttm.o amdgpu_object.o amdgpu_gart.o \
 	amdgpu_encoders.o amdgpu_display.o amdgpu_i2c.o \
 	amdgpu_fb.o amdgpu_gem.o amdgpu_ring.o \
 	amdgpu_cs.o amdgpu_bios.o amdgpu_benchmark.o amdgpu_test.o \
 	amdgpu_pm.o atombios_dp.o amdgpu_afmt.o amdgpu_trace_points.o \
 	atombios_encoders.o amdgpu_sa.o atombios_i2c.o \
 	amdgpu_prime.o amdgpu_vm.o amdgpu_ib.o amdgpu_pll.o \
 	amdgpu_ucode.o amdgpu_bo_list.o amdgpu_ctx.o amdgpu_sync.o \
 	amdgpu_gtt_mgr.o amdgpu_vram_mgr.o amdgpu_virt.o amdgpu_atomfirmware.o \
-	amdgpu_queue_mgr.o
+	amdgpu_queue_mgr.o amdgpu_sched.o
 
 # add asic specific block
 amdgpu-$(CONFIG_DRM_AMDGPU_CIK)+= cik.o cik_ih.o kv_smc.o kv_dpm.o \
 	ci_smc.o ci_dpm.o dce_v8_0.o gfx_v7_0.o cik_sdma.o uvd_v4_2.o vce_v2_0.o \
 	amdgpu_amdkfd_gfx_v7.o
 
 amdgpu-$(CONFIG_DRM_AMDGPU_SI)+= si.o gmc_v6_0.o gfx_v6_0.o si_ih.o si_dma.o dce_v6_0.o si_dpm.o si_smc.o
 
 amdgpu-y += \
 	vi.o mxgpu_vi.o nbio_v6_1.o soc15.o mxgpu_ai.o nbio_v7_0.o
 
 # add GMC block
 amdgpu-y += \
 	gmc_v7_0.o \
 	gmc_v8_0.o \
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -18
index cc15b7e..a578557 100644
@@ -12,30 +12,31 @@
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors: monk liu <[email protected]>
  */
 
 #include <drm/drmP.h>
 #include "amdgpu.h"
+#include "amdgpu_sched.h"
 
 static amdgpu_ctx_priority_permit(struct drm_file *filp,
 				  enum amd_sched_priority priority)
 {
 	/* NORMAL and below are accessible by everyone */
 	if (priority <= AMD_SCHED_PRIORITY_NORMAL)
 		return 0;
 
 	if (capable(CAP_SYS_NICE))
 		return 0;
 
 	if (drm_is_current_master(filp))
 		return 0;
 
 	return -EACCES;
@@ -234,48 +235,30 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 	out->state.hangs = 0x0;
 
 	/* determine if a GPU reset has occured since the last call */
 	reset_counter = atomic_read(&adev->gpu_reset_counter);
 	/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
 	if (ctx->reset_counter == reset_counter)
 		out->state.reset_status = AMDGPU_CTX_NO_RESET;
 	else
 		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
 	ctx->reset_counter = reset_counter;
 
 	mutex_unlock(&mgr->lock);
 	return 0;
 }
 
-static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
-{
-	switch (amdgpu_priority) {
-	case AMDGPU_CTX_PRIORITY_HIGH_HW:
-		return AMD_SCHED_PRIORITY_HIGH_HW;
-	case AMDGPU_CTX_PRIORITY_HIGH_SW:
-		return AMD_SCHED_PRIORITY_HIGH_SW;
-	case AMDGPU_CTX_PRIORITY_NORMAL:
-		return AMD_SCHED_PRIORITY_NORMAL;
-	case AMDGPU_CTX_PRIORITY_LOW_SW:
-	case AMDGPU_CTX_PRIORITY_LOW_HW:
-		return AMD_SCHED_PRIORITY_LOW;
-	default:
-		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
-		return AMD_SCHED_PRIORITY_INVALID;
-	}
-}
-
 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 		     struct drm_file *filp)
 {
 	int r;
 	uint32_t id;
 	enum amd_sched_priority priority;
 
 	union drm_amdgpu_ctx *args = data;
 	struct amdgpu_device *adev = dev->dev_private;
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 
 	r = 0;
 	id = args->in.ctx_id;
 	priority = amdgpu_to_sched_priority(args->in.priority);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +2 -0
index 92e93b3..6a33c61 100644
@@ -16,30 +16,31 @@
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors: Dave Airlie
  *          Alex Deucher
  *          Jerome Glisse
  */
 #include <drm/drmP.h>
 #include "amdgpu.h"
 #include <drm/amdgpu_drm.h>
+#include "amdgpu_sched.h"
 #include "amdgpu_uvd.h"
 #include "amdgpu_vce.h"
 
 #include <linux/vga_switcheroo.h>
 #include <linux/slab.h>
 #include <linux/pm_runtime.h>
 #include "amdgpu_amdkfd.h"
 
 /**
  * amdgpu_driver_unload_kms - Main unload function for KMS.
  *
  * @dev: drm dev pointer
  *
  * This is the main unload function for KMS (all asics).
  * Returns 0 on success.
@@ -1004,30 +1005,31 @@ int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  *
  * Disable the interrupt on the requested crtc (all asics).
  */
 void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
 {
 	struct amdgpu_device *adev = dev->dev_private;
 	int idx = amdgpu_crtc_idx_to_irq_type(adev, pipe);
 
 	amdgpu_irq_put(adev, &adev->crtc_irq, idx);
 }
 
 const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER),
 	DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	/* KMS */
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c new file +117 -0

117 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h new file +34 -0
new file mode 100644
index 0000000..b28c067
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Valve Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Andres Rodriguez <[email protected]>
+ */
+
+#ifndef __AMDGPU_SCHED_H__
+#define __AMDGPU_SCHED_H__
+
+#include <drm/drmP.h>
+
+enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority);
+int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
+		       struct drm_file *filp);
+
+#endif // __AMDGPU_SCHED_H__
include/uapi/drm/amdgpu_drm.h +25 -0
index 590fc2a..99eb946 100644
@@ -40,45 +40,47 @@ extern "C" {
 
 #define DRM_AMDGPU_GEM_CREATE		0x00
 #define DRM_AMDGPU_GEM_MMAP		0x01
 #define DRM_AMDGPU_CTX			0x02
 #define DRM_AMDGPU_BO_LIST		0x03
 #define DRM_AMDGPU_CS			0x04
 #define DRM_AMDGPU_INFO			0x05
 #define DRM_AMDGPU_GEM_METADATA		0x06
 #define DRM_AMDGPU_GEM_WAIT_IDLE	0x07
 #define DRM_AMDGPU_GEM_VA		0x08
 #define DRM_AMDGPU_WAIT_CS		0x09
 #define DRM_AMDGPU_GEM_OP		0x10
 #define DRM_AMDGPU_GEM_USERPTR		0x11
 #define DRM_AMDGPU_WAIT_FENCES		0x12
 #define DRM_AMDGPU_VM			0x13
+#define DRM_AMDGPU_SCHED		0x14
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
 #define DRM_IOCTL_AMDGPU_CTX		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_CTX, union drm_amdgpu_ctx)
 #define DRM_IOCTL_AMDGPU_BO_LIST	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_BO_LIST, union drm_amdgpu_bo_list)
 #define DRM_IOCTL_AMDGPU_CS		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_CS, union drm_amdgpu_cs)
 #define DRM_IOCTL_AMDGPU_INFO		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_INFO, struct drm_amdgpu_info)
 #define DRM_IOCTL_AMDGPU_GEM_METADATA	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_METADATA, struct drm_amdgpu_gem_metadata)
 #define DRM_IOCTL_AMDGPU_GEM_WAIT_IDLE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_WAIT_IDLE, union drm_amdgpu_gem_wait_idle)
 #define DRM_IOCTL_AMDGPU_GEM_VA		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_VA, struct drm_amdgpu_gem_va)
 #define DRM_IOCTL_AMDGPU_WAIT_CS	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_WAIT_CS, union drm_amdgpu_wait_cs)
 #define DRM_IOCTL_AMDGPU_GEM_OP		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_OP, struct drm_amdgpu_gem_op)
 #define DRM_IOCTL_AMDGPU_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_USERPTR, struct drm_amdgpu_gem_userptr)
 #define DRM_IOCTL_AMDGPU_WAIT_FENCES	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_WAIT_FENCES, union drm_amdgpu_wait_fences)
 #define DRM_IOCTL_AMDGPU_VM		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
+#define DRM_IOCTL_AMDGPU_SCHED		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
 
 #define AMDGPU_GEM_DOMAIN_CPU		0x1
 #define AMDGPU_GEM_DOMAIN_GTT		0x2
 #define AMDGPU_GEM_DOMAIN_VRAM		0x4
 #define AMDGPU_GEM_DOMAIN_GDS		0x8
 #define AMDGPU_GEM_DOMAIN_GWS		0x10
 #define AMDGPU_GEM_DOMAIN_OA		0x20
 
 /* Flag that CPU access will be required for the case of VRAM domain */
 #define AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED	(1 << 0)
 /* Flag that CPU access will not work, this VRAM domain is invisible */
 #define AMDGPU_GEM_CREATE_NO_CPU_ACCESS		(1 << 1)
 /* Flag that USWC attributes should be used for GTT */
 #define AMDGPU_GEM_CREATE_CPU_GTT_USWC		(1 << 2)
 /* Flag that the memory should be in VRAM and cleared */
@@ -208,30 +210,53 @@ struct drm_amdgpu_vm_in {
 	/** AMDGPU_VM_OP_* */
 	__u32	op;
 	__u32	flags;
 };
 
 struct drm_amdgpu_vm_out {
 	/** For future use, no flags defined so far */
 	__u64	flags;
 };
 
 union drm_amdgpu_vm {
 	struct drm_amdgpu_vm_in in;
 	struct drm_amdgpu_vm_out out;
 };
 
+/* sched ioctl */
+#define AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET	1
+#define AMDGPU_SCHED_OP_PROCESS_PRIORITY_PUT	2
+
+struct drm_amdgpu_sched_in {
+	/* AMDGPU_SCHED_OP_* */
+	__u32	op;
+	__u32	fd;
+	__s32	priority;
+	/* For future use */
+	__u32	reserved;
+};
+
+struct drm_amdgpu_sched_out {
+	/* For future use */
+	__u64	reserved;
+};
+
+union drm_amdgpu_sched {
+	struct drm_amdgpu_sched_in in;
+	struct drm_amdgpu_sched_out out;
+};
+
 /*
  * This is not a reliable API and you should expect it to fail for any
  * number of reasons and have fallback path that do not use userptr to
  * perform any operation.
  */
 #define AMDGPU_GEM_USERPTR_READONLY	(1 << 0)
 #define AMDGPU_GEM_USERPTR_ANONONLY	(1 << 1)
 #define AMDGPU_GEM_USERPTR_VALIDATE	(1 << 2)
 #define AMDGPU_GEM_USERPTR_REGISTER	(1 << 3)
 
 struct drm_amdgpu_gem_userptr {
 	__u64		addr;
 	__u64		size;
 	/* AMDGPU_GEM_USERPTR_* */
 	__u32		flags;
Signature · 6 lines
2.9.3

_______________________________________________
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): 20170608220623.9002-9-andresx7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#37 lore raw

Re: [PATCH 8/8] drm/amdgpu: add interface for editing a foreign process's priority

Am 09.06.2017 um 00:06 schrieb Andres Rodriguez:

Quoted 446 lines

The AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET/PUT ioctls are used to set the priority of a different process in the current system.

When all requests are dropped, the foreign process's contexts will be restored to the priority specified at context creation time.

An fd is used to identify the remote process. This is simpler than passing a pid number, which is vulnerable to re-use, etc.

This functionality is limited to DRM_MASTER since abuse of this interface can have a negative impact on the system's performance.

---
  drivers/gpu/drm/amd/amdgpu/Makefile       |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   |  19 +----
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c   |   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 117 ++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h |  34 +++++++++
  include/uapi/drm/amdgpu_drm.h             |  25 +++++++
  6 files changed, 180 insertions(+), 19 deletions(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index b62d9e9..e4d3b07 100644
@@ -13,31 +13,31 @@ ccflags-y := -Iinclude/drm -I$(FULL_AMD_PATH)/include/asic_reg \
 
  amdgpu-y := amdgpu_drv.o
 
  # add KMS driver
  amdgpu-y += amdgpu_device.o amdgpu_kms.o \
  	amdgpu_atombios.o atombios_crtc.o amdgpu_connectors.o \
  	atom.o amdgpu_fence.o amdgpu_ttm.o amdgpu_object.o amdgpu_gart.o \
  	amdgpu_encoders.o amdgpu_display.o amdgpu_i2c.o \
  	amdgpu_fb.o amdgpu_gem.o amdgpu_ring.o \
  	amdgpu_cs.o amdgpu_bios.o amdgpu_benchmark.o amdgpu_test.o \
  	amdgpu_pm.o atombios_dp.o amdgpu_afmt.o amdgpu_trace_points.o \
  	atombios_encoders.o amdgpu_sa.o atombios_i2c.o \
  	amdgpu_prime.o amdgpu_vm.o amdgpu_ib.o amdgpu_pll.o \
  	amdgpu_ucode.o amdgpu_bo_list.o amdgpu_ctx.o amdgpu_sync.o \
  	amdgpu_gtt_mgr.o amdgpu_vram_mgr.o amdgpu_virt.o amdgpu_atomfirmware.o \
-	amdgpu_queue_mgr.o
+	amdgpu_queue_mgr.o amdgpu_sched.o
 
  # add asic specific block
  amdgpu-$(CONFIG_DRM_AMDGPU_CIK)+= cik.o cik_ih.o kv_smc.o kv_dpm.o \
  	ci_smc.o ci_dpm.o dce_v8_0.o gfx_v7_0.o cik_sdma.o uvd_v4_2.o vce_v2_0.o \
  	amdgpu_amdkfd_gfx_v7.o
 
  amdgpu-$(CONFIG_DRM_AMDGPU_SI)+= si.o gmc_v6_0.o gfx_v6_0.o si_ih.o si_dma.o dce_v6_0.o si_dpm.o si_smc.o
 
  amdgpu-y += \
  	vi.o mxgpu_vi.o nbio_v6_1.o soc15.o mxgpu_ai.o nbio_v7_0.o
 
  # add GMC block
  amdgpu-y += \
  	gmc_v7_0.o \
  	gmc_v8_0.o \
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -18
index cc15b7e..a578557 100644
@@ -12,30 +12,31 @@
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   * OTHER DEALINGS IN THE SOFTWARE.
   *
   * Authors: monk liu <[email protected]>
   */
 
  #include <drm/drmP.h>
  #include "amdgpu.h"
+#include "amdgpu_sched.h"
 
  static amdgpu_ctx_priority_permit(struct drm_file *filp,
  				  enum amd_sched_priority priority)
  {
  	/* NORMAL and below are accessible by everyone */
  	if (priority <= AMD_SCHED_PRIORITY_NORMAL)
  		return 0;
 
  	if (capable(CAP_SYS_NICE))
  		return 0;
 
  	if (drm_is_current_master(filp))
  		return 0;
 
  	return -EACCES;
@@ -234,48 +235,30 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
  	out->state.hangs = 0x0;
 
  	/* determine if a GPU reset has occured since the last call */
  	reset_counter = atomic_read(&adev->gpu_reset_counter);
  	/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  	if (ctx->reset_counter == reset_counter)
  		out->state.reset_status = AMDGPU_CTX_NO_RESET;
  	else
  		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  	ctx->reset_counter = reset_counter;
 
  	mutex_unlock(&mgr->lock);
  	return 0;
  }
 
-static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
-{
-	switch (amdgpu_priority) {
-	case AMDGPU_CTX_PRIORITY_HIGH_HW:
-		return AMD_SCHED_PRIORITY_HIGH_HW;
-	case AMDGPU_CTX_PRIORITY_HIGH_SW:
-		return AMD_SCHED_PRIORITY_HIGH_SW;
-	case AMDGPU_CTX_PRIORITY_NORMAL:
-		return AMD_SCHED_PRIORITY_NORMAL;
-	case AMDGPU_CTX_PRIORITY_LOW_SW:
-	case AMDGPU_CTX_PRIORITY_LOW_HW:
-		return AMD_SCHED_PRIORITY_LOW;
-	default:
-		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
-		return AMD_SCHED_PRIORITY_INVALID;
-	}
-}
-
  int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  		     struct drm_file *filp)
  {
  	int r;
  	uint32_t id;
  	enum amd_sched_priority priority;
 
  	union drm_amdgpu_ctx *args = data;
  	struct amdgpu_device *adev = dev->dev_private;
  	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 
  	r = 0;
  	id = args->in.ctx_id;
  	priority = amdgpu_to_sched_priority(args->in.priority);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +2 -0
index 92e93b3..6a33c61 100644
@@ -16,30 +16,31 @@
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   * OTHER DEALINGS IN THE SOFTWARE.
   *
   * Authors: Dave Airlie
   *          Alex Deucher
   *          Jerome Glisse
   */
  #include <drm/drmP.h>
  #include "amdgpu.h"
  #include <drm/amdgpu_drm.h>
+#include "amdgpu_sched.h"
  #include "amdgpu_uvd.h"
  #include "amdgpu_vce.h"
 
  #include <linux/vga_switcheroo.h>
  #include <linux/slab.h>
  #include <linux/pm_runtime.h>
  #include "amdgpu_amdkfd.h"
 
  /**
   * amdgpu_driver_unload_kms - Main unload function for KMS.
   *
   * @dev: drm dev pointer
   *
   * This is the main unload function for KMS (all asics).
   * Returns 0 on success.
@@ -1004,30 +1005,31 @@ int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe)
   *
   * Disable the interrupt on the requested crtc (all asics).
   */
  void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  {
  	struct amdgpu_device *adev = dev->dev_private;
  	int idx = amdgpu_crtc_idx_to_irq_type(adev, pipe);
 
  	amdgpu_irq_put(adev, &adev->crtc_irq, idx);
  }
 
  const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER),
  	DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	/* KMS */
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  };
  const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c new file +117 -0
new file mode 100644
index 0000000..eacce6f
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2017 Valve Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Andres Rodriguez <[email protected]>
+ */
+
+#include <linux/fdtable.h>
+#include <linux/pid.h>
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+#include "amdgpu_vm.h"
+
+enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
+{
+	switch (amdgpu_priority) {
+	case AMDGPU_CTX_PRIORITY_HIGH_HW:
+		return AMD_SCHED_PRIORITY_HIGH_HW;
+	case AMDGPU_CTX_PRIORITY_HIGH_SW:
+		return AMD_SCHED_PRIORITY_HIGH_SW;
+	case AMDGPU_CTX_PRIORITY_NORMAL:
+		return AMD_SCHED_PRIORITY_NORMAL;
+	case AMDGPU_CTX_PRIORITY_LOW_SW:
+	case AMDGPU_CTX_PRIORITY_LOW_HW:
+		return AMD_SCHED_PRIORITY_LOW;
+	default:
+		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
+		return AMD_SCHED_PRIORITY_INVALID;
+	}
+}
+
+static int amdgpu_sched_process_priority_ref(struct amdgpu_device *adev,
+					     int fd,
+					     enum amd_sched_priority priority,
+					     bool acquire)
+{
+	struct file *filp = fcheck(fd);
+	struct drm_file *file;
+	struct pid *pid;
+	struct amdgpu_fpriv *fpriv;
+	struct amdgpu_ctx *ctx;
+	uint32_t id;
+
+	if (!filp)
+		return -EINVAL;
+
+	pid = get_pid(((struct drm_file *)filp->private_data)->pid);
+
+	mutex_lock(&adev->ddev->filelist_mutex);
+	list_for_each_entry(file, &adev->ddev->filelist, lhead) {
+		if (file->pid != pid)
+			continue;
+
+		fpriv = file->driver_priv;
+		idr_for_each_entry(&fpriv->ctx_mgr.ctx_handles, ctx, id)
+			if (acquire)
+				amdgpu_ctx_priority_get(ctx, priority);
+			else
+				amdgpu_ctx_priority_put(ctx, priority);
+	}
+	mutex_unlock(&adev->ddev->filelist_mutex);
+
+	put_pid(pid);
+
+	return 0;
+}
+
+int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
+		       struct drm_file *filp)
+{
+	union drm_amdgpu_sched *args = data;
+	struct amdgpu_device *adev = dev->dev_private;
+	enum amd_sched_priority priority;
+	int r;
+
+	priority = amdgpu_to_sched_priority(args->in.priority);
+	if (args->in.reserved || priority == AMD_SCHED_PRIORITY_INVALID)
+		return -EINVAL;
+
+	switch (args->in.op) {
+	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET:
+		r = amdgpu_sched_process_priority_ref(adev,
+						      args->in.fd,
+						      priority,
+						      true);
+		break;
+	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_PUT:
+		r = amdgpu_sched_process_priority_ref(adev,
+						      args->in.fd,
+						      priority,
+						      false);
+		break;
+	default:
+		r = -EINVAL;
+		break;
+	}
+
+	return r;
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h new file +34 -0
new file mode 100644
index 0000000..b28c067
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Valve Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Andres Rodriguez <[email protected]>
+ */
+
+#ifndef __AMDGPU_SCHED_H__
+#define __AMDGPU_SCHED_H__
+
+#include <drm/drmP.h>
+
+enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority);
+int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
+		       struct drm_file *filp);
+
+#endif // __AMDGPU_SCHED_H__
include/uapi/drm/amdgpu_drm.h +3 -0
index 590fc2a..99eb946 100644
@@ -40,45 +40,47 @@ extern "C" {
 
  #define DRM_AMDGPU_GEM_CREATE		0x00
  #define DRM_AMDGPU_GEM_MMAP		0x01
  #define DRM_AMDGPU_CTX			0x02
  #define DRM_AMDGPU_BO_LIST		0x03
  #define DRM_AMDGPU_CS			0x04
  #define DRM_AMDGPU_INFO			0x05
  #define DRM_AMDGPU_GEM_METADATA		0x06
  #define DRM_AMDGPU_GEM_WAIT_IDLE	0x07
  #define DRM_AMDGPU_GEM_VA		0x08
  #define DRM_AMDGPU_WAIT_CS		0x09
  #define DRM_AMDGPU_GEM_OP		0x10
  #define DRM_AMDGPU_GEM_USERPTR		0x11
  #define DRM_AMDGPU_WAIT_FENCES		0x12
  #define DRM_AMDGPU_VM			0x13
+#define DRM_AMDGPU_SCHED		0x14
 
  #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
  #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
  #define DRM_IOCTL_AMDGPU_CTX		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_CTX, union drm_amdgpu_ctx)
  #define DRM_IOCTL_AMDGPU_BO_LIST	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_BO_LIST, union drm_amdgpu_bo_list)
  #define DRM_IOCTL_AMDGPU_CS		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_CS, union drm_amdgpu_cs)
  #define DRM_IOCTL_AMDGPU_INFO		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_INFO, struct drm_amdgpu_info)
  #define DRM_IOCTL_AMDGPU_GEM_METADATA	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_METADATA, struct drm_amdgpu_gem_metadata)
  #define DRM_IOCTL_AMDGPU_GEM_WAIT_IDLE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_WAIT_IDLE, union drm_amdgpu_gem_wait_idle)
  #define DRM_IOCTL_AMDGPU_GEM_VA		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_VA, struct drm_amdgpu_gem_va)
  #define DRM_IOCTL_AMDGPU_WAIT_CS	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_WAIT_CS, union drm_amdgpu_wait_cs)
  #define DRM_IOCTL_AMDGPU_GEM_OP		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_OP, struct drm_amdgpu_gem_op)
  #define DRM_IOCTL_AMDGPU_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_USERPTR, struct drm_amdgpu_gem_userptr)
  #define DRM_IOCTL_AMDGPU_WAIT_FENCES	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_WAIT_FENCES, union drm_amdgpu_wait_fences)
  #define DRM_IOCTL_AMDGPU_VM		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
+#define DRM_IOCTL_AMDGPU_SCHED		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
 
  #define AMDGPU_GEM_DOMAIN_CPU		0x1
  #define AMDGPU_GEM_DOMAIN_GTT		0x2
  #define AMDGPU_GEM_DOMAIN_VRAM		0x4
  #define AMDGPU_GEM_DOMAIN_GDS		0x8
  #define AMDGPU_GEM_DOMAIN_GWS		0x10
  #define AMDGPU_GEM_DOMAIN_OA		0x20
 
  /* Flag that CPU access will be required for the case of VRAM domain */
  #define AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED	(1 << 0)
  /* Flag that CPU access will not work, this VRAM domain is invisible */
  #define AMDGPU_GEM_CREATE_NO_CPU_ACCESS		(1 << 1)
  /* Flag that USWC attributes should be used for GTT */
  #define AMDGPU_GEM_CREATE_CPU_GTT_USWC		(1 << 2)
  /* Flag that the memory should be in VRAM and cleared */
@@ -208,30 +210,53 @@ struct drm_amdgpu_vm_in {
  	/** AMDGPU_VM_OP_* */
  	__u32	op;
  	__u32	flags;
  };
 
  struct drm_amdgpu_vm_out {
  	/** For future use, no flags defined so far */
  	__u64	flags;
  };
 
  union drm_amdgpu_vm {
  	struct drm_amdgpu_vm_in in;
  	struct drm_amdgpu_vm_out out;
  };
 
+/* sched ioctl */

include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_vm_in { · line 235

Quoted 10 lines
include/uapi/drm/amdgpu_drm.h +10 -0
+#define AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET	1
+#define AMDGPU_SCHED_OP_PROCESS_PRIORITY_PUT	2
+
+struct drm_amdgpu_sched_in {
+	/* AMDGPU_SCHED_OP_* */
+	__u32	op;
+	__u32	fd;
+	__s32	priority;
+	/* For future use */
+	__u32	reserved;

Even if you don't define one, call that flags not reserved.

include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_vm_in { · line 241

Quoted 6 lines
include/uapi/drm/amdgpu_drm.h +6 -0
+};
+
+struct drm_amdgpu_sched_out {
+	/* For future use */
+	__u64	reserved;
+};

If you don't have an out parameter, don't add a dummy structure and declare the IOCTL DRM_IOW instead of DRM_IORW.

Regards,
Christian.

Quoted 11 lines
include/uapi/drm/amdgpu_drm.h +6 -0
+
+union drm_amdgpu_sched {
+	struct drm_amdgpu_sched_in in;
+	struct drm_amdgpu_sched_out out;
+};
+
  /*
   * This is not a reliable API and you should expect it to fail for any
   * number of reasons and have fallback path that do not use userptr to
   * perform any operation.
   */

include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_vm_in { · line 262

Quoted 10 lines
include/uapi/drm/amdgpu_drm.h +0 -0
  #define AMDGPU_GEM_USERPTR_READONLY	(1 << 0)
  #define AMDGPU_GEM_USERPTR_ANONONLY	(1 << 1)
  #define AMDGPU_GEM_USERPTR_VALIDATE	(1 << 2)
  #define AMDGPU_GEM_USERPTR_REGISTER	(1 << 3)
 
  struct drm_amdgpu_gem_userptr {
  	__u64		addr;
  	__u64		size;
  	/* AMDGPU_GEM_USERPTR_* */
  	__u32		flags;

_______________________________________________
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): 0bb8f14b-1f11-7165-fa3d-74f2d2b38f4b-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#39 lore raw

PATCH 8/8 drm/amdgpu: add interface for editing a foreign process's priority v2

The AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET/PUT ioctls are used to set the priority of a different process in the current system.

When all requests are dropped, the foreign process's contexts will be restored to the priority specified at context creation time.

An fd is used to identify the remote process. This is simpler than passing a pid number, which is vulnerable to re-use, etc.

This functionality is limited to DRM_MASTER since abuse of this interface can have a negative impact on the system's performance.

v2: removed unused output structure

---
 drivers/gpu/drm/amd/amdgpu/Makefile       |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   |  19 +----
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c   |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 117 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h |  34 +++++++++
 include/uapi/drm/amdgpu_drm.h             |  18 +++++
 6 files changed, 173 insertions(+), 19 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index b62d9e9..e4d3b07 100644
@@ -25,7 +25,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
 	amdgpu_prime.o amdgpu_vm.o amdgpu_ib.o amdgpu_pll.o \
 	amdgpu_ucode.o amdgpu_bo_list.o amdgpu_ctx.o amdgpu_sync.o \
 	amdgpu_gtt_mgr.o amdgpu_vram_mgr.o amdgpu_virt.o amdgpu_atomfirmware.o \
-	amdgpu_queue_mgr.o
+	amdgpu_queue_mgr.o amdgpu_sched.o
 
 # add asic specific block
 amdgpu-$(CONFIG_DRM_AMDGPU_CIK)+= cik.o cik_ih.o kv_smc.o kv_dpm.o \
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -18
index dca483f..0f1c174 100644
@@ -25,6 +25,7 @@
 #include <drm/drmP.h>
 #include <drm/drm_auth.h>
 #include "amdgpu.h"
+#include "amdgpu_sched.h"
 
 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
 				      enum amd_sched_priority priority)
@@ -247,24 +248,6 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 	return 0;
 }
 
-static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
-{
-	switch (amdgpu_priority) {
-	case AMDGPU_CTX_PRIORITY_HIGH_HW:
-		return AMD_SCHED_PRIORITY_HIGH_HW;
-	case AMDGPU_CTX_PRIORITY_HIGH_SW:
-		return AMD_SCHED_PRIORITY_HIGH_SW;
-	case AMDGPU_CTX_PRIORITY_NORMAL:
-		return AMD_SCHED_PRIORITY_NORMAL;
-	case AMDGPU_CTX_PRIORITY_LOW_SW:
-	case AMDGPU_CTX_PRIORITY_LOW_HW:
-		return AMD_SCHED_PRIORITY_LOW;
-	default:
-		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
-		return AMD_SCHED_PRIORITY_INVALID;
-	}
-}
-
 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 		     struct drm_file *filp)
 {
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +2 -0
index 92e93b3..6a33c61 100644
@@ -28,6 +28,7 @@
 #include <drm/drmP.h>
 #include "amdgpu.h"
 #include <drm/amdgpu_drm.h>
+#include "amdgpu_sched.h"
 #include "amdgpu_uvd.h"
 #include "amdgpu_vce.h"
 
@@ -1016,6 +1017,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER),
 	DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	/* KMS */
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c new file +117 -0

117 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_sched.h new file +34 -0
new file mode 100644
index 0000000..b28c067
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Valve Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Andres Rodriguez <[email protected]>
+ */
+
+#ifndef __AMDGPU_SCHED_H__
+#define __AMDGPU_SCHED_H__
+
+#include <drm/drmP.h>
+
+enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority);
+int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
+		       struct drm_file *filp);
+
+#endif // __AMDGPU_SCHED_H__
include/uapi/drm/amdgpu_drm.h +18 -0
index 590fc2a..0604757 100644
@@ -52,6 +52,7 @@ extern "C" {
 #define DRM_AMDGPU_GEM_USERPTR		0x11
 #define DRM_AMDGPU_WAIT_FENCES		0x12
 #define DRM_AMDGPU_VM			0x13
+#define DRM_AMDGPU_SCHED		0x14
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -67,6 +68,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_USERPTR, struct drm_amdgpu_gem_userptr)
 #define DRM_IOCTL_AMDGPU_WAIT_FENCES	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_WAIT_FENCES, union drm_amdgpu_wait_fences)
 #define DRM_IOCTL_AMDGPU_VM		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
+#define DRM_IOCTL_AMDGPU_SCHED		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
 
 #define AMDGPU_GEM_DOMAIN_CPU		0x1
 #define AMDGPU_GEM_DOMAIN_GTT		0x2
@@ -220,6 +222,22 @@ union drm_amdgpu_vm {
 	struct drm_amdgpu_vm_out out;
 };
 
+/* sched ioctl */
+#define AMDGPU_SCHED_OP_PROCESS_PRIORITY_GET	1
+#define AMDGPU_SCHED_OP_PROCESS_PRIORITY_PUT	2
+
+struct drm_amdgpu_sched_in {
+	/* AMDGPU_SCHED_OP_* */
+	__u32	op;
+	__u32	fd;
+	__s32	priority;
+	__u32	flags;
+};
+
+union drm_amdgpu_sched {
+	struct drm_amdgpu_sched_in in;
+};
+
 /*
  * This is not a reliable API and you should expect it to fail for any
  * number of reasons and have fallback path that do not use userptr to
Signature · 6 lines
2.9.3

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