2 * Copyright (c) 2019-2020 François Tigeot <ftigeot@wolfpond.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <linux/slab.h>
29 #include <linux/dma-buf.h>
30 #include <linux/dma-fence.h>
31 #include <linux/export.h>
32 #include <linux/module.h>
33 #include <linux/seq_file.h>
34 #include <linux/poll.h>
35 #include <linux/reservation.h>
39 dmabuf_stat(struct file *fp, struct stat *sb, struct ucred *cred)
41 struct dma_buf *dmabuf = fp->f_data;
43 memset(sb, 0, sizeof(*sb));
44 sb->st_size = dmabuf->size;
45 sb->st_mode = S_IFIFO; /* XXX */
51 dmabuf_close(struct file *fp)
53 kprintf("dmabuf_close(): not implemented\n");
57 struct fileops dmabuf_fileops = {
58 .fo_read = badfo_readwrite,
59 .fo_write = badfo_readwrite,
60 .fo_ioctl = badfo_ioctl,
61 .fo_kqfilter = badfo_kqfilter,
62 .fo_stat = dmabuf_stat,
63 .fo_close = dmabuf_close,
67 dma_buf_export(const struct dma_buf_export_info *exp_info)
69 struct dma_buf *dmabuf;
72 falloc(curthread->td_lwp, &fp, NULL);
74 return ERR_PTR(-ENFILE);
76 dmabuf = kmalloc(sizeof(struct dma_buf), M_DRM, M_WAITOK);
77 fp->f_type = DTYPE_DMABUF;
78 fp->f_ops = &dmabuf_fileops;
79 fp->private_data = dmabuf;
80 dmabuf->priv = exp_info->priv;
81 dmabuf->ops = exp_info->ops;
82 dmabuf->size = exp_info->size;
89 dma_buf_fd(struct dma_buf *dmabuf, int flags)
96 if (dmabuf->file == NULL)
99 if (flags & O_CLOEXEC) {
100 /* XXX: CLOEXEC not handled yet */
102 __set_close_on_exec(fd, fdt);
104 __clear_close_on_exec(fd, fdt);
108 error = fdalloc(curproc, 0, &fd);
112 fsetfd(curproc->p_fd, dmabuf->file, fd);
121 struct dma_buf *dmabuf;
123 if ((fp = holdfp(curthread, fd, -1)) == NULL)
124 return ERR_PTR(-EBADF);
126 if (fp->f_ops != &dmabuf_fileops) {
127 kprintf("dma_buf_get(): file->f_ops != &dmabuf_fileops\n");
129 return ERR_PTR(-EBADF);
132 dmabuf = fp->private_data;
139 dma_buf_map_attachment(struct dma_buf_attachment *attach,
140 enum dma_data_direction direction)
142 struct sg_table *sg_table;
145 return ERR_PTR(-EINVAL);
147 if (attach->dmabuf == NULL)
148 return ERR_PTR(-EINVAL);
150 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
151 if (sg_table == NULL)
152 return ERR_PTR(-ENOMEM);
157 void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
158 struct sg_table *sg_table,
159 enum dma_data_direction direction)