Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / lima / lima_ctx.c
blob891d5cd5019a7451200de317e9c9887b34d9377a
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2018-2019 Qiang Yu <yuq825@gmail.com> */
4 #include <linux/slab.h>
6 #include "lima_device.h"
7 #include "lima_ctx.h"
9 int lima_ctx_create(struct lima_device *dev, struct lima_ctx_mgr *mgr, u32 *id)
11 struct lima_ctx *ctx;
12 int i, err;
14 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
15 if (!ctx)
16 return -ENOMEM;
17 ctx->dev = dev;
18 kref_init(&ctx->refcnt);
20 for (i = 0; i < lima_pipe_num; i++) {
21 err = lima_sched_context_init(dev->pipe + i, ctx->context + i, &ctx->guilty);
22 if (err)
23 goto err_out0;
26 err = xa_alloc(&mgr->handles, id, ctx, xa_limit_32b, GFP_KERNEL);
27 if (err < 0)
28 goto err_out0;
30 ctx->pid = task_pid_nr(current);
31 get_task_comm(ctx->pname, current);
33 return 0;
35 err_out0:
36 for (i--; i >= 0; i--)
37 lima_sched_context_fini(dev->pipe + i, ctx->context + i);
38 kfree(ctx);
39 return err;
42 static void lima_ctx_do_release(struct kref *ref)
44 struct lima_ctx *ctx = container_of(ref, struct lima_ctx, refcnt);
45 int i;
47 for (i = 0; i < lima_pipe_num; i++)
48 lima_sched_context_fini(ctx->dev->pipe + i, ctx->context + i);
49 kfree(ctx);
52 int lima_ctx_free(struct lima_ctx_mgr *mgr, u32 id)
54 struct lima_ctx *ctx;
55 int ret = 0;
57 mutex_lock(&mgr->lock);
58 ctx = xa_erase(&mgr->handles, id);
59 if (ctx)
60 kref_put(&ctx->refcnt, lima_ctx_do_release);
61 else
62 ret = -EINVAL;
63 mutex_unlock(&mgr->lock);
64 return ret;
67 struct lima_ctx *lima_ctx_get(struct lima_ctx_mgr *mgr, u32 id)
69 struct lima_ctx *ctx;
71 mutex_lock(&mgr->lock);
72 ctx = xa_load(&mgr->handles, id);
73 if (ctx)
74 kref_get(&ctx->refcnt);
75 mutex_unlock(&mgr->lock);
76 return ctx;
79 void lima_ctx_put(struct lima_ctx *ctx)
81 kref_put(&ctx->refcnt, lima_ctx_do_release);
84 void lima_ctx_mgr_init(struct lima_ctx_mgr *mgr)
86 mutex_init(&mgr->lock);
87 xa_init_flags(&mgr->handles, XA_FLAGS_ALLOC);
90 void lima_ctx_mgr_fini(struct lima_ctx_mgr *mgr)
92 struct lima_ctx *ctx;
93 unsigned long id;
95 xa_for_each(&mgr->handles, id, ctx) {
96 kref_put(&ctx->refcnt, lima_ctx_do_release);
99 xa_destroy(&mgr->handles);
100 mutex_destroy(&mgr->lock);