xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/lima/lima_ctx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR MIT
2*4882a593Smuzhiyun /* Copyright 2018-2019 Qiang Yu <yuq825@gmail.com> */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include <linux/slab.h>
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include "lima_device.h"
7*4882a593Smuzhiyun #include "lima_ctx.h"
8*4882a593Smuzhiyun 
lima_ctx_create(struct lima_device * dev,struct lima_ctx_mgr * mgr,u32 * id)9*4882a593Smuzhiyun int lima_ctx_create(struct lima_device *dev, struct lima_ctx_mgr *mgr, u32 *id)
10*4882a593Smuzhiyun {
11*4882a593Smuzhiyun 	struct lima_ctx *ctx;
12*4882a593Smuzhiyun 	int i, err;
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
15*4882a593Smuzhiyun 	if (!ctx)
16*4882a593Smuzhiyun 		return -ENOMEM;
17*4882a593Smuzhiyun 	ctx->dev = dev;
18*4882a593Smuzhiyun 	kref_init(&ctx->refcnt);
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun 	for (i = 0; i < lima_pipe_num; i++) {
21*4882a593Smuzhiyun 		err = lima_sched_context_init(dev->pipe + i, ctx->context + i, &ctx->guilty);
22*4882a593Smuzhiyun 		if (err)
23*4882a593Smuzhiyun 			goto err_out0;
24*4882a593Smuzhiyun 	}
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	err = xa_alloc(&mgr->handles, id, ctx, xa_limit_32b, GFP_KERNEL);
27*4882a593Smuzhiyun 	if (err < 0)
28*4882a593Smuzhiyun 		goto err_out0;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	ctx->pid = task_pid_nr(current);
31*4882a593Smuzhiyun 	get_task_comm(ctx->pname, current);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	return 0;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun err_out0:
36*4882a593Smuzhiyun 	for (i--; i >= 0; i--)
37*4882a593Smuzhiyun 		lima_sched_context_fini(dev->pipe + i, ctx->context + i);
38*4882a593Smuzhiyun 	kfree(ctx);
39*4882a593Smuzhiyun 	return err;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
lima_ctx_do_release(struct kref * ref)42*4882a593Smuzhiyun static void lima_ctx_do_release(struct kref *ref)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct lima_ctx *ctx = container_of(ref, struct lima_ctx, refcnt);
45*4882a593Smuzhiyun 	int i;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	for (i = 0; i < lima_pipe_num; i++)
48*4882a593Smuzhiyun 		lima_sched_context_fini(ctx->dev->pipe + i, ctx->context + i);
49*4882a593Smuzhiyun 	kfree(ctx);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
lima_ctx_free(struct lima_ctx_mgr * mgr,u32 id)52*4882a593Smuzhiyun int lima_ctx_free(struct lima_ctx_mgr *mgr, u32 id)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	struct lima_ctx *ctx;
55*4882a593Smuzhiyun 	int ret = 0;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	mutex_lock(&mgr->lock);
58*4882a593Smuzhiyun 	ctx = xa_erase(&mgr->handles, id);
59*4882a593Smuzhiyun 	if (ctx)
60*4882a593Smuzhiyun 		kref_put(&ctx->refcnt, lima_ctx_do_release);
61*4882a593Smuzhiyun 	else
62*4882a593Smuzhiyun 		ret = -EINVAL;
63*4882a593Smuzhiyun 	mutex_unlock(&mgr->lock);
64*4882a593Smuzhiyun 	return ret;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
lima_ctx_get(struct lima_ctx_mgr * mgr,u32 id)67*4882a593Smuzhiyun struct lima_ctx *lima_ctx_get(struct lima_ctx_mgr *mgr, u32 id)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	struct lima_ctx *ctx;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	mutex_lock(&mgr->lock);
72*4882a593Smuzhiyun 	ctx = xa_load(&mgr->handles, id);
73*4882a593Smuzhiyun 	if (ctx)
74*4882a593Smuzhiyun 		kref_get(&ctx->refcnt);
75*4882a593Smuzhiyun 	mutex_unlock(&mgr->lock);
76*4882a593Smuzhiyun 	return ctx;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
lima_ctx_put(struct lima_ctx * ctx)79*4882a593Smuzhiyun void lima_ctx_put(struct lima_ctx *ctx)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	kref_put(&ctx->refcnt, lima_ctx_do_release);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
lima_ctx_mgr_init(struct lima_ctx_mgr * mgr)84*4882a593Smuzhiyun void lima_ctx_mgr_init(struct lima_ctx_mgr *mgr)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	mutex_init(&mgr->lock);
87*4882a593Smuzhiyun 	xa_init_flags(&mgr->handles, XA_FLAGS_ALLOC);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
lima_ctx_mgr_fini(struct lima_ctx_mgr * mgr)90*4882a593Smuzhiyun void lima_ctx_mgr_fini(struct lima_ctx_mgr *mgr)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct lima_ctx *ctx;
93*4882a593Smuzhiyun 	unsigned long id;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	xa_for_each(&mgr->handles, id, ctx) {
96*4882a593Smuzhiyun 		kref_put(&ctx->refcnt, lima_ctx_do_release);
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	xa_destroy(&mgr->handles);
100*4882a593Smuzhiyun 	mutex_destroy(&mgr->lock);
101*4882a593Smuzhiyun }
102