treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / lima / lima_ctx.c
blob22fff6caa961bf3fb5801d0d95975ec158a4a40e
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 return 0;
32 err_out0:
33 for (i--; i >= 0; i--)
34 lima_sched_context_fini(dev->pipe + i, ctx->context + i);
35 kfree(ctx);
36 return err;
39 static void lima_ctx_do_release(struct kref *ref)
41 struct lima_ctx *ctx = container_of(ref, struct lima_ctx, refcnt);
42 int i;
44 for (i = 0; i < lima_pipe_num; i++)
45 lima_sched_context_fini(ctx->dev->pipe + i, ctx->context + i);
46 kfree(ctx);
49 int lima_ctx_free(struct lima_ctx_mgr *mgr, u32 id)
51 struct lima_ctx *ctx;
52 int ret = 0;
54 mutex_lock(&mgr->lock);
55 ctx = xa_erase(&mgr->handles, id);
56 if (ctx)
57 kref_put(&ctx->refcnt, lima_ctx_do_release);
58 else
59 ret = -EINVAL;
60 mutex_unlock(&mgr->lock);
61 return ret;
64 struct lima_ctx *lima_ctx_get(struct lima_ctx_mgr *mgr, u32 id)
66 struct lima_ctx *ctx;
68 mutex_lock(&mgr->lock);
69 ctx = xa_load(&mgr->handles, id);
70 if (ctx)
71 kref_get(&ctx->refcnt);
72 mutex_unlock(&mgr->lock);
73 return ctx;
76 void lima_ctx_put(struct lima_ctx *ctx)
78 kref_put(&ctx->refcnt, lima_ctx_do_release);
81 void lima_ctx_mgr_init(struct lima_ctx_mgr *mgr)
83 mutex_init(&mgr->lock);
84 xa_init_flags(&mgr->handles, XA_FLAGS_ALLOC);
87 void lima_ctx_mgr_fini(struct lima_ctx_mgr *mgr)
89 struct lima_ctx *ctx;
90 unsigned long id;
92 xa_for_each(&mgr->handles, id, ctx) {
93 kref_put(&ctx->refcnt, lima_ctx_do_release);
96 xa_destroy(&mgr->handles);
97 mutex_destroy(&mgr->lock);