1 /*        $NetBSD: dma-buf.h,v 1.12 2021/12/19 12:01:40 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 #ifndef _LINUX_DMA_BUF_H_
33 #define _LINUX_DMA_BUF_H_
34 
35 #include <sys/types.h>
36 #include <sys/bus.h>
37 #include <sys/mutex.h>
38 
39 #include <linux/err.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/dma-resv.h>
42 #include <linux/scatterlist.h>
43 
44 struct device;
45 struct dma_buf;
46 struct dma_buf_attachment;
47 struct dma_buf_export_info;
48 struct dma_buf_ops;
49 struct file;
50 struct module;
51 struct dma_resv;
52 struct sg_table;
53 struct uvm_object;
54 
55 struct dma_buf_ops {
56           bool      cache_sgt_mapping;
57           bool      dynamic_mapping;
58           int       (*attach)(struct dma_buf *, struct dma_buf_attachment *);
59           void      (*detach)(struct dma_buf *, struct dma_buf_attachment *);
60           struct sg_table *
61                     (*map_dma_buf)(struct dma_buf_attachment *,
62                         enum dma_data_direction);
63           void      (*unmap_dma_buf)(struct dma_buf_attachment *,
64                         struct sg_table *, enum dma_data_direction);
65           void      (*release)(struct dma_buf *);
66           int       (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
67           int       (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
68           int       (*mmap)(struct dma_buf *, off_t *, size_t, int, int *,
69                         int *, struct uvm_object **, int *);
70           void *    (*vmap)(struct dma_buf *);
71           void      (*vunmap)(struct dma_buf *, void *);
72 };
73 
74 struct dma_buf {
75           void                                    *priv;
76           const struct dma_buf_ops      *ops;
77           size_t                                  size;
78           struct dma_resv                         *resv;
79 
80           kmutex_t                      db_lock;
81           volatile unsigned             db_refcnt;
82           struct dma_resv_poll                    db_resv_poll;
83           struct dma_resv                         db_resv_int[];
84 };
85 
86 struct dma_buf_attachment {
87           void                                    *priv;
88           struct dma_buf                          *dmabuf;
89           bus_dma_tag_t                           dev; /* XXX expedient misnomer */
90           bool                                    dynamic_mapping;
91 };
92 
93 struct dma_buf_export_info {
94 #if 0
95           const char                              *exp_name;
96           struct module                           *owner;
97 #endif
98           const struct dma_buf_ops      *ops;
99           size_t                                  size;
100           int                                     flags;
101           struct dma_resv                         *resv;
102           void                                    *priv;
103 };
104 
105 #define   DEFINE_DMA_BUF_EXPORT_INFO(info)                                            \
106           struct dma_buf_export_info info = { .priv = NULL }
107 
108 #define   dma_buf_attach                linux_dma_buf_attach
109 #define   dma_buf_detach                linux_dma_buf_detach
110 #define   dma_buf_dynamic_attach        linux_dma_buf_dynamic_attach
111 #define   dma_buf_export                linux_dma_buf_export
112 #define   dma_buf_fd                    linux_dma_buf_fd
113 #define   dma_buf_get                   linux_dma_buf_get
114 #define   dma_buf_map_attachment        linux_dma_buf_map_attachment
115 #define   dma_buf_put                   linux_dma_buf_put
116 #define   dma_buf_unmap_attachment linux_dma_buf_unmap_attachment
117 #define   get_dma_buf                   linux_get_dma_buf
118 
119 struct dma_buf *
120           dma_buf_export(struct dma_buf_export_info *);
121 
122 int       dma_buf_fd(struct dma_buf *, int);
123 struct dma_buf *
124           dma_buf_get(int);
125 void      get_dma_buf(struct dma_buf *);
126 void      dma_buf_put(struct dma_buf *);
127 
128 struct dma_buf_attachment *
129           dma_buf_attach(struct dma_buf *, bus_dma_tag_t);
130 struct dma_buf_attachment *
131           dma_buf_dynamic_attach(struct dma_buf *, bus_dma_tag_t, bool);
132 void      dma_buf_detach(struct dma_buf *, struct dma_buf_attachment *);
133 
134 struct sg_table *
135           dma_buf_map_attachment(struct dma_buf_attachment *,
136               enum dma_data_direction);
137 void      dma_buf_unmap_attachment(struct dma_buf_attachment *,
138               struct sg_table *, enum dma_data_direction);
139 
140 #endif  /* _LINUX_DMA_BUF_H_ */
141