xref: /OK3568_Linux_fs/kernel/drivers/video/rockchip/rga3/rga_fence.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) Rockchip Electronics Co., Ltd.
4  *
5  * Author: Huang Lee <Putin.li@rock-chips.com>
6  */
7 
8 #define pr_fmt(fmt) "rga_fence: " fmt
9 
10 #include <linux/dma-fence.h>
11 #include <linux/sync_file.h>
12 #include <linux/slab.h>
13 
14 #include "rga_drv.h"
15 #include "rga_fence.h"
16 
rga_fence_get_name(struct dma_fence * fence)17 static const char *rga_fence_get_name(struct dma_fence *fence)
18 {
19 	return DRIVER_NAME;
20 }
21 
22 static const struct dma_fence_ops rga_fence_ops = {
23 	.get_driver_name = rga_fence_get_name,
24 	.get_timeline_name = rga_fence_get_name,
25 };
26 
rga_fence_context_init(struct rga_fence_context ** ctx)27 int rga_fence_context_init(struct rga_fence_context **ctx)
28 {
29 	struct rga_fence_context *fence_ctx = NULL;
30 
31 	fence_ctx = kzalloc(sizeof(struct rga_fence_context), GFP_KERNEL);
32 	if (!fence_ctx) {
33 		pr_err("can not kzalloc for rga_fence_context!\n");
34 		return -ENOMEM;
35 	}
36 
37 	fence_ctx->context = dma_fence_context_alloc(1);
38 	spin_lock_init(&fence_ctx->spinlock);
39 
40 	*ctx = fence_ctx;
41 
42 	return 0;
43 }
44 
rga_fence_context_remove(struct rga_fence_context ** ctx)45 void rga_fence_context_remove(struct rga_fence_context **ctx)
46 {
47 	if (*ctx == NULL)
48 		return;
49 
50 	kfree(*ctx);
51 	*ctx = NULL;
52 }
53 
rga_dma_fence_alloc(void)54 struct dma_fence *rga_dma_fence_alloc(void)
55 {
56 	struct rga_fence_context *fence_ctx = rga_drvdata->fence_ctx;
57 	struct dma_fence *fence = NULL;
58 
59 	if (fence_ctx == NULL) {
60 		pr_err("fence_context is NULL!\n");
61 		return ERR_PTR(-EINVAL);
62 	}
63 
64 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
65 	if (!fence)
66 		return ERR_PTR(-ENOMEM);
67 
68 	dma_fence_init(fence, &rga_fence_ops, &fence_ctx->spinlock,
69 		       fence_ctx->context, ++fence_ctx->seqno);
70 
71 	return fence;
72 }
73 
rga_dma_fence_get_fd(struct dma_fence * fence)74 int rga_dma_fence_get_fd(struct dma_fence *fence)
75 {
76 	struct sync_file *sync_file = NULL;
77 	int fence_fd = -1;
78 
79 	if (!fence)
80 		return -EINVAL;
81 
82 	fence_fd = get_unused_fd_flags(O_CLOEXEC);
83 	if (fence_fd < 0)
84 		return fence_fd;
85 
86 	sync_file = sync_file_create(fence);
87 	if (!sync_file) {
88 		put_unused_fd(fence_fd);
89 		return -ENOMEM;
90 	}
91 
92 	fd_install(fence_fd, sync_file->file);
93 
94 	return fence_fd;
95 }
96 
rga_get_dma_fence_from_fd(int fence_fd)97 struct dma_fence *rga_get_dma_fence_from_fd(int fence_fd)
98 {
99 	struct dma_fence *fence;
100 
101 	fence = sync_file_get_fence(fence_fd);
102 	if (!fence)
103 		pr_err("can not get fence from fd\n");
104 
105 	return fence;
106 }
107 
rga_dma_fence_wait(struct dma_fence * fence)108 int rga_dma_fence_wait(struct dma_fence *fence)
109 {
110 	int ret = 0;
111 
112 	ret = dma_fence_wait(fence, true);
113 
114 	dma_fence_put(fence);
115 
116 	return ret;
117 }
118 
rga_dma_fence_add_callback(struct dma_fence * fence,dma_fence_func_t func,void * private)119 int rga_dma_fence_add_callback(struct dma_fence *fence, dma_fence_func_t func, void *private)
120 {
121 	int ret;
122 	struct rga_fence_waiter *waiter = NULL;
123 
124 	waiter = kmalloc(sizeof(*waiter), GFP_KERNEL);
125 	if (!waiter) {
126 		pr_err("%s: Failed to allocate waiter\n", __func__);
127 		return -ENOMEM;
128 	}
129 
130 	waiter->private = private;
131 
132 	ret = dma_fence_add_callback(fence, &waiter->waiter, func);
133 	if (ret == -ENOENT) {
134 		pr_err("'input fence' has been already signaled.");
135 		goto err_free_waiter;
136 	} else if (ret == -EINVAL) {
137 		pr_err("%s: failed to add callback to dma_fence, err: %d\n", __func__, ret);
138 		goto err_free_waiter;
139 	}
140 
141 	return ret;
142 
143 err_free_waiter:
144 	kfree(waiter);
145 	return ret;
146 }
147