1565c8854SFrançois Tigeot /*
272b9d63cSFrançois Tigeot * Copyright (c) 2019-2020 François Tigeot <ftigeot@wolfpond.org>
3565c8854SFrançois Tigeot * All rights reserved.
4565c8854SFrançois Tigeot *
5565c8854SFrançois Tigeot * Redistribution and use in source and binary forms, with or without
6565c8854SFrançois Tigeot * modification, are permitted provided that the following conditions
7565c8854SFrançois Tigeot * are met:
8565c8854SFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
9565c8854SFrançois Tigeot * notice unmodified, this list of conditions, and the following
10565c8854SFrançois Tigeot * disclaimer.
11565c8854SFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
12565c8854SFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
13565c8854SFrançois Tigeot * documentation and/or other materials provided with the distribution.
14565c8854SFrançois Tigeot *
15565c8854SFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16565c8854SFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17565c8854SFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18565c8854SFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19565c8854SFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20565c8854SFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21565c8854SFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22565c8854SFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23565c8854SFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24565c8854SFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25565c8854SFrançois Tigeot */
26565c8854SFrançois Tigeot
27565c8854SFrançois Tigeot #include <linux/fs.h>
28565c8854SFrançois Tigeot #include <linux/slab.h>
29565c8854SFrançois Tigeot #include <linux/dma-buf.h>
306559babbSFrançois Tigeot #include <linux/dma-fence.h>
31565c8854SFrançois Tigeot #include <linux/export.h>
32565c8854SFrançois Tigeot #include <linux/module.h>
33565c8854SFrançois Tigeot #include <linux/seq_file.h>
34565c8854SFrançois Tigeot #include <linux/poll.h>
35565c8854SFrançois Tigeot #include <linux/reservation.h>
36565c8854SFrançois Tigeot #include <linux/mm.h>
37565c8854SFrançois Tigeot
38*269e9b1eSFrançois Tigeot static int
dmabuf_stat(struct file * fp,struct stat * sb,struct ucred * cred)39*269e9b1eSFrançois Tigeot dmabuf_stat(struct file *fp, struct stat *sb, struct ucred *cred)
40565c8854SFrançois Tigeot {
41*269e9b1eSFrançois Tigeot struct dma_buf *dmabuf = fp->f_data;
42*269e9b1eSFrançois Tigeot
43*269e9b1eSFrançois Tigeot memset(sb, 0, sizeof(*sb));
44*269e9b1eSFrançois Tigeot sb->st_size = dmabuf->size;
45*269e9b1eSFrançois Tigeot sb->st_mode = S_IFIFO; /* XXX */
46*269e9b1eSFrançois Tigeot
47*269e9b1eSFrançois Tigeot return (0);
48*269e9b1eSFrançois Tigeot }
49*269e9b1eSFrançois Tigeot
50*269e9b1eSFrançois Tigeot static int
dmabuf_close(struct file * fp)51*269e9b1eSFrançois Tigeot dmabuf_close(struct file *fp)
52*269e9b1eSFrançois Tigeot {
53*269e9b1eSFrançois Tigeot kprintf("dmabuf_close(): not implemented\n");
54*269e9b1eSFrançois Tigeot return (EINVAL);
55*269e9b1eSFrançois Tigeot }
56*269e9b1eSFrançois Tigeot
57*269e9b1eSFrançois Tigeot struct fileops dmabuf_fileops = {
58*269e9b1eSFrançois Tigeot .fo_read = badfo_readwrite,
59*269e9b1eSFrançois Tigeot .fo_write = badfo_readwrite,
60*269e9b1eSFrançois Tigeot .fo_ioctl = badfo_ioctl,
61*269e9b1eSFrançois Tigeot .fo_kqfilter = badfo_kqfilter,
62*269e9b1eSFrançois Tigeot .fo_stat = dmabuf_stat,
63*269e9b1eSFrançois Tigeot .fo_close = dmabuf_close,
64*269e9b1eSFrançois Tigeot };
65*269e9b1eSFrançois Tigeot
66*269e9b1eSFrançois Tigeot struct dma_buf *
dma_buf_export(const struct dma_buf_export_info * exp_info)67*269e9b1eSFrançois Tigeot dma_buf_export(const struct dma_buf_export_info *exp_info)
68*269e9b1eSFrançois Tigeot {
69*269e9b1eSFrançois Tigeot struct dma_buf *dmabuf;
70*269e9b1eSFrançois Tigeot struct file *fp;
71*269e9b1eSFrançois Tigeot
72*269e9b1eSFrançois Tigeot falloc(curthread->td_lwp, &fp, NULL);
73*269e9b1eSFrançois Tigeot if (fp == NULL)
74*269e9b1eSFrançois Tigeot return ERR_PTR(-ENFILE);
75*269e9b1eSFrançois Tigeot
76*269e9b1eSFrançois Tigeot dmabuf = kmalloc(sizeof(struct dma_buf), M_DRM, M_WAITOK);
77*269e9b1eSFrançois Tigeot fp->f_type = DTYPE_DMABUF;
78*269e9b1eSFrançois Tigeot fp->f_ops = &dmabuf_fileops;
79*269e9b1eSFrançois Tigeot fp->private_data = dmabuf;
80*269e9b1eSFrançois Tigeot dmabuf->priv = exp_info->priv;
81*269e9b1eSFrançois Tigeot dmabuf->ops = exp_info->ops;
82*269e9b1eSFrançois Tigeot dmabuf->size = exp_info->size;
83*269e9b1eSFrançois Tigeot dmabuf->file = fp;
84*269e9b1eSFrançois Tigeot
85*269e9b1eSFrançois Tigeot return dmabuf;
86*269e9b1eSFrançois Tigeot }
87*269e9b1eSFrançois Tigeot
88*269e9b1eSFrançois Tigeot int
dma_buf_fd(struct dma_buf * dmabuf,int flags)89*269e9b1eSFrançois Tigeot dma_buf_fd(struct dma_buf *dmabuf, int flags)
90*269e9b1eSFrançois Tigeot {
91*269e9b1eSFrançois Tigeot int fd, error;
92*269e9b1eSFrançois Tigeot
93*269e9b1eSFrançois Tigeot if (dmabuf == NULL)
94*269e9b1eSFrançois Tigeot return -EINVAL;
95*269e9b1eSFrançois Tigeot
96*269e9b1eSFrançois Tigeot if (dmabuf->file == NULL)
97*269e9b1eSFrançois Tigeot return -EINVAL;
98*269e9b1eSFrançois Tigeot
99*269e9b1eSFrançois Tigeot if (flags & O_CLOEXEC) {
100*269e9b1eSFrançois Tigeot /* XXX: CLOEXEC not handled yet */
101*269e9b1eSFrançois Tigeot #if 0
102*269e9b1eSFrançois Tigeot __set_close_on_exec(fd, fdt);
103*269e9b1eSFrançois Tigeot else
104*269e9b1eSFrançois Tigeot __clear_close_on_exec(fd, fdt);
105*269e9b1eSFrançois Tigeot #endif
106*269e9b1eSFrançois Tigeot }
107*269e9b1eSFrançois Tigeot
108*269e9b1eSFrançois Tigeot error = fdalloc(curproc, 0, &fd);
109*269e9b1eSFrançois Tigeot if (error != 0)
110*269e9b1eSFrançois Tigeot return -error;
111*269e9b1eSFrançois Tigeot
112*269e9b1eSFrançois Tigeot fsetfd(curproc->p_fd, dmabuf->file, fd);
113*269e9b1eSFrançois Tigeot
114*269e9b1eSFrançois Tigeot return fd;
115*269e9b1eSFrançois Tigeot }
116*269e9b1eSFrançois Tigeot
117*269e9b1eSFrançois Tigeot struct dma_buf *
dma_buf_get(int fd)118*269e9b1eSFrançois Tigeot dma_buf_get(int fd)
119*269e9b1eSFrançois Tigeot {
120*269e9b1eSFrançois Tigeot struct file *fp;
121*269e9b1eSFrançois Tigeot struct dma_buf *dmabuf;
122*269e9b1eSFrançois Tigeot
123*269e9b1eSFrançois Tigeot if ((fp = holdfp(curthread, fd, -1)) == NULL)
124*269e9b1eSFrançois Tigeot return ERR_PTR(-EBADF);
125*269e9b1eSFrançois Tigeot
126*269e9b1eSFrançois Tigeot if (fp->f_ops != &dmabuf_fileops) {
127*269e9b1eSFrançois Tigeot kprintf("dma_buf_get(): file->f_ops != &dmabuf_fileops\n");
128*269e9b1eSFrançois Tigeot fdrop(fp);
129*269e9b1eSFrançois Tigeot return ERR_PTR(-EBADF);
130*269e9b1eSFrançois Tigeot }
131*269e9b1eSFrançois Tigeot
132*269e9b1eSFrançois Tigeot dmabuf = fp->private_data;
133*269e9b1eSFrançois Tigeot fdrop(fp);
134*269e9b1eSFrançois Tigeot
135*269e9b1eSFrançois Tigeot return dmabuf;
136565c8854SFrançois Tigeot }
137565c8854SFrançois Tigeot
13872b9d63cSFrançois Tigeot struct sg_table *
dma_buf_map_attachment(struct dma_buf_attachment * attach,enum dma_data_direction direction)13972b9d63cSFrançois Tigeot dma_buf_map_attachment(struct dma_buf_attachment *attach,
14072b9d63cSFrançois Tigeot enum dma_data_direction direction)
14172b9d63cSFrançois Tigeot {
14272b9d63cSFrançois Tigeot struct sg_table *sg_table;
14372b9d63cSFrançois Tigeot
14472b9d63cSFrançois Tigeot if (attach == NULL)
14572b9d63cSFrançois Tigeot return ERR_PTR(-EINVAL);
14672b9d63cSFrançois Tigeot
14772b9d63cSFrançois Tigeot if (attach->dmabuf == NULL)
14872b9d63cSFrançois Tigeot return ERR_PTR(-EINVAL);
14972b9d63cSFrançois Tigeot
15072b9d63cSFrançois Tigeot sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
15172b9d63cSFrançois Tigeot if (sg_table == NULL)
15272b9d63cSFrançois Tigeot return ERR_PTR(-ENOMEM);
15372b9d63cSFrançois Tigeot
15472b9d63cSFrançois Tigeot return sg_table;
15572b9d63cSFrançois Tigeot }
15672b9d63cSFrançois Tigeot
dma_buf_unmap_attachment(struct dma_buf_attachment * attach,struct sg_table * sg_table,enum dma_data_direction direction)157565c8854SFrançois Tigeot void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
158565c8854SFrançois Tigeot struct sg_table *sg_table,
159565c8854SFrançois Tigeot enum dma_data_direction direction)
160565c8854SFrançois Tigeot {
161565c8854SFrançois Tigeot }
162565c8854SFrançois Tigeot
163