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