[PATCH 1/3] drm/lima: add usage counting method to ctx_mgr

From: Erico Nunes
Date: Sun Mar 12 2023 - 19:31:55 EST


lima maintains a context manager per drm_file, similar to amdgpu.
In order to account for the complete usage per drm_file, all of the
associated contexts need to be considered.
Previously released contexts also need to be accounted for but their
drm_sched_entity info is gone once they get released, so account for it
in the ctx_mgr.

Signed-off-by: Erico Nunes <nunes.erico@xxxxxxxxx>
---
drivers/gpu/drm/lima/lima_ctx.c | 30 +++++++++++++++++++++++++++++-
drivers/gpu/drm/lima/lima_ctx.h | 3 +++
2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/lima/lima_ctx.c b/drivers/gpu/drm/lima/lima_ctx.c
index 891d5cd5019a..e008e586fad0 100644
--- a/drivers/gpu/drm/lima/lima_ctx.c
+++ b/drivers/gpu/drm/lima/lima_ctx.c
@@ -15,6 +15,7 @@ int lima_ctx_create(struct lima_device *dev, struct lima_ctx_mgr *mgr, u32 *id)
if (!ctx)
return -ENOMEM;
ctx->dev = dev;
+ ctx->mgr = mgr;
kref_init(&ctx->refcnt);

for (i = 0; i < lima_pipe_num; i++) {
@@ -42,10 +43,17 @@ int lima_ctx_create(struct lima_device *dev, struct lima_ctx_mgr *mgr, u32 *id)
static void lima_ctx_do_release(struct kref *ref)
{
struct lima_ctx *ctx = container_of(ref, struct lima_ctx, refcnt);
+ struct lima_ctx_mgr *mgr = ctx->mgr;
int i;

- for (i = 0; i < lima_pipe_num; i++)
+ for (i = 0; i < lima_pipe_num; i++) {
+ struct lima_sched_context *context = &ctx->context[i];
+ struct drm_sched_entity *entity = &context->base;
+
+ mgr->elapsed_ns[i] += entity->elapsed_ns;
+
lima_sched_context_fini(ctx->dev->pipe + i, ctx->context + i);
+ }
kfree(ctx);
}

@@ -99,3 +107,23 @@ void lima_ctx_mgr_fini(struct lima_ctx_mgr *mgr)
xa_destroy(&mgr->handles);
mutex_destroy(&mgr->lock);
}
+
+void lima_ctx_mgr_usage(struct lima_ctx_mgr *mgr, u64 usage[lima_pipe_num])
+{
+ struct lima_ctx *ctx;
+ unsigned long id;
+
+ for (int i = 0; i < lima_pipe_num; i++)
+ usage[i] = mgr->elapsed_ns[i];
+
+ mutex_lock(&mgr->lock);
+ xa_for_each(&mgr->handles, id, ctx) {
+ for (int i = 0; i < lima_pipe_num; i++) {
+ struct lima_sched_context *context = &ctx->context[i];
+ struct drm_sched_entity *entity = &context->base;
+
+ usage[i] += entity->elapsed_ns;
+ }
+ }
+ mutex_unlock(&mgr->lock);
+}
diff --git a/drivers/gpu/drm/lima/lima_ctx.h b/drivers/gpu/drm/lima/lima_ctx.h
index 74e2be09090f..6068863880eb 100644
--- a/drivers/gpu/drm/lima/lima_ctx.h
+++ b/drivers/gpu/drm/lima/lima_ctx.h
@@ -12,6 +12,7 @@
struct lima_ctx {
struct kref refcnt;
struct lima_device *dev;
+ struct lima_ctx_mgr *mgr;
struct lima_sched_context context[lima_pipe_num];
atomic_t guilty;

@@ -23,6 +24,7 @@ struct lima_ctx {
struct lima_ctx_mgr {
struct mutex lock;
struct xarray handles;
+ u64 elapsed_ns[lima_pipe_num];
};

int lima_ctx_create(struct lima_device *dev, struct lima_ctx_mgr *mgr, u32 *id);
@@ -31,5 +33,6 @@ struct lima_ctx *lima_ctx_get(struct lima_ctx_mgr *mgr, u32 id);
void lima_ctx_put(struct lima_ctx *ctx);
void lima_ctx_mgr_init(struct lima_ctx_mgr *mgr);
void lima_ctx_mgr_fini(struct lima_ctx_mgr *mgr);
+void lima_ctx_mgr_usage(struct lima_ctx_mgr *mgr, u64 usage[lima_pipe_num]);

#endif
--
2.39.2