xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_dma_buf.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: linux_dma_buf.c,v 1.13 2021/12/19 12:01:40 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_dma_buf.c,v 1.13 2021/12/19 12:01:40 riastradh Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/atomic.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/kmem.h>
40 #include <sys/mutex.h>
41 
42 #include <linux/dma-buf.h>
43 #include <linux/err.h>
44 #include <linux/dma-resv.h>
45 
46 static int	dmabuf_fop_poll(struct file *, int);
47 static int	dmabuf_fop_close(struct file *);
48 static int	dmabuf_fop_kqfilter(struct file *, struct knote *);
49 static int	dmabuf_fop_mmap(struct file *, off_t *, size_t, int, int *,
50 		    int *, struct uvm_object **, int *);
51 
52 static const struct fileops dmabuf_fileops = {
53 	.fo_name = "dmabuf",
54 	.fo_read = fbadop_read,
55 	.fo_write = fbadop_write,
56 	.fo_ioctl = fbadop_ioctl,
57 	.fo_fcntl = fnullop_fcntl,
58 	.fo_poll = dmabuf_fop_poll,
59 	.fo_stat = fbadop_stat,
60 	.fo_close = dmabuf_fop_close,
61 	.fo_kqfilter = dmabuf_fop_kqfilter,
62 	.fo_restart = fnullop_restart,
63 	.fo_mmap = dmabuf_fop_mmap,
64 };
65 
66 struct dma_buf *
67 dma_buf_export(struct dma_buf_export_info *info)
68 {
69 	struct dma_buf *dmabuf;
70 
71 	if (info->resv == NULL) {
72 		dmabuf = kmem_zalloc(offsetof(struct dma_buf, db_resv_int[1]),
73 		    KM_SLEEP);
74 	} else {
75 		dmabuf = kmem_zalloc(sizeof(*dmabuf), KM_SLEEP);
76 	}
77 
78 	dmabuf->priv = info->priv;
79 	dmabuf->ops = info->ops;
80 	dmabuf->size = info->size;
81 	dmabuf->resv = info->resv;
82 
83 	mutex_init(&dmabuf->db_lock, MUTEX_DEFAULT, IPL_NONE);
84 	dmabuf->db_refcnt = 1;
85 	dma_resv_poll_init(&dmabuf->db_resv_poll);
86 
87 	if (dmabuf->resv == NULL) {
88 		dmabuf->resv = &dmabuf->db_resv_int[0];
89 		dma_resv_init(dmabuf->resv);
90 	}
91 
92 	return dmabuf;
93 }
94 
95 int
96 dma_buf_fd(struct dma_buf *dmabuf, int flags)
97 {
98 	struct file *file;
99 	int fd;
100 	unsigned refcnt __diagused;
101 	int ret;
102 
103 	/* XXX errno NetBSD->Linux */
104 	ret = -fd_allocfile(&file, &fd);
105 	if (ret)
106 		goto out0;
107 
108 	refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
109 	KASSERT(refcnt > 1);
110 
111 	file->f_type = DTYPE_MISC;
112 	file->f_flag = 0;	/* XXX DRM code allows only O_CLOEXEC.  */
113 	file->f_ops = &dmabuf_fileops;
114 	file->f_data = dmabuf;
115 	fd_set_exclose(curlwp, fd, (flags & O_CLOEXEC) != 0);
116 	fd_affix(curproc, file, fd);
117 
118 	ret = fd;
119 out0:	return ret;
120 }
121 
122 struct dma_buf *
123 dma_buf_get(int fd)
124 {
125 	struct file *file;
126 	struct dma_buf *dmabuf;
127 	unsigned refcnt __diagused;
128 	int error;
129 
130 	if ((file = fd_getfile(fd)) == NULL) {
131 		error = EBADF;
132 		goto fail0;
133 	}
134 	if (file->f_type != DTYPE_MISC || file->f_ops != &dmabuf_fileops) {
135 		error = EINVAL;
136 		goto fail1;
137 	}
138 
139 	dmabuf = file->f_data;
140 	refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
141 	KASSERT(refcnt > 1);
142 	fd_putfile(fd);
143 	return dmabuf;
144 
145 fail1:	fd_putfile(fd);
146 fail0:	KASSERT(error);
147 	return ERR_PTR(-error);
148 }
149 
150 void
151 get_dma_buf(struct dma_buf *dmabuf)
152 {
153 	unsigned refcnt __diagused;
154 
155 	refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
156 	KASSERT(refcnt > 1);
157 }
158 
159 void
160 dma_buf_put(struct dma_buf *dmabuf)
161 {
162 
163 	if (atomic_dec_uint_nv(&dmabuf->db_refcnt) != 0)
164 		return;
165 
166 	dma_resv_poll_fini(&dmabuf->db_resv_poll);
167 	mutex_destroy(&dmabuf->db_lock);
168 	if (dmabuf->resv == &dmabuf->db_resv_int[0]) {
169 		dma_resv_fini(dmabuf->resv);
170 		kmem_free(dmabuf, offsetof(struct dma_buf, db_resv_int[1]));
171 	} else {
172 		kmem_free(dmabuf, sizeof(*dmabuf));
173 	}
174 }
175 
176 struct dma_buf_attachment *
177 dma_buf_dynamic_attach(struct dma_buf *dmabuf, bus_dma_tag_t dmat,
178     bool dynamic_mapping)
179 {
180 	struct dma_buf_attachment *attach;
181 	int ret = 0;
182 
183 	attach = kmem_zalloc(sizeof(*attach), KM_SLEEP);
184 	attach->dmabuf = dmabuf;
185 	attach->dev = dmat;
186 	attach->dynamic_mapping = dynamic_mapping;
187 
188 	mutex_enter(&dmabuf->db_lock);
189 	if (dmabuf->ops->attach)
190 		ret = dmabuf->ops->attach(dmabuf, attach);
191 	mutex_exit(&dmabuf->db_lock);
192 	if (ret)
193 		goto fail0;
194 
195 	if (attach->dynamic_mapping != dmabuf->ops->dynamic_mapping)
196 		panic("%s: NYI", __func__);
197 
198 	return attach;
199 
200 fail0:	kmem_free(attach, sizeof(*attach));
201 	return ERR_PTR(ret);
202 }
203 
204 struct dma_buf_attachment *
205 dma_buf_attach(struct dma_buf *dmabuf, bus_dma_tag_t dmat)
206 {
207 
208 	return dma_buf_dynamic_attach(dmabuf, dmat, /*dynamic_mapping*/false);
209 }
210 
211 void
212 dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
213 {
214 
215 	mutex_enter(&dmabuf->db_lock);
216 	if (dmabuf->ops->detach)
217 		dmabuf->ops->detach(dmabuf, attach);
218 	mutex_exit(&dmabuf->db_lock);
219 
220 	kmem_free(attach, sizeof(*attach));
221 }
222 
223 struct sg_table *
224 dma_buf_map_attachment(struct dma_buf_attachment *attach,
225     enum dma_data_direction dir)
226 {
227 	struct sg_table *sg;
228 
229 	if (attach->dmabuf->ops->dynamic_mapping)
230 		dma_resv_lock(attach->dmabuf->resv, NULL);
231 	sg = attach->dmabuf->ops->map_dma_buf(attach, dir);
232 	if (attach->dmabuf->ops->dynamic_mapping)
233 		dma_resv_unlock(attach->dmabuf->resv);
234 
235 	return sg;
236 }
237 
238 void
239 dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
240     struct sg_table *sg, enum dma_data_direction dir)
241 {
242 
243 	if (attach->dmabuf->ops->dynamic_mapping)
244 		dma_resv_lock(attach->dmabuf->resv, NULL);
245 	attach->dmabuf->ops->unmap_dma_buf(attach, sg, dir);
246 	if (attach->dmabuf->ops->dynamic_mapping)
247 		dma_resv_unlock(attach->dmabuf->resv);
248 }
249 
250 static int
251 dmabuf_fop_close(struct file *file)
252 {
253 	struct dma_buf *dmabuf = file->f_data;
254 
255 	dma_buf_put(dmabuf);
256 	return 0;
257 }
258 
259 static int
260 dmabuf_fop_poll(struct file *file, int events)
261 {
262 	struct dma_buf *dmabuf = file->f_data;
263 	struct dma_resv_poll *rpoll = &dmabuf->db_resv_poll;
264 
265 	return dma_resv_do_poll(dmabuf->resv, events, rpoll);
266 }
267 
268 static int
269 dmabuf_fop_kqfilter(struct file *file, struct knote *kn)
270 {
271 	struct dma_buf *dmabuf = file->f_data;
272 	struct dma_resv_poll *rpoll = &dmabuf->db_resv_poll;
273 
274 	return dma_resv_kqfilter(dmabuf->resv, kn, rpoll);
275 }
276 
277 static int
278 dmabuf_fop_mmap(struct file *file, off_t *offp, size_t size, int prot,
279     int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp)
280 {
281 	struct dma_buf *dmabuf = file->f_data;
282 
283 	if (size > dmabuf->size)
284 		return EINVAL;
285 
286 	return dmabuf->ops->mmap(dmabuf, offp, size, prot, flagsp, advicep,
287 	    uobjp, maxprotp);
288 }
289