xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_dma_buf.c (revision 9ff13907d286a44cc5268ca9557a2582947e8089)
1 /*	$NetBSD: linux_dma_buf.c,v 1.10 2021/12/19 11:33:31 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.10 2021/12/19 11:33:31 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 struct dma_buf_file {
47 	struct dma_buf	*dbf_dmabuf;
48 };
49 
50 static int	dmabuf_fop_poll(struct file *, int);
51 static int	dmabuf_fop_close(struct file *);
52 static int	dmabuf_fop_kqfilter(struct file *, struct knote *);
53 static int	dmabuf_fop_mmap(struct file *, off_t *, size_t, int, int *,
54 		    int *, struct uvm_object **, int *);
55 
56 static const struct fileops dmabuf_fileops = {
57 	.fo_name = "dmabuf",
58 	.fo_read = fbadop_read,
59 	.fo_write = fbadop_write,
60 	.fo_ioctl = fbadop_ioctl,
61 	.fo_fcntl = fnullop_fcntl,
62 	.fo_poll = dmabuf_fop_poll,
63 	.fo_stat = fbadop_stat,
64 	.fo_close = dmabuf_fop_close,
65 	.fo_kqfilter = dmabuf_fop_kqfilter,
66 	.fo_restart = fnullop_restart,
67 	.fo_mmap = dmabuf_fop_mmap,
68 };
69 
70 struct dma_buf *
71 dma_buf_export(struct dma_buf_export_info *info)
72 {
73 	struct dma_buf *dmabuf;
74 
75 	if (info->resv == NULL) {
76 		dmabuf = kmem_zalloc(offsetof(struct dma_buf, db_resv_int[1]),
77 		    KM_SLEEP);
78 	} else {
79 		dmabuf = kmem_zalloc(sizeof(*dmabuf), KM_SLEEP);
80 	}
81 
82 	dmabuf->priv = info->priv;
83 	dmabuf->ops = info->ops;
84 	dmabuf->size = info->size;
85 	dmabuf->resv = info->resv;
86 
87 	mutex_init(&dmabuf->db_lock, MUTEX_DEFAULT, IPL_NONE);
88 	dmabuf->db_refcnt = 1;
89 	dma_resv_poll_init(&dmabuf->db_resv_poll);
90 
91 	if (dmabuf->resv == NULL) {
92 		dmabuf->resv = &dmabuf->db_resv_int[0];
93 		dma_resv_init(dmabuf->resv);
94 	}
95 
96 	return dmabuf;
97 }
98 
99 int
100 dma_buf_fd(struct dma_buf *dmabuf, int flags)
101 {
102 	struct file *file;
103 	int fd;
104 	unsigned refcnt __diagused;
105 	int ret;
106 
107 	ret = -fd_allocfile(&file, &fd);
108 	if (ret)
109 		goto out0;
110 
111 	refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
112 	KASSERT(refcnt > 1);
113 
114 	file->f_type = DTYPE_MISC;
115 	file->f_flag = 0;	/* XXX DRM code allows only O_CLOEXEC.  */
116 	file->f_ops = &dmabuf_fileops;
117 	file->f_data = dmabuf;
118 	fd_set_exclose(curlwp, fd, (flags & O_CLOEXEC) != 0);
119 	fd_affix(curproc, file, fd);
120 
121 	ret = fd;
122 out0:	return ret;
123 }
124 
125 struct dma_buf *
126 dma_buf_get(int fd)
127 {
128 	struct file *file;
129 	struct dma_buf *dmabuf;
130 	unsigned refcnt __diagused;
131 	int error;
132 
133 	if ((file = fd_getfile(fd)) == NULL) {
134 		error = EBADF;
135 		goto fail0;
136 	}
137 	if (file->f_type != DTYPE_MISC || file->f_ops != &dmabuf_fileops) {
138 		error = EINVAL;
139 		goto fail1;
140 	}
141 
142 	dmabuf = file->f_data;
143 	refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
144 	KASSERT(refcnt > 1);
145 	fd_putfile(fd);
146 	return dmabuf;
147 
148 fail1:	fd_putfile(fd);
149 fail0:	KASSERT(error);
150 	return ERR_PTR(-error);
151 }
152 
153 void
154 get_dma_buf(struct dma_buf *dmabuf)
155 {
156 	unsigned refcnt __diagused;
157 
158 	refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
159 	KASSERT(refcnt > 1);
160 }
161 
162 void
163 dma_buf_put(struct dma_buf *dmabuf)
164 {
165 
166 	if (atomic_dec_uint_nv(&dmabuf->db_refcnt) != 0)
167 		return;
168 
169 	dma_resv_poll_fini(&dmabuf->db_resv_poll);
170 	mutex_destroy(&dmabuf->db_lock);
171 	if (dmabuf->resv == &dmabuf->db_resv_int[0]) {
172 		dma_resv_fini(dmabuf->resv);
173 		kmem_free(dmabuf, offsetof(struct dma_buf, db_resv_int[1]));
174 	} else {
175 		kmem_free(dmabuf, sizeof(*dmabuf));
176 	}
177 }
178 
179 struct dma_buf_attachment *
180 dma_buf_attach(struct dma_buf *dmabuf, bus_dma_tag_t dmat)
181 {
182 	struct dma_buf_attachment *attach;
183 	int ret = 0;
184 
185 	attach = kmem_zalloc(sizeof(*attach), KM_SLEEP);
186 	attach->dmabuf = dmabuf;
187 	attach->dev = dmat;
188 
189 	mutex_enter(&dmabuf->db_lock);
190 	if (dmabuf->ops->attach)
191 		ret = dmabuf->ops->attach(dmabuf, attach);
192 	mutex_exit(&dmabuf->db_lock);
193 	if (ret)
194 		goto fail0;
195 
196 	return attach;
197 
198 fail0:	kmem_free(attach, sizeof(*attach));
199 	return ERR_PTR(ret);
200 }
201 
202 void
203 dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
204 {
205 
206 	mutex_enter(&dmabuf->db_lock);
207 	if (dmabuf->ops->detach)
208 		dmabuf->ops->detach(dmabuf, attach);
209 	mutex_exit(&dmabuf->db_lock);
210 
211 	kmem_free(attach, sizeof(*attach));
212 }
213 
214 struct sg_table *
215 dma_buf_map_attachment(struct dma_buf_attachment *attach,
216     enum dma_data_direction dir)
217 {
218 
219 	return attach->dmabuf->ops->map_dma_buf(attach, dir);
220 }
221 
222 void
223 dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
224     struct sg_table *sg, enum dma_data_direction dir)
225 {
226 
227 	return attach->dmabuf->ops->unmap_dma_buf(attach, sg, dir);
228 }
229 
230 static int
231 dmabuf_fop_close(struct file *file)
232 {
233 	struct dma_buf_file *dbf = file->f_data;
234 	struct dma_buf *dmabuf = dbf->dbf_dmabuf;
235 
236 	dma_buf_put(dmabuf);
237 	return 0;
238 }
239 
240 static int
241 dmabuf_fop_poll(struct file *file, int events)
242 {
243 	struct dma_buf_file *dbf = file->f_data;
244 	struct dma_buf *dmabuf = dbf->dbf_dmabuf;
245 	struct dma_resv_poll *rpoll = &dmabuf->db_resv_poll;
246 
247 	return dma_resv_do_poll(dmabuf->resv, events, rpoll);
248 }
249 
250 static int
251 dmabuf_fop_kqfilter(struct file *file, struct knote *kn)
252 {
253 	struct dma_buf_file *dbf = file->f_data;
254 	struct dma_buf *dmabuf = dbf->dbf_dmabuf;
255 	struct dma_resv_poll *rpoll = &dmabuf->db_resv_poll;
256 
257 	return dma_resv_kqfilter(dmabuf->resv, kn, rpoll);
258 }
259 
260 static int
261 dmabuf_fop_mmap(struct file *file, off_t *offp, size_t size, int prot,
262     int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp)
263 {
264 	struct dma_buf_file *dbf = file->f_data;
265 	struct dma_buf *dmabuf = dbf->dbf_dmabuf;
266 
267 	if (size > dmabuf->size)
268 		return EINVAL;
269 
270 	return dmabuf->ops->mmap(dmabuf, offp, size, prot, flagsp, advicep,
271 	    uobjp, maxprotp);
272 }
273