1 // SPDX-License-Identifier: GPL-2.0 OR MIT
5 #include <linux/slab.h>
7 #include "lima_device.h"
10 int lima_ctx_create(struct lima_device *dev, struct lima_ctx_mgr *mgr, u32 *id)
15 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
19 kref_init(&ctx->refcnt);
21 for (i = 0; i < lima_pipe_num; i++) {
22 err = lima_sched_context_init(dev->pipe + i, ctx->context + i);
27 err = xa_alloc(&mgr->handles, id, ctx, xa_limit_32b, GFP_KERNEL);
31 ctx->pid = task_pid_nr(current);
32 get_task_comm(ctx->pname, current);
37 for (i--; i >= 0; i--)
38 lima_sched_context_fini(dev->pipe + i, ctx->context + i);
43 static void lima_ctx_do_release(struct kref *ref)
45 struct lima_ctx *ctx = container_of(ref, struct lima_ctx, refcnt);
48 for (i = 0; i < lima_pipe_num; i++)
49 lima_sched_context_fini(ctx->dev->pipe + i, ctx->context + i);
53 int lima_ctx_free(struct lima_ctx_mgr *mgr, u32 id)
58 mutex_lock(&mgr->lock);
59 ctx = xa_erase(&mgr->handles, id);
61 kref_put(&ctx->refcnt, lima_ctx_do_release);
64 mutex_unlock(&mgr->lock);
68 struct lima_ctx *lima_ctx_get(struct lima_ctx_mgr *mgr, u32 id)
72 mutex_lock(&mgr->lock);
73 ctx = xa_load(&mgr->handles, id);
75 kref_get(&ctx->refcnt);
76 mutex_unlock(&mgr->lock);
80 void lima_ctx_put(struct lima_ctx *ctx)
82 kref_put(&ctx->refcnt, lima_ctx_do_release);
85 void lima_ctx_mgr_init(struct lima_ctx_mgr *mgr)
87 mutex_init(&mgr->lock);
88 xa_init_flags(&mgr->handles, XA_FLAGS_ALLOC);
91 void lima_ctx_mgr_fini(struct lima_ctx_mgr *mgr)
96 xa_for_each(&mgr->handles, id, ctx) {
97 kref_put(&ctx->refcnt, lima_ctx_do_release);
100 xa_destroy(&mgr->handles);
101 mutex_destroy(&mgr->lock);