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