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