0dc41378f59c0fe61bbb8b7bab2ffbe9ceb8f03b
[cascardo/linux.git] / include / linux / dma-buf.h
1 /*
2  * Header file for dma buffer sharing framework.
3  *
4  * Copyright(C) 2011 Linaro Limited. All rights reserved.
5  * Author: Sumit Semwal <sumit.semwal@ti.com>
6  *
7  * Many thanks to linaro-mm-sig list, and specially
8  * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
9  * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
10  * refining of this idea.
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License version 2 as published by
14  * the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License along with
22  * this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 #ifndef __DMA_BUF_H__
25 #define __DMA_BUF_H__
26
27 #include <linux/file.h>
28 #include <linux/err.h>
29 #include <linux/scatterlist.h>
30 #include <linux/list.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/fs.h>
33 #include <linux/kds.h>
34
35 struct device;
36 struct dma_buf;
37 struct dma_buf_attachment;
38
39 /**
40  * struct dma_buf_ops - operations possible on struct dma_buf
41  * @attach: [optional] allows different devices to 'attach' themselves to the
42  *          given buffer. It might return -EBUSY to signal that backing storage
43  *          is already allocated and incompatible with the requirements
44  *          of requesting device.
45  * @detach: [optional] detach a given device from this buffer.
46  * @map_dma_buf: returns list of scatter pages allocated, increases usecount
47  *               of the buffer. Requires atleast one attach to be called
48  *               before. Returned sg list should already be mapped into
49  *               _device_ address space. This call may sleep. May also return
50  *               -EINTR. Should return -EINVAL if attach hasn't been called yet.
51  * @unmap_dma_buf: decreases usecount of buffer, might deallocate scatter
52  *                 pages.
53  * @release: release this buffer; to be called after the last dma_buf_put.
54  * @begin_cpu_access: [optional] called before cpu access to invalidate cpu
55  *                    caches and allocate backing storage (if not yet done)
56  *                    respectively pin the objet into memory.
57  * @end_cpu_access: [optional] called after cpu access to flush cashes.
58  * @kmap_atomic: maps a page from the buffer into kernel address
59  *               space, users may not block until the subsequent unmap call.
60  *               This callback must not sleep.
61  * @kunmap_atomic: [optional] unmaps a atomically mapped page from the buffer.
62  *                 This Callback must not sleep.
63  * @kmap: maps a page from the buffer into kernel address space.
64  * @kunmap: [optional] unmaps a page from the buffer.
65  * @mmap: used to expose the backing storage to userspace. Note that the
66  *        mapping needs to be coherent - if the exporter doesn't directly
67  *        support this, it needs to fake coherency by shooting down any ptes
68  *        when transitioning away from the cpu domain.
69  * @vmap: [optional] creates a virtual mapping for the buffer into kernel
70  *        address space. Same restrictions as for vmap and friends apply.
71  * @vunmap: [optional] unmaps a vmap from the buffer
72  */
73 struct dma_buf_ops {
74         int (*attach)(struct dma_buf *, struct device *,
75                         struct dma_buf_attachment *);
76
77         void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
78
79         /* For {map,unmap}_dma_buf below, any specific buffer attributes
80          * required should get added to device_dma_parameters accessible
81          * via dev->dma_params.
82          */
83         struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
84                                                 enum dma_data_direction);
85         void (*unmap_dma_buf)(struct dma_buf_attachment *,
86                                                 struct sg_table *,
87                                                 enum dma_data_direction);
88         /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
89          * if the call would block.
90          */
91
92         /* after final dma_buf_put() */
93         void (*release)(struct dma_buf *);
94
95         int (*begin_cpu_access)(struct dma_buf *, size_t, size_t,
96                                 enum dma_data_direction);
97         void (*end_cpu_access)(struct dma_buf *, size_t, size_t,
98                                enum dma_data_direction);
99         void *(*kmap_atomic)(struct dma_buf *, unsigned long);
100         void (*kunmap_atomic)(struct dma_buf *, unsigned long, void *);
101         void *(*kmap)(struct dma_buf *, unsigned long);
102         void (*kunmap)(struct dma_buf *, unsigned long, void *);
103
104         int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
105
106         void *(*vmap)(struct dma_buf *);
107         void (*vunmap)(struct dma_buf *, void *vaddr);
108 };
109
110 /**
111  * struct dma_buf - shared buffer object
112  * @size: size of the buffer
113  * @file: file pointer used for sharing buffers across, and for refcounting.
114  * @attachments: list of dma_buf_attachment that denotes all devices attached.
115  * @ops: dma_buf_ops associated with this buffer object.
116  * @priv: exporter specific private data for this buffer object.
117  */
118 struct dma_buf {
119         size_t size;
120         struct file *file;
121         struct list_head attachments;
122         const struct dma_buf_ops *ops;
123         /* mutex to serialize list manipulation and attach/detach */
124         struct mutex lock;
125         struct kds_resource kds;
126         void *priv;
127 };
128
129 /**
130  * struct dma_buf_attachment - holds device-buffer attachment data
131  * @dmabuf: buffer for this attachment.
132  * @dev: device attached to the buffer.
133  * @node: list of dma_buf_attachment.
134  * @priv: exporter specific attachment data.
135  *
136  * This structure holds the attachment information between the dma_buf buffer
137  * and its user device(s). The list contains one attachment struct per device
138  * attached to the buffer.
139  */
140 struct dma_buf_attachment {
141         struct dma_buf *dmabuf;
142         struct device *dev;
143         struct list_head node;
144         void *priv;
145 };
146
147 /**
148  * get_dma_buf - convenience wrapper for get_file.
149  * @dmabuf:     [in]    pointer to dma_buf
150  *
151  * Increments the reference count on the dma-buf, needed in case of drivers
152  * that either need to create additional references to the dmabuf on the
153  * kernel side.  For example, an exporter that needs to keep a dmabuf ptr
154  * so that subsequent exports don't create a new dmabuf.
155  */
156 static inline void get_dma_buf(struct dma_buf *dmabuf)
157 {
158         get_file(dmabuf->file);
159 }
160
161 /**
162  * get_dma_buf_kds_resource - get a KDS resource for this dma-buf
163  * @dmabuf:     [in]    pointer to dma_buf
164  *
165  * Returns a KDS resource that represents the dma-buf. This should be used by
166  * drivers to synchronize access to the buffer. Note that the caller should
167  * ensure that a reference to the dma-buf exists from the call to
168  * kds_async_wait until kds_resource_set_release is called.
169  */
170 static inline struct kds_resource *
171         get_dma_buf_kds_resource(struct dma_buf *dmabuf)
172 {
173         return &dmabuf->kds;
174 }
175
176 #ifdef CONFIG_DMA_SHARED_BUFFER
177 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
178                                                         struct device *dev);
179 void dma_buf_detach(struct dma_buf *dmabuf,
180                                 struct dma_buf_attachment *dmabuf_attach);
181 struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
182                                size_t size, int flags);
183 int dma_buf_fd(struct dma_buf *dmabuf, int flags);
184 struct dma_buf *dma_buf_get(int fd);
185 void dma_buf_put(struct dma_buf *dmabuf);
186
187 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
188                                         enum dma_data_direction);
189 void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
190                                 enum dma_data_direction);
191 int dma_buf_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t len,
192                              enum dma_data_direction dir);
193 void dma_buf_end_cpu_access(struct dma_buf *dma_buf, size_t start, size_t len,
194                             enum dma_data_direction dir);
195 void *dma_buf_kmap_atomic(struct dma_buf *, unsigned long);
196 void dma_buf_kunmap_atomic(struct dma_buf *, unsigned long, void *);
197 void *dma_buf_kmap(struct dma_buf *, unsigned long);
198 void dma_buf_kunmap(struct dma_buf *, unsigned long, void *);
199
200 int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
201                  unsigned long);
202 void *dma_buf_vmap(struct dma_buf *);
203 void dma_buf_vunmap(struct dma_buf *, void *vaddr);
204 #else
205
206 static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
207                                                         struct device *dev)
208 {
209         return ERR_PTR(-ENODEV);
210 }
211
212 static inline void dma_buf_detach(struct dma_buf *dmabuf,
213                                   struct dma_buf_attachment *dmabuf_attach)
214 {
215         return;
216 }
217
218 static inline struct dma_buf *dma_buf_export(void *priv,
219                                              const struct dma_buf_ops *ops,
220                                              size_t size, int flags)
221 {
222         return ERR_PTR(-ENODEV);
223 }
224
225 static inline int dma_buf_fd(struct dma_buf *dmabuf, int flags)
226 {
227         return -ENODEV;
228 }
229
230 static inline struct dma_buf *dma_buf_get(int fd)
231 {
232         return ERR_PTR(-ENODEV);
233 }
234
235 static inline void dma_buf_put(struct dma_buf *dmabuf)
236 {
237         return;
238 }
239
240 static inline struct sg_table *dma_buf_map_attachment(
241         struct dma_buf_attachment *attach, enum dma_data_direction write)
242 {
243         return ERR_PTR(-ENODEV);
244 }
245
246 static inline void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
247                         struct sg_table *sg, enum dma_data_direction dir)
248 {
249         return;
250 }
251
252 static inline int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
253                                            size_t start, size_t len,
254                                            enum dma_data_direction dir)
255 {
256         return -ENODEV;
257 }
258
259 static inline void dma_buf_end_cpu_access(struct dma_buf *dmabuf,
260                                           size_t start, size_t len,
261                                           enum dma_data_direction dir)
262 {
263 }
264
265 static inline void *dma_buf_kmap_atomic(struct dma_buf *dmabuf,
266                                         unsigned long pnum)
267 {
268         return NULL;
269 }
270
271 static inline void dma_buf_kunmap_atomic(struct dma_buf *dmabuf,
272                                          unsigned long pnum, void *vaddr)
273 {
274 }
275
276 static inline void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long pnum)
277 {
278         return NULL;
279 }
280
281 static inline void dma_buf_kunmap(struct dma_buf *dmabuf,
282                                   unsigned long pnum, void *vaddr)
283 {
284 }
285
286 static inline int dma_buf_mmap(struct dma_buf *dmabuf,
287                                struct vm_area_struct *vma,
288                                unsigned long pgoff)
289 {
290         return -ENODEV;
291 }
292
293 static inline void *dma_buf_vmap(struct dma_buf *dmabuf)
294 {
295         return NULL;
296 }
297
298 static inline void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
299 {
300 }
301 #endif /* CONFIG_DMA_SHARED_BUFFER */
302
303 #endif /* __DMA_BUF_H__ */